Created
September 7, 2008 09:07
-
-
Save satyr/9248 to your computer and use it in GitHub Desktop.
[Ubiquity] google+
This file contains hidden or 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(nums, pq, results, cursor, list){ | |
const Google = 'http://www.google.com/', | |
noun_type_page = { | |
_name: 'page', | |
suggest: function(txt){ | |
var n = ~~txt % (this.pages.length + 1); | |
return [{data: i, summary: i} for(i in this.pages) if(++i >= n)]; | |
}, | |
pages: [1], | |
}; | |
CmdUtils.CreateCommand({ | |
name: "google", | |
icon: Google +'favicon.ico', | |
takes: {'?': noun_arb_text}, | |
modifiers: { | |
'@': noun_type_page, | |
'>': { | |
_name: 'open', | |
suggest: function(txt, m) (m = (txt || '').match(/[1-4a-d]/ig)) | |
? [{data: m, summary: m}] : [{data: [1], summary: 1}], | |
}, | |
'#': noun_type_language}, | |
description: '++'+ 'google'.link(Google), | |
help: 'Try "google foobar > abc".', | |
execute: function(input){ var n; | |
if(results) for(n in nums) | |
results[n] && Utils.openUrlInBrowser(results[n].unescapedUrl); | |
if(!n){ | |
Utils.openUrlInBrowser(URL + encodeURIComponent(input.text)); | |
CmdUtils.setLastResult(input.text); | |
} | |
clear(); | |
}, | |
preview: function(block, input, mod){ | |
var term = jQuery.trim(input.text); | |
if(!term){ block.innerHTML = Logo; clear(); return } | |
var open = mod['>'].data || []; | |
var page = mod['@'].data || 1; | |
var lang = mod['#'].data || 'en'; | |
nums = open.reduce(function(d, x) ( | |
d[+x ? x - 1 : parseInt(x, 18) % 10] = true, d), {}); | |
var q = term + page + lang; | |
if(pq === q){ block.innerHTML = list; checkRows(block); return } | |
pq = q; | |
block.innerHTML = Loading + Logo; | |
CmdUtils.previewGet(block, APIURL, { | |
q: term, start: page - 1 << 2, hl: lang, | |
v: '1.0', rsz: 'small', safe: 'off', | |
}, function(o){ | |
var stat = o.responseStatus; | |
if(stat !== 200) | |
return error(block, stat === 400 ? 'Out of page range.' : stat); | |
({results, cursor}) = o.responseData; | |
noun_type_page.pages = cursor.pages; | |
makeTable(block); | |
}, 'json'); | |
}, | |
author: 'satyr'.link('http://d.hatena.ne.jp/murky-satyr'), | |
license: 'MIT', | |
}); | |
function clear() nums = pq = results = cursor = list = ''; | |
function error(block, msg){ | |
block.innerHTML = <div class="google"><div class="error">{ | |
'Error: '+ msg}</div></div> + Logo; | |
} | |
function makeTable(block){ | |
if(!results.length) return error(block, 'No matches.'); | |
var i = 0, a = [ | |
Style, '<div class="google"><div class="info">page ', | |
cursor.currentPageIndex + 1, '/', cursor.pages.length, ' | ', | |
cursor.estimatedResultCount.link(cursor.moreResultsUrl), '</div><table>']; | |
for each(var r in results) a.push( | |
'<tr><th>', (++i+9).toString(18), | |
'/<kbd class="access">', i, '</kbd></th>', | |
'<td><a href="', r.unescapedUrl, '" accesskey="', i, '">', r.title, | |
'</a> <span class="url">', r.visibleUrl, ' [cache]'.link(r.cacheUrl), | |
'</span><div class="content">', r.content, '</div></td></tr>'); | |
a.push('</table></div>', Logo); | |
block.innerHTML = list = a.join(''); | |
checkRows(block); | |
} | |
function checkRows(block){ | |
nums && Array.forEach( | |
block.getElementsByTagName('tr'), | |
function(e, i){ e.className = nums[i] ? 'selected' : '' }); | |
} | |
const URL = Google +'search?ie=utf-8&q='; | |
const APIURL = 'http://ajax.googleapis.com/ajax/services/search/web'; | |
const Loading = <p class="loading">...</p>; | |
const Logo = <div class="logo" style="margin-top:8px"><a | |
href={Google}><img border="0" | |
src={Google +'intl/en_ALL/images/logo.gif'}/></a></div>; | |
jQuery.get(Google +'webhp', function(htm){ | |
var [, s, h] = /<img src=\/(logos\S+).*?height=(\d+)/(htm) || 0; | |
if(!s) return; | |
var img = Logo..img; | |
img.@src = Google + s; | |
img.@height = h; | |
}); | |
const Style = <style><![CDATA[ | |
table { border: 0; font-size: 84% } | |
th, td { vertical-align: top } | |
.url { font-size: 90% } | |
.info { text-align: right; font-size: 80% } | |
.selected, .error { font-style: italic } | |
kbd.access { text-decoration: underline } | |
]]></style>.toXMLString(); | |
/* | |
function makePlugin(to, options){ | |
var target = Utils.__globalObject['cmd_'+ to]; | |
var opts = { | |
icon: target.icon, | |
modifiers: target.modifiers, | |
execute: function(input, mod){ target(this.input(), mod) }, | |
preview: function(block, input, mod){ | |
target.preview(block, this.input(), mod) }, | |
}; | |
for(var k in options) opts[k] = options[k]; | |
opts.description += ' by invoking "'+ to +'" command.'; | |
CmdUtils.CreateCommand(opts); | |
} | |
makePlugin('google', { | |
name: 'grelated', | |
description: 'Shows related sites to the current page', | |
input: function() | |
({text: 'related:'+ context.focusedWindow.document.location}), | |
}); | |
makePlugin('google', { | |
name: 'glink', | |
description: 'Shows linking sites to the current page', | |
input: function() | |
({text: 'link:'+ context.focusedWindow.document.location}), | |
}); | |
*/ | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment