focus.js - firefox-fix-web - Firefox extension: fix web
(HTM) git clone git://git.codemadness.org/firefox-fix-web
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
focus.js (1260B)
---
1 (function() {
2 function hasfocus(el) {
3 return el === document.activeElement;
4 }
5 function findfield() {
6 var found = null;
7 var el = document.activeElement;
8 if(typeof(el) == 'object') {
9 var type = (el.type || '').toLowerCase();
10 if(el.tagName == 'INPUT' && ['text', 'search'].indexOf(type) != -1)
11 found = el;
12 }
13 if(found === null) {
14 var els = document.getElementsByTagName('input') || [];
15 for(var i = 0; i < els.length; i++) {
16 var type = (els[i].type || '').toLowerCase();
17 if(['text', 'search'].indexOf(type) != -1) {
18 found = els[i];
19 break;
20 }
21 }
22 }
23 if(found !== null) {
24 return found;
25 }
26 return null;
27 }
28 function focusfield(el) {
29 if(el === null)
30 return el;
31 el.click(); // Some sites require this to clear the input field.
32 var value = el.value || '';
33 el.selectionStart = 0; // value.length;
34 el.selectionEnd = value.length;
35 el.focus();
36 return el;
37 }
38 function blurfield(el) {
39 el.blur();
40 }
41 function onkeydown(e) {
42 if(e.ctrlKey && e.keyCode == 32) { /* ctrl + space */
43 var el = findfield();
44 /* toggle */
45 if(hasfocus(el)) {
46 blurfield(el);
47 } else {
48 focusfield(el);
49 }
50
51 }
52 }
53 window.addEventListener('keydown', onkeydown, true);
54 // focusfield(findfield());
55 })();