[wiki] [sites] Adding script for navigation asked on the list || Thuban

From: <hg_AT_suckless.org>
Date: Thu, 8 Nov 2012 07:24:54 +0100 (CET)

changeset: 1000:bbccbfcf6ef5
tag: tip
user: Thuban <thuban_AT_singularity.fr>
date: Thu Nov 08 07:24:51 2012 +0100
files: surf.suckless.org/files/easier_key.md
description:
Adding script for navigation asked on the list


diff -r 2ea0645000de -r bbccbfcf6ef5 surf.suckless.org/files/easier_key.md
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/surf.suckless.org/files/easier_key.md Thu Nov 08 07:24:51 2012 +0100
_AT_@ -0,0 +1,135 @@
+Easier keyboard shortcuts
+=========
+
+Just a javascript to put in `~/.surf/script.js`, coming from userscript.
+It just reproduce the vim behavious, to navigate in pages without using
+the Ctrl key everytime :
+
+- h : scroll left
+- j : scroll down
+- k : scroll up
+- l : scroll right
+- g : Top of page
+- G : bottom of page
+
+
+<pre>
+// ==UserScript==
+// _AT_name vimkeybindings
+// _AT_namespace renevier.fdn.fr
+// _AT_author arno <arenevier_AT_fdn.fr>
+// _AT_licence GPL/LGPL/MPL
+// _AT_description use vim keybingings (i, j, k, l, …) to navigate a web page.
+// ==/UserScript==
+
+/*
+ * If you're a vim addict, and you always find yourself typing j or k in a web
+ * page, then wondering why it just does not go up and down like any good
+ * software, that user script is what you have been looking for.
+ */
+
+
+function up() {
+ if (window.scrollByLines)
+ window.scrollByLines(-1); // gecko
+ else
+ window.scrollBy(0, -12); // webkit
+}
+
+function down() {
+ if (window.scrollByLines)
+ window.scrollByLines(1); // gecko
+ else
+ window.scrollBy(0, 12); // webkit
+}
+
+function pageup() {
+ if (window.scrollByPages)
+ window.scrollByPages(-1); // gecko
+ else
+ window.scrollBy(0, 0 - _pageScroll()); // webkit
+}
+
+function pagedown() {
+ if (window.scrollByPages)
+ window.scrollByPages(1); // gecko
+ else
+ window.scrollBy(0, _pageScroll()); // webkit
+}
+
+function right() {
+ window.scrollBy(15, 0);
+}
+
+function left() {
+ window.scrollBy(-15, 0);
+}
+
+function home() {
+ window.scroll(0, 0);
+}
+
+function bottom() {
+ window.scroll(document.width, document.height);
+}
+
+// If you don't like default key bindings, customize here.
+// if you want to use the combination 'Ctrl + b' (for example), use '^b'
+var bindings = {
+ 'h' : left,
+ 'l' : right,
+ 'k' : up,
+ 'j' : down,
+ 'g' : home,
+ 'G' : bottom,
+ //'^b': pageup,
+ //'^f': pagedown,
+}
+
+function isEditable(element) {
+
+ if (element.nodeName.toLowerCase() == "textarea")
+ return true;
+
+ // we don't get keypress events for text input, but I don't known
+ // if it's a bug, so let's test that
+ if (element.nodeName.toLowerCase() == "input" && element.type == "text")
+ return true;
+
+ // element is editable
+ if (document.designMode == "on" || element.contentEditable == "true") {
+ return true;
+ }
+
+ return false;
+}
+
+function keypress(evt) {
+ var target = evt.target;
+
+ // if we're on a editable element, we probably don't want to catch
+ // keypress, we just want to write the typed character.
+ if (isEditable(target))
+ return;
+
+ var key = String.fromCharCode(evt.charCode);
+ if (evt.ctrlKey) {
+ key = '^' + key;
+ }
+
+ var fun = bindings[key];
+ if (fun)
+ fun();
+
+}
+
+function _pageScroll() {
+ // Gecko algorithm
+ // ----------------
+ // The page increment is the size of the page, minus the smaller of
+ // 10% of the size or 2 lines.
+ return window.innerHeight - Math.min(window.innerHeight / 10, 24);
+}
+
+window.addEventListener("keypress", keypress, false);
+</pre>
Received on Thu Nov 08 2012 - 07:24:54 CET

This archive was generated by hypermail 2.3.0 : Thu Nov 08 2012 - 07:36:06 CET