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