2 * This script is derived (by Jason Woofenden) from an example script from
3 * uzbl (see uzbl.org) and is thus presumably licensed under the GNU GPLv3. In
4 * any case, Jason's changes are in the public domain.
8 // @name numbered links
9 // @namespace http://patcavit.com/greasemonkey
10 // @description make all links followable with the keyboard
20 // This is activated (and canceled) by pressing the ^C
21 // Press L (first) to switch to one-hand mode
23 // I was getting some very funky return values from String.fromCharCode() for
24 // punctuation keys so I made my own table. You might need to change this for
25 // your computer/keyboard. You'll need to have every character in "charset" in
64 '76': 'l' // switch to one-hand mode
67 //Just some shortcuts and globals
68 var charset = 'thsnd-rcgmvwb/;789aefijkopquxyz234'; // update key_to_char if you add to this
69 var uzblid = 'uzbl_link_hint';
70 var uzbldivid = uzblid + '_div_container';
73 var links = document.links;
74 var forms = document.forms;
76 //Calculate element position to draw the hint
77 //Pretty accurate but on fails in some very fancy cases
78 function element_position(el) {
79 var x = el.offsetLeft;
81 var width = el.offsetWidth;
82 var height = el.offsetHeight;
83 while (el.offsetParent) {
88 return { x: x, y: y, width: width, height: height };
91 // Simulate a click on the element
92 function click_element(target, options) {
93 var event = target.ownerDocument.createEvent('MouseEvents');
94 var pos = element_position(target);
95 options = options || {};
99 options.type || 'click',
100 options.canBubble || true,
101 options.cancelable || true,
102 options.view || target.ownerDocument.defaultView,
104 options.screenX || pos.x - window.pageXOffset,
105 options.screenY || pos.y - window.pageYOffset,
106 options.clientX || pos.x,
107 options.clientY || pos.y,
108 options.ctrlKey || false,
109 options.altKey || false,
110 options.shiftKey || false,
111 options.metaKey || false,
112 options.button || 0, //0 = left, 1 = middle, 2 = right
113 options.relatedTarget || null
116 target.dispatchEvent(event);
119 //Calculate if an element is visible
120 function isVisible(el) {
127 if (!el.parentNode) {
131 if (el.style.display == 'none') {
134 if (el.style.visibility == 'hidden') {
138 return isVisible(el.parentNode);
140 //Calculate if an element is on the viewport.
141 function elementInViewport(el) {
142 var pos = element_position(el);
143 return pos.y < window.pageYOffset + window.innerHeight && pos.x < window.pageXOffset + window.innerWidth && (pos.y + pos.height) > window.pageYOffset && (pos.x + pos.width) > window.pageXOffset;
145 //Removes all hints/leftovers that might be generated
147 function removeAllHints() {
148 var elements = doc.getElementById(uzbldivid);
150 elements.parentNode.removeChild(elements);
153 //Generate a hint for an element with the given label
154 //Here you can play around with the style of the hints!
155 function generateHint(el, label) {
156 var pos = element_position(el);
157 var hint = doc.createElement('div');
158 hint.setAttribute('name', uzblid);
159 hint.innerText = label;
160 hint.style.display = 'inline';
161 hint.style.backgroundColor = '#B9FF00';
162 hint.style.border = '2px solid #4A6600';
163 hint.style.color = 'black';
164 hint.style.fontSize = '9px';
165 hint.style.fontWeight = 'bold';
166 hint.style.lineHeight = '9px';
167 hint.style.margin = '0px';
168 hint.style.width = 'auto'; // fix broken rendering on w3schools.com
169 hint.style.padding = '1px';
170 hint.style.position = 'absolute';
171 hint.style.zIndex = '1000';
172 hint.style.textTransform = 'uppercase';
173 hint.style.left = Math.max(-1, (pos.x - (7 + label.length * 9))) + 'px';
174 hint.style.top = (pos.y + 1) + 'px';
175 var img = el.getElementsByTagName('img');
176 //if (img.length > 0) {
177 //hint.style.top = pos.x + img[0].height / 2 - 6 + 'px';
179 hint.style.textDecoration = 'none';
180 // hint.style.webkitBorderRadius = '6px'; // slow
183 //Here we choose what to do with an element if we
184 //want to "follow" it. On form elements we "select"
185 //or pass the focus, on links we try to perform a click,
186 //but at least set the href of the link. (needs some improvements)
187 function clickElem(item) {
190 var name = item.tagName;
193 window.location = item.href;
194 } else if (name == 'INPUT') {
195 var type = item.getAttribute('type').toUpperCase();
196 if (type == 'TEXT' || type == 'FILE' || type == 'PASSWORD') {
202 } else if (name == 'TEXTAREA' || name == 'SELECT') {
207 window.location = item.href;
211 //Returns a list of all links (in this version
212 //just the elements itself, but in other versions, we
213 //add the label here.
214 function addLinks() {
216 for (var l = 0; l < links.length; l++) {
218 if (isVisible(li) && elementInViewport(li)) {
224 //Same as above, just for the form elements
225 function addFormElems() {
227 for (var f = 0; f < forms.length; f++) {
228 for (var e = 0; e < forms[f].elements.length; e++) {
229 var el = forms[f].elements[e];
230 if (el && ['INPUT', 'TEXTAREA', 'SELECT'].indexOf(el.tagName) + 1 && isVisible(el) && elementInViewport(el)) {
237 //Draw all hints for all elements passed. "len" is for
238 //the number of chars we should use to avoid collisions
239 function reDrawHints(elems, chars) {
241 var hintdiv = doc.createElement('div');
242 hintdiv.setAttribute('id', uzbldivid);
243 for (var i = 0; i < elems[0].length; i++) {
245 var label = elems[1][i].substring(chars);
246 var h = generateHint(elems[0][i], label);
247 hintdiv.appendChild(h);
251 document.body.appendChild(hintdiv);
254 // pass: number of keys
255 // returns: key length
256 function labelLength(n) {
262 n -= 1; // our highest key will be n-1
265 n = Math.floor(n / charset.length);
271 function intToLabel(n) {
274 label = charset.charAt(n % charset.length) + label;
275 n = Math.floor(n / charset.length);
281 function labelToInt(label) {
284 for(i = 0; i < label.length; ++i) {
286 n += charset.indexOf(label[i]);
290 //Put it all together
291 function followLinks(follow) {
292 if(follow.charAt(0) == 'l') {
293 follow = follow.substr(1);
294 charset = 'thsnlrcgfdbmwvz-/';
296 var s = follow.split('');
297 var linknr = labelToInt(follow);
298 var linkelems = addLinks();
299 var formelems = addFormElems();
300 var elems = [linkelems[0].concat(formelems[0]), linkelems[1].concat(formelems[1])];
301 var len = labelLength(elems[0].length);
302 var oldDiv = doc.getElementById(uzbldivid);
303 var leftover = [[], []];
304 if (s.length == len && linknr < elems[0].length && linknr >= 0) {
305 clickElem(elems[0][linknr]);
309 for (var j = 0; j < elems[0].length; j++) {
311 var label = intToLabel(j);
312 var n = label.length;
313 for (n; n < len; n++) {
314 label = charset.charAt(0) + label;
316 for (var k = 0; k < s.length; k++) {
317 b = b && label.charAt(k) == s[k];
320 leftover[0].push(elems[0][j]);
321 leftover[1].push(label);
324 reDrawHints(leftover, s.length);
328 // from your event handler you can: return stop_event(e)
329 function stop_event(e) {
334 // return false; // IE-compat
339 document.addEventListener(
342 // [de]activate on ^C
345 (e.ctrlKey && e.keyCode == 67)
346 || (e.keyCode == 27 && active == 1)
355 return stop_event(e);
357 if(active == 1 && !e.ctrlKey && !e.shiftKey && !e.altKey) {
358 if(key_to_char[e.keyCode]) {
359 got += key_to_char[e.keyCode];
361 return stop_event(e);
364 // general keybinding, unrelated to numbered links
365 if (document.activeElement == document.body) { // FIXME get more specific
366 var c = key_to_char[e.keyCode];
369 window.scrollBy(0, -200);
373 window.scrollBy(0, 200);
377 } else if (e.keyCode == 27) {
379 document.activeElement.blur();