Created
September 4, 2008 17:46
-
-
Save june29/8822 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name QuickSearchForm | |
// @namespace http://june29.jp/ | |
// @include http://* | |
// @include https://* | |
// ==/UserScript== | |
(function() { | |
var BIND_KEY = '/'; | |
var searchform = null; | |
var firefox; | |
if(typeof unsafeWindow != 'undefined'){ | |
firefox = true; | |
} else { | |
firefox = false; | |
} | |
function focusForm() { | |
if(searchform) { | |
getFirstElementByXPath(searchform).focus(); | |
} | |
} | |
var skipEl = {'input': false, 'button': true, 'select': true, 'textarea': false, 'password': false}; | |
window.addEventListener('keypress', function(e) { | |
if (skipEl[e.target.tagName.toLowerCase()]) {return;} | |
var key = '' | |
+ ((e.ctrlKey) ? 'C' : '') | |
+ ((e.shiftKey) ? 'S' : '') | |
+ ((e.altKey || e.metaKey) ? 'A' : '') | |
+ '-'; | |
if(firefox){ | |
key += String.fromCharCode(e.charCode).toLowerCase(); | |
}else{ | |
key += String.fromCharCode(e.keyCode).toLowerCase(); | |
} | |
if (key == "C-"+BIND_KEY){ | |
focusForm(); | |
} | |
}, false); | |
function getFirstElementByXPath(xpath, node) { | |
var node = node || document; | |
var doc = node.ownerDocument ? node.ownerDocument : node; | |
var result = doc.evaluate(xpath, node, null, | |
XPathResult.FIRST_ORDERED_NODE_TYPE, null); | |
return result.singleNodeValue ? result.singleNodeValue : null; | |
} | |
GM_xmlhttpRequest({ | |
method: 'get', | |
url: 'http://wedata.net/databases/SearchForm/items.json', | |
onload: function(response) { | |
var data = eval(response.responseText); | |
data.forEach(function(datum) { | |
var url = datum['data']['url']; | |
if(location.href.match(url)) { | |
searchform = datum['data']['form']; | |
} | |
}); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment