-
-
Save jasherai/75466 to your computer and use it in GitHub Desktop.
This file contains 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
const Google = 'http://google.com/', | |
PLang = 'extensions.ubiquity.google.lang', | |
PSafe = 'extensions.ubiquity.google.safe', | |
PSize = 'extensions.ubiquity.google.size', | |
{prefs} = Application, | |
ID = '#_'+ (Math.random() +'').slice(2), | |
Logo = <a class="logo" href={Google}><img border="0" | |
src={Google +'intl/en_ALL/images/logo.gif'} height="110"/></a>, | |
Base = <div class="google"><style><![CDATA[ | |
@{position:relative; line-height:1.25} | |
@ul {list-style:none; padding:0; margin:0} | |
@li {padding-bottom:0.4em} | |
@button {padding:0; border-width:1px} | |
@button[disabled]{background-color:transparent; color:inherit} | |
@li button {margin-right:3px; vertical-align:bottom} | |
@kbd {text-decoration:underline; margin:0 3px 0 1px} | |
@button, @kbd {font:bold 92% "Consolas", monospace} | |
@.count {position:absolute; right:4px} | |
@.pagination {text-align:center} | |
@.logo {display:block} | |
@.error {font-style:oblique; line-height:1.8} | |
@.loading + .logo {opacity:0.4} | |
@.error + .logo {opacity:0.7} | |
]]></style><div id={ID.slice(1)} | |
/></div>.toXMLString().replace(/@/g, '.google '); | |
var entry, logot; | |
CmdUtils.CreateCommand({ | |
name: "google", | |
icon: Google +'favicon.ico', | |
takes: {'?': {_name: 'google-suggest', suggest: function(txt, htm, cb){ | |
jQuery.getJSON( | |
Google +'complete/search?json&hl='+ prefs.getValue(PLang, '') + | |
'&q='+ encodeURIComponent(txt), | |
function([, qs, ns]){ | |
qs.length && cb(qs.map(function(q, i)({ | |
text: q, summary: q +' '+ ns[i].small()}))) }); | |
return [{text: txt, summary: txt}]; | |
}}}, | |
modifiers: {in: {_name: 'language', suggest: function(txt, htm, cb, sx){ | |
if(sx || !txt) return []; | |
txt = txt.toLowerCase(); | |
return [{summary: k, data: LanguageCodes[k]} | |
for(k in LanguageCodes) if(~k.indexOf(txt))]; | |
}}}, | |
description: ''+<><a href={Google}>google</a> + multi-page + | |
<a href="http://labs.google.com/suggestfaq.html">suggest</a></>, | |
help: | |
[[<a href={Google +'language_tools'}>Language</a>, | |
PLang, LanguageCodes, 'en'], | |
[<a href={Google +'safesearch_help.html'}>Safe Search</a>, | |
PSafe, {active: 'active', moderate: 'moderate', off: 'off'}, 'moderate'], | |
[<>Result Size</>, PSize, {4: 'small', 8: 'large'}, 'small'], | |
].reduce(function(form, [name, pref, dict, defv]){ | |
var li = <li/>, select = <select onchange={ | |
'Application.prefs.setValue("'+ pref +'", this.value)'}/>; | |
defv = prefs.getValue(pref, defv); | |
for each(var [key, val] in Iterator(dict)){ | |
var opt = <option value={val}>{key}</option>; | |
if(val === defv) opt.@selected = 'selected'; | |
select.* += opt; | |
} | |
li.* += name + select; | |
form.ul.* += li; | |
return form; | |
}, <form>Options:<ul style="list-style:none"/></form>) +'', | |
execute: function(inp, mod){ | |
Utils.openUrlInBrowser(Google +'search'+ | |
Utils.paramsToString(params(inp, mod))); | |
}, | |
preview: function(pbl, inp, mod, index){ | |
var {q, hl, safe} = params(inp, mod), me = this, | |
$p = jQuery(pbl), $c = $p.find(ID); | |
if(!$c[0]) $c = $p.html(Base).find(ID).after(Logo +''), entry = ''; | |
if(!q || entry === (entry = q + hl + safe + index)) return; | |
var rsz = prefs.getValue(PSize, 'small'); | |
$c.addClass('loading'); | |
CmdUtils.previewAjax(pbl, { | |
url: 'http://ajax.googleapis.com/ajax/services/search/web', | |
data: {v: '1.0', q: q, hl: hl, safe: safe, | |
rsz: rsz, start: index << (rsz === 'small' ? 2 : 3)}, | |
dataType: 'json', | |
success: function({responseStatus: stat, responseData: data}){ | |
if(stat !== 200) return oops( | |
$c, stat === 400 ? 'Out of page range.' : 'Error: '+ stat); | |
var {results, cursor} = data; | |
if(!results.length) return oops($c, 'No results.'); | |
$c.removeClass().html(render(results, cursor)); | |
$c.find('.b_open').each(function(i){ | |
tie(this, function(){ | |
this.disabled = true; | |
Utils.openUrlInBrowser(results[i].unescapedUrl); | |
})}); | |
$c.find('.b_page').each(function(){ | |
this.disabled || tie(this, function(){ | |
me.preview(pbl, inp, mod, this.value - 1); | |
})}); | |
}, | |
error: function(x){ oops($c, x.status, x.statusText) }, | |
}); | |
var now = Date.now(); | |
if(now - logot < 8 * 60 * 6e4) return; | |
logot = now; | |
jQuery.get(Google +'webhp', function(htm){ | |
var [lg] = /<img [^>]+/(htm) || 0; | |
if(!lg) return; | |
Logo.img.@src = Google + (/\bsrc=\"?\/([^\"\s]+)/(lg) || 0)[1]; | |
Logo.img.@height = (/\bheight=\D?(\d+)/(lg) || 0)[1]; | |
}); | |
}, | |
previewDelay: 333, | |
author: 'satyr'.link('http://d.hatena.ne.jp/murky-satyr'), license: 'MIT', | |
}); | |
function params({text}, {in: {data}})({ | |
q: jQuery.trim(text), hl: data || prefs.getValue(PLang, ''), | |
safe: prefs.getValue(PSafe, ''), ie: 'utf-8'}); | |
function tie(it, fn){ | |
it.addEventListener('focus', function(e){ | |
this.blur(); | |
fn.call(it); | |
e.preventDefault(); | |
e.stopPropagation(); | |
}, true); | |
} | |
function oops($c){ | |
$c.addClass('error').text(Array.slice(arguments, 1).join(' ')); | |
} | |
function render(res, cur){ | |
var a2h, i2p, q2x, ggl = [ | |
'<div class="count">', | |
cur.estimatedResultCount.link(cur.moreResultsUrl), '</div>', | |
paginates(cur.currentPageIndex + 1, cur.pages.length), '<ul>']; | |
for(var i in res) with(res[i]) ggl.push( | |
'<li><button class="b_open" accesskey="', a2h = 'ABCDEFGH'[i], '">', a2h, | |
'</button><a class="title" href="', unescapedUrl, '" accesskey="', | |
i2p = 'IJKLMNOP'[i] ,'">', title, '</a><kbd>', i2p, | |
'</kbd><span class="meta"><a href="', cacheUrl, '" accesskey="', | |
q2x = 'QRSTUVWX'[i], '">[cache]</a><kbd>', q2x, '</kbd> ', visibleUrl, | |
'</span><div class="content">', content, '</div></li>'); | |
return ggl.join('') +'</ul>'; | |
} | |
function paginates(cpi, len)( | |
'<div class="pagination">'+ | |
btn('<,', ',', cpi <= 1, cpi - 1) + | |
[btn(++i, i, cpi === i, i) for(i in Array(len)+0)].join('') + | |
btn('.>', '.', cpi >= len, cpi + 1) + | |
'</div>'); | |
function btn(txt, key, dis, val)( | |
'<button class="b_page" accesskey="'+ key +'" value="'+ val + | |
(dis ? '" disabled="disabled">' : '">') + txt +'</button>'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
``