Skip to content

Instantly share code, notes, and snippets.

@june29
Created September 4, 2008 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save june29/8822 to your computer and use it in GitHub Desktop.
Save june29/8822 to your computer and use it in GitHub Desktop.
// ==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