Skip to content

Instantly share code, notes, and snippets.

@arvidj
Forked from anonymous/gist:10787
Created September 15, 2008 22:36
Show Gist options
  • Save arvidj/10958 to your computer and use it in GitHub Desktop.
Save arvidj/10958 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',
_baseUrl: 'http://dearcomputer.nl/gir/',
_resRegexp: new RegExp('<a target="blank" href="(.+?)">'),
_nrImages: 5,
_size: 3,
_nsfw: 'on',
execute: function(directObject) {
var searchTerm = directObject.text;
var params = { q: searchTerm, nsfw: this._nsfw, s: this._size };
var url = this._baseUrl + '?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 params = { q: searchTerm, nsfw: 'on', s: this._size };
var that = this;
jQuery.get(this._baseUrl, params, function(data) {
var imgurls = execAll(data, that._resRegexp, that._nrImages)
.map(function (v) { return v[1]; });
var pdoc = pblock.ownerDocument;
imgurls.forEach(function (url) {
var img = pdoc.createElement('img');
img.addEventListener('load', function() {
pblock.appendChild(img);
}, false);
img.setAttribute('src', url);
});
});
}
});
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