Skip to content

Instantly share code, notes, and snippets.

@satyr
Created September 9, 2008 06:40
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 satyr/9624 to your computer and use it in GitHub Desktop.
Save satyr/9624 to your computer and use it in GitHub Desktop.
[Ubiquity] Google Image
+function(nums, pq, results, cursor){
var noun_type_page = {
_name: 'page',
suggest: function(txt, htm)
[{data: i, summary: i} for(i in this.pages) if(++i >= +txt)],
pages: [1],
};
var noun_type_open = {
_name: 'open',
suggest: function(txt, m)
(m = (txt || '').match(/\w/g)) ? [CmdUtils.makeSugg(txt, null, m)] : [],
};
CmdUtils.CreateCommand({
name: "gimage",
icon: 'http://www.google.com/favicon.ico',
takes: {'search terms': noun_arb_text},
modifiers: {
'@': noun_type_page,
'>': noun_type_open,
'#': noun_type_language},
description:
'Searches <a href="http://image.google.com/">Google Image</a>.',
execute: function(input){ var n;
if(results) for(n in nums)
results[n] && Utils.openUrlInBrowser(results[n].unescapedUrl);
if(!n){
var url = URL + encodeURIComponent(input.text);
Utils.openUrlInBrowser(url);
CmdUtils.setLastResult(url);
}
nums = results = cursor = null;
},
preview: function(block, input, mod){
if(!block) return;
var term = jQuery.trim(input.text);
var open = mod['>'].data || [];
var page = mod['@'].data || 1;
var lang = mod['#'].data || 'en';
nums = open.reduce(function(d, x)(d[parseInt(x, 18) % 10] = true, d), {});
var q = term + page + lang;
if(pq === q) return makeList(block);
block.style.height = CmdUtils.getWindow().innerHeight +'px';
pq = q;
block.innerHTML = Loading;
CmdUtils.previewGet(block, APIURL, {
q: term, start: page - 1 << 3, hl: lang,
v: '1.0', rsz: 'large', safe: 'off',
}, function(o){
var stat = o.responseStatus;
if(stat != 200) return block.innerHTML =
(stat == 400 ? 'Out of page range.'
: stat +': Error.').italics() + Logo;
({results, cursor}) = o.responseData;
noun_type_page.pages = cursor.pages;
makeList(block);
}, 'json');
},
});
function makeList(div){
if(!results) return div.innerHTML = Logo;
if(!results.length) return div.innerHTML = '<i>No matches.</i>'+ Logo;
var a = [
'<style>ul {font-size:72%} li{list-style:none;float:left;padding:0}',
'.info{text-align:right;font-size:80%} .selected{font-style:italic}',
'</style><div class="gimage"><div class="info">page ',
cursor.currentPageIndex + 1, '/', cursor.pages.length, ' | ',
cursor.estimatedResultCount.link(cursor.moreResultsUrl), '</div><ul>'];
for(var [i, r] in Iterator(results)) with(r) a.push(
'<li style="width:', tbWidth, 'px"><a href="', unescapedUrl,
'" accesskey="', ++i, '"><img src="', tbUrl, '" border="0" alt="',
contentNoFormatting, '"></a><span>', (i+9).toString(18), '/<u>', i,
'</u> ', width, 'x', height, ' ', visibleUrl.link(originalContextUrl),
'</span></li>');
a.push('</ul></div>', '<br style="clear:left"/>', Logo);
div.innerHTML = a.join('');
checkItems(div);
}
function checkItems(block){ nums && Array.forEach(
block.getElementsByTagName('li'),
function(e, i){ e.className = nums[i] ? 'selected' : '' }) }
const URL = 'http://image.google.com/images?ie=utf-8&q=';
const APIURL = 'http://www.google.com/uds/GimageSearch';
const Loading = '<p class="loading">...</p>';
const LogoURL = 'http://www.google.com/';
var Logo = <div class='logo' style='height:100px;margin-top:8px'><a
href={LogoURL}><img border='0'
src={LogoURL +'intl/en_ALL/images/logo.gif'}/></a></div>;
jQuery.get(LogoURL, function(htm){
var m = /<img src=\/logos[^>]+>/(htm);
if(m) Logo = m[0].replace('/', LogoURL).link(LogoURL);
});
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment