// ==UserScript== // @name Input focus // @namespace - // @description Input focus // @version 1.1 // @grant none // ==/UserScript== // // 1.1 // - added toggle to blur field (ctrl+space). // (function() { function hasfocus(el) { return el === document.activeElement; } function findfield() { var found = null; var el = document.activeElement; if(typeof(el) == 'object') { var type = (el.type || '').toLowerCase(); if(el.tagName == 'INPUT' && ['text', 'search'].indexOf(type) != -1) found = el; } if(found === null) { var els = document.getElementsByTagName('input') || []; for(var i = 0; i < els.length; i++) { var type = (els[i].type || '').toLowerCase(); if(['text', 'search'].indexOf(type) != -1) { found = els[i]; break; } } } if(found !== null) { return found; } return null; } function focusfield(el) { if(el === null) return el; el.click(); // Some sites require this to clear the input field. var value = el.value || ''; el.selectionStart = 0; // value.length; el.selectionEnd = value.length; el.focus(); return el; } function blurfield(el) { el.blur(); } function onkeydown(e) { if(e.ctrlKey && e.keyCode == 32) { /* ctrl + space */ var el = findfield(); /* toggle */ if(hasfocus(el)) { blurfield(el); } else { focusfield(el); } } } window.addEventListener('keydown', onkeydown, true); // focusfield(findfield()); })();