index.md - sites - public wiki contents of suckless.org
(HTM) git clone git://git.suckless.org/sites
(DIR) Log
(DIR) Files
(DIR) Refs
---
index.md (3094B)
---
1 Easier keyboard shortcuts
2 =========================
3
4 Just a javascript to put in `~/.surf/script.js`, coming from userscript.
5 It just reproduce the vim behavious, to navigate in pages without using
6 the Ctrl key everytime :
7
8 * h: scroll left
9 * j: scroll down
10 * k: scroll up
11 * l: scroll right
12 * g: Top of page
13 * G: bottom of page
14
15 // ==UserScript==
16 // @name vimkeybindings
17 // @namespace renevier.fdn.fr
18 // @author arno <arenevier@fdn.fr>
19 // @licence GPL/LGPL/MPL
20 // @description use vim keybingings (i, j, k, l, …) to navigate a web page.
21 // ==/UserScript==
22
23 /*
24 * If you're a vim addict, and you always find yourself typing j or k in a web
25 * page, then wondering why it just does not go up and down like any good
26 * software, that user script is what you have been looking for.
27 */
28
29
30 function up() {
31 if (window.scrollByLines)
32 window.scrollByLines(-1); // gecko
33 else
34 window.scrollBy(0, -12); // webkit
35 }
36
37 function down() {
38 if (window.scrollByLines)
39 window.scrollByLines(1); // gecko
40 else
41 window.scrollBy(0, 12); // webkit
42 }
43
44 function pageup() {
45 if (window.scrollByPages)
46 window.scrollByPages(-1); // gecko
47 else
48 window.scrollBy(0, 0 - _pageScroll()); // webkit
49 }
50
51 function pagedown() {
52 if (window.scrollByPages)
53 window.scrollByPages(1); // gecko
54 else
55 window.scrollBy(0, _pageScroll()); // webkit
56 }
57
58 function right() {
59 window.scrollBy(15, 0);
60 }
61
62 function left() {
63 window.scrollBy(-15, 0);
64 }
65
66 function home() {
67 window.scroll(0, 0);
68 }
69
70 function bottom() {
71 window.scroll(document.width, document.height);
72 }
73
74 // If you don't like default key bindings, customize here.
75 // if you want to use the combination 'Ctrl + b' (for example), use '^b'
76 var bindings = {
77 'h' : left,
78 'l' : right,
79 'k' : up,
80 'j' : down,
81 'g' : home,
82 'G' : bottom,
83 //'^b': pageup,
84 //'^f': pagedown,
85 }
86
87 function isEditable(element) {
88
89 if (element.nodeName.toLowerCase() == "textarea")
90 return true;
91
92 // we don't get keypress events for text input, but I don't known
93 // if it's a bug, so let's test that
94 if (element.nodeName.toLowerCase() == "input" && element.type == "text")
95 return true;
96
97 // element is editable
98 if (document.designMode == "on" || element.contentEditable == "true") {
99 return true;
100 }
101
102 return false;
103 }
104
105 function keypress(evt) {
106 var target = evt.target;
107
108 // if we're on a editable element, we probably don't want to catch
109 // keypress, we just want to write the typed character.
110 if (isEditable(target))
111 return;
112
113 var key = String.fromCharCode(evt.charCode);
114 if (evt.ctrlKey) {
115 key = '^' + key;
116 }
117
118 var fun = bindings[key];
119 if (fun)
120 fun();
121
122 }
123
124 function _pageScroll() {
125 // Gecko algorithm
126 // ----------------
127 // The page increment is the size of the page, minus the smaller of
128 // 10% of the size or 2 lines.
129 return window.innerHeight - Math.min(window.innerHeight / 10, 24);
130 }
131
132 window.addEventListener("keypress", keypress, false);