Skip to content

Instantly share code, notes, and snippets.

@hotchpotch
Created February 8, 2009 12:07
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 hotchpotch/60363 to your computer and use it in GitHub Desktop.
Save hotchpotch/60363 to your computer and use it in GitHub Desktop.
(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