Created
February 8, 2009 12:07
-
-
Save hotchpotch/60363 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
(function() { | |
var p = function (arg) { | |
Application.console.log(arg); | |
// liberator.log(arg); | |
} | |
function $X (exp, context, resolver) { | |
context || (context = document); | |
var Doc = context.ownerDocument || context; | |
var result = Doc.evaluate(exp, context, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | |
for (var i = 0, len = result.snapshotLength, res = []; i < len; i++) { | |
res.push(result.snapshotItem(i)); | |
} | |
return res; | |
} | |
commands.addUserCommand(['google'], | |
'Google Search, and AutoComplete', | |
function (args) { | |
var url = args.string; | |
if (url.indexOf('http') != 0) { | |
url = 'http://www.google.co.jp/search?q=' + encodeURIComponent(args.string); | |
} | |
liberator.open(url, args.bang? liberator.NEW_TAB : null); | |
}, { | |
completer: function (context) { | |
context.filters = [function() true]; | |
var [lang] = Cc['@mozilla.org/network/protocol;1?name=http'] | |
.getService(Ci.nsIHttpProtocolHandler) | |
.language.split('-', 1); | |
var xhr = new XMLHttpRequest(); | |
var endpoint = 'http://www.google.co.jp/search'; | |
var reqURL = endpoint + '?hl=' + lang + '&q=' + encodeURIComponent(context.filter); | |
xhr.open('GET', reqURL, false); | |
xhr.send(null); | |
var div = window.content.document.createElement('div'); | |
div.innerHTML = xhr.responseText; | |
var elements = $X('//div/ol/li/h3/a', div); | |
var regex = new RegExp('/url\\?q=([^&]+)'); | |
context.completions = elements.map(function(e) { | |
if (e.href.match(regex)) { | |
// for moshikasite? | |
return [decodeURIComponent(RegExp.$1), e.textContent]; | |
} else { | |
return [e.href, e.textContent]; | |
} | |
}); | |
}, | |
argCount: '1', | |
bang: true, | |
}, | |
true | |
); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment