Instantly share code, notes, and snippets.
Created
January 9, 2009 01:16
-
Star
0
(0)
You must be signed in to star a gist -
Fork
1
(1)
You must be signed in to fork a gist
-
Save satyr/44979 to your computer and use it in GitHub Desktop.
Obsolete. See: http://gist.github.com/60561
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(pgg, logot){ | |
const Google = 'http://www.google.com/', | |
PKSafe = 'extensions.ubiquity.google.safe', | |
PKLang = 'extensions.ubiquity.google.lang'; | |
CmdUtils.CreateCommand({ | |
name: "google", | |
icon: Google +'favicon.ico', | |
takes: {'?': noun_arb_text}, | |
modifiers: {'in': {_name: 'lang', suggest: function(txt, htm, cb, sx){ | |
if(!sx && txt){ | |
var re = RegExp(txt, 'i'); | |
for(var [k, v] in Iterator(LanguageCodes)) | |
re.test(k) && cb(CmdUtils.makeSugg(k, k, v)); | |
} | |
return [] }}}, | |
description: ''+<><a href={Google}>google</a> + keyboard navigation</>, | |
help: ''+<ul style="list-style-image:none"> | |
<li>Try the buttons with corresponding accesskeys.</li> | |
<li><a href="about:config">Options</a>:<dl> | |
<dt><a href={Google +'safesearch_help.html'}>SafeSearch</a></dt> | |
<dd><code>{PKSafe} = "active"|"moderate"|"off"</code></dd> | |
<dt><a href={Google +'language_tools'}>Language</a></dt> | |
<dd><code>{PKLang} = "en"|"ja"|...</code></dd> | |
</dl></li></ul>, | |
execute: function(inp, mod){ Utils.openUrlInBrowser(makeReq(inp, mod).g) }, | |
preview: function(pbl, inp, mod, page){ | |
var {g, term, lang, safe} = makeReq(inp, mod), me = this, | |
$p = jQuery(pbl), $c = $p.find(ID); | |
if(!$c[0]) $c = $p.html(Base).find(ID).after(Logo +''), pgg = ''; | |
if(!term || pgg === (pgg = page + g)) return; | |
CmdUtils.previewAjax(pbl, { | |
url: 'http://ajax.googleapis.com/ajax/services/search/web', | |
data: { | |
q: term, hl: lang, safe: safe, start: page << 2, | |
v: '1.0', rsz: 'small'}, | |
dataType: 'json', | |
success: function(o){ | |
var stat = o.responseStatus; | |
if(stat !== 200) | |
return oops($c, stat === 400 ? 'Out of page range.' : stat); | |
var {results, cursor} = o.responseData; | |
if(!results.length) return oops($c, 'No matches.'); | |
($c.html(makeList(results, cursor)) | |
.find('button.rs').each(function(i){ | |
tie(this, function(){ | |
this.disabled = true; | |
Utils.openUrlInBrowser(results[i].unescapedUrl); | |
})}).end() | |
.find('button.pg').each(function(){ | |
this.disabled || tie(this, function(){ | |
me.preview(pbl, inp, mod, this.value - 1); | |
})})); | |
}, | |
error: function(x, e){ | |
oops($c, x.status +' '+ x.statusText +'/'+ e) }, | |
}); | |
var now = Date.now(); | |
if(now - logot < 8 * 60 * 60 * 1e3) return; | |
logot = now; | |
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; | |
}); | |
}, | |
previewDelay: 333, | |
author: 'satyr'.link('http://d.hatena.ne.jp/murky-satyr'), license: 'MIT' | |
});{} | |
function makeReq(inp, mod){ | |
var term = jQuery.trim(inp.text), safe = prefs.getValue(PKSafe, ''), | |
lang = mod.in.data || prefs.getValue(PKLang, ''); | |
return { | |
g: Google +'search?ie=utf-8&q='+ encodeURIComponent(inp.text)+ | |
'&hl='+ lang +'&safe='+ safe, | |
term:term, lang:lang, safe:safe} | |
} | |
function tie(it, fn){ | |
it.addEventListener('focus', function(e){ | |
this.blur(); | |
e.preventDefault(); | |
e.stopPropagation(); | |
fn.call(it); | |
}, true); | |
} | |
function oops($e, msg){ | |
$e.html('<div class="error">Error: '+ msg +'</div>'); | |
} | |
function makeList(res, cur){ | |
var abcd, efgh, ijkl, ggl = [ | |
'<div class="count">', | |
cur.estimatedResultCount.link(cur.moreResultsUrl), '</div>', | |
paginates(cur.currentPageIndex + 1, cur.pages.length), '<table>']; | |
for(var i in res) with(res[i]) ggl.push( | |
'<tr><td><button class="rs" accesskey="', abcd = 'ABCD'[i], '">', | |
abcd, '</button></td><td><a class="title" href="', unescapedUrl, | |
'" accesskey="', efgh = 'EFGH'[i] ,'">', title, '</a><kbd>', efgh, | |
'</kbd><span class="meta"><a href="', cacheUrl, '" accesskey="', | |
ijkl = 'IJKL'[i], '">[cache]</a><kbd>', ijkl, '</kbd> ', visibleUrl, | |
'</span><div class="content">', content, '</div></td></tr>'); | |
return jQuery(ggl.join('')+'</table>'); | |
} | |
function paginates(cpi, len){ | |
for(var p = '', i = 0; ++i <= len;) p += button(i, i, cpi === i, i); | |
return ('<div class="pagination">'+ | |
button('<,', ',', cpi <= 1, cpi - 1)+ p + | |
button('.>', '.', cpi >= len, cpi + 1)+'</div>'); | |
} | |
function button(txt, key, dis, val) | |
'<button class="pg" accesskey="'+ key +'" value="'+ val + | |
(dis ? '" disabled="disabled">' : '">')+ txt +'</button>'; | |
const | |
{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[ | |
@td {vertical-align:top; font-size:88%} | |
@button {padding:0; border-width:1px; font-weight:bold} | |
@button[disabled]{background-color:transparent; color:inherit} | |
@kbd {text-decoration:underline; margin:0 3px 0 1px} | |
@button, @kbd {font:bold 92% "Consolas", monospace} | |
@.count {position:absolute; right:4px} | |
@.error {font-style:italic} | |
@.pagination {text-align:center} | |
@table, @.error {margin-bottom:4px} | |
@.logo {display:block} | |
]]></style><div id={ID.slice(1)} | |
/></div>.toXMLString().replace(/@/g, '.google '); | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment