Skip to content

Instantly share code, notes, and snippets.

Created September 15, 2008 01:15
Show Gist options
  • Save anonymous/10787 to your computer and use it in GitHub Desktop.
Save anonymous/10787 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
name: 'gir',
takes: {"search term": noun_arb_text},
icon: "http://dearcomputer.nl/favicon.php",
description: 'Google Image Ripper',
_resRegexp: new RegExp('<a target="blank" href="(.+?)">'),
_nrImages: 5,
_size: 3,
execute: function(directObject) {
var searchTerm = directObject.text;
var params = { q: searchTerm, nsfw: 'on', s: this._size };
var url = 'http://dearcomputer.nl/gir/?q=${q}&nsfw=${nsfw}&s=${s}';
Utils.openUrlInBrowser(CmdUtils.renderTemplate(url, params));
},
preview: function(pblock, directObject) {
var searchTerm = directObject.text;
var pTemplate = "Searches Google Image Ripper for <b>${query}</b>";
pblock.innerHTML = CmdUtils.renderTemplate(pTemplate, {query: searchTerm});
var url = "http://dearcomputer.nl/gir/";
var params = { q: searchTerm, nsfw: 'on', s: this._size };
var that = this;
jQuery.get(url, params, function(data) {
var m = execAll(data, that._resRegexp, that._nrImages)
.map(function (v) { return v[1]; });
var template = '{for result in results} '
+ '<div style=\"clear: both\"><img src=${result} style="float: left; background-color: white;"/></div>'
+ '{forelse}'
+ '<b>Your search - ${searchTerm} in size ${size} did not match any images.</b>'
+ '{/for}';
pblock.innerHTML = CmdUtils.renderTemplate(template, {results: m, searchTerm: searchTerm, size: that._size});
});
}
});
function execAll(str, re, max) {
var idx = 0, m, res = [], _max = max || 0;
while ((res.length < _max || _max == 0) && (m = re.exec(str))) {
res.push(m);
str = RegExp.rightContext;
CmdUtils.log(m);
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment