Skip to content

Instantly share code, notes, and snippets.

@satyr
Created March 14, 2009 01:46
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/78875 to your computer and use it in GitHub Desktop.
Save satyr/78875 to your computer and use it in GitHub Desktop.
simple dictionary commands
const
DY = 'http://dic.search.yahoo.co.jp/',
TR = 'http://thesaurus.reference.com/',
MW = 'http://www.merriam-webster.com/',
UD = 'http://www.urbandictionary.com/',
WK = 'http://en.wiktionary.org/',
CSS = '\
body {line-height:1.35}\
ul, ol {margin:0 0 0.5em; padding-left:2em}\
a img {border:none}\
kbd, input {font:bold 108% monospace; text-transform:uppercase}\
input {border-width:1px}\
kbd {text-decoration:underline}\
.error {font-style:oblique; line-height:1.8}\
.logo > img {vertical-align:bottom}\
.loading {opacity:0.9}\
.loading + .logo {opacity:0.4}\
.error + .logo {opacity:0.7}\
',
MWCSS = '\
#results_ad, #wordclickDiv > br:first-child, .pag-count-one\
, .britannica-entry, .franklin-promo {display:none}\
.sense_break, .syn, .rel, .v {display:block}\
.snum, .scnt {display:inline}\
h1 {font-size:128%; margin:0 0 0.2em}\
h2 {font-size:108%; margin:0.3em 0 0.1em}\
form {margin:0}\
dl, .defs {margin:0 0 0.5em}\
dd {margin-left:1em}\
p {margin:0.4em 0}\
input, kbd {margin:0 0.2em}\
input {padding:0; font-size:108%}\
li > a > sup {vertical-align:top !important}\
#mwEntryData input {font-size:92%}\
strong {margin-left:0.2em}\
strong + strong {margin-left:-0.1em}\
.v {margin-top:0.4em}\
.sense_label {padding-right:0.5em}\
#franklin_spelling_help, ol.results {\
list-style-type:none; padding-left:0.3em}\
.au {\
border:none; padding:0 4px;\
background:transparent url("http://mw4.m-w.com/images/audio.gif")\
no-repeat left center}\
.pr {font-family: "Times New Roman","Times Serif",serif}\
.unicode {font:90% "lucida sans unicode",sans-serif}\
',
H = Utils.escapeHtml
function I(x) x
function ng(c){
c.textContent = Array.slice(arguments, 1).join(' ');
c.className = 'error';
}
function wclix(e){
var s = e.view.getSelection() +'';
if(!s) return;
var U = context.chromeWindow.gUbiquity;
U.textBox.value = this.cmdName +' '+ s;
U.__processInput();
}
function pajax(cn, opts){
cn.className = 'loading';
return CmdUtils.previewAjax(cn.ownerDocument.body, opts);
}
function b4a(a, i, cb){
var b = a.ownerDocument.createElement('input');
b.type = 'button';
b.value = b.accessKey = (i + 10).toString(36);
b.addEventListener('click', function blick(ev){
ev.preventDefault();
this.blur();
this.disabled = true;
cb(this.nextSibling.href);
}, false);
a.parentNode.insertBefore(b, a);
}
function mw(type)({
name: 'merriam-webster '+ type,
desc: 'Merriam-Webstar '+ type[0].toUpperCase() + type.slice(1),
url : MW + type +'/',
icon: MW +'favicon.ico',
css : MWCSS,
logo: '<img src='+ MW +'images/hdr_mw_logo_area.gif>',
req : I,
load: mw_load,
tweak: mw_tweak,
});
function mw_load(htm)
/<div id="mwEntryData"[^]+?(?=<div id="fb-)/.test(htm)
? RegExp.lastMatch.replace(/\b(?:style|onmousemove)=/g, 'x=')
: /<div class="spelling-help"[^]+?(?=<div)/.exec(htm) || htm
function mw_tweak(cn, ojax){
$('.learners-link', cn).remove()
CmdUtils.absUrl(cn, ojax.url)
var doc = cn.ownerDocument, win = doc.defaultView
Array.forEach(cn.querySelectorAll('.au'), function(b, i){
let {parentNode} = b, kbd = doc.createElement('kbd')
, onc = b.getAttribute('onclick')
if(!onc) return
b.removeAttribute('onclick')
b.addEventListener('click', function(){
mw_au.apply(this,
onc.match(/'.+?'/g).map(function(s) escape(s.slice(1, -1))))
}, false)
b.accessKey = kbd.textContent = (i + 1) % 10
parentNode.insertBefore(kbd, b.nextSibling)
parentNode.insertBefore(parentNode.removeChild(b), kbd)
})
var artu = MW +'art/';
Array.slice(cn.getElementsByTagName('a'), 0, 26).forEach(function(a, i){
if(~a.href.lastIndexOf(artu, 0)) return mw_art(a);
b4a(a, i, mw_button);
a.removeAttribute('onclick');
});
function mw_button(u) rejax({url: u});
function rejax(opt) pajax(cn, $.extend(ojax, opt));
}
function mw_au(file, word){
var target = this
target.disabled = true
target.style.opacity = .2
$.ajax({
url: 'http://www.merriam-webster.com/audio.php?file='+ file +'&word='+ word,
success: function mw_au_success(htm){
audio(/\bhttp:\/\/[^/]+\/soundc11\/[^\"\'\s]+/.exec(htm), target)
},
});
}
function mw_input(ev){
ev.preventDefault();
this.blur();
if(this.type === 'submit'){
this.disabled = true;
this.onclick(ev);
}
}
function mw_art(a) $.get(a.href, function mw_art_get(htm){
if(!/<p>([^]+?)<\/p>/.test(htm)) return;
a.innerHTML = RegExp.$1;
CmdUtils.absUrl(a, MW +'art/dict/');
});
function audio(url, target){
var doc = target.ownerDocument, audio = new doc.defaultView.Audio(url);
target.style.opacity = .4;
audio.addEventListener('play', function(){
target.style.opacity = .7;
}, false);
audio.addEventListener('ended', function(){
target.style.opacity = 1;
target.disabled = false;
}, false);
audio.autoplay = true;
audio.load();
}
!function DictCommand({
name, url, css, logo, desc, req, load, nick, icon, tweak}){
const Name = name.replace(/\s/g, '-')
, Base = '<style>'+ CSS + css +'</style>'
+ '<div class='+ Name +'>'
+ '<div id='+ Name +'></div>'
+ '<a class=logo accesskey=/ href="'+ H(url) +'">'+ logo +'</a>'
+ '</div>'
function request(x) (x = x.trim()) && url + req(encodeURIComponent(x));
CmdUtils.CreateCommand({
name: name,
icon: icon || url +'favicon.ico',
argument: noun_arb_text,
description: '<a href="'+ H(url) +'">'+ H(desc) +'</a>',
execute: function dict_execute({object: {text}}){
Utils.openUrlInBrowser(request(text) || url);
},
preview: function dict_preview(pb, {object: {text}}){
var cn, q = request(text), me = this;
while(!(cn = pb.ownerDocument.getElementById(Name))) pb.innerHTML = Base;
if(!q) return;
cn.cmdName = this.name;
cn.addEventListener('dblclick', wclix, false);
pajax(cn, {
url: q,
success: function dict_success(htm){
var r = load(htm);
if(!r) return ng(cn, '??');
cn.innerHTML = r.join ? r.join('') : r;
cn.className = '';
cn.ownerDocument.defaultView.scrollTo(0, 0);
if(!tweak) return;
tweak(cn, {url: this.url, success: this.success, error: this.error});
},
error: function dict_error(x, s){
x.status && ng(cn, x.status, x.statusText, '('+ s +')')
},
})
},
})
return DictCommand
}(mw('dictionary'))(mw('thesaurus'))({
name: 'thesaurus',
desc: 'Thesaurus.com',
url : TR,
css : '\
table {border-collapse:collapse; margin:0 0 0.7em}\
td {padding:0 0.4em 0 0; line-height:1.2}\
h2 {font-size:1.2em}\
input[type=button] {margin:0 0.2em; padding:0}\
.browse {display:none}\
.logo > img {background-color:#f4a50a}\
',
logo: '<img height=43'+
' src=http://sp.ask.com/en/i/dictionary/thesaurus_logo.gif>',
req : function tsrs_req(x) 'browse/'+ x,
load: function tsrs_load(htm)
htm.match(/<table\b[^>]*?\bclass="the_content"[^]+?\/table>/g) ||
/<div id="sph"[^]+?(?=<div id="define")/.exec(htm) ||
/<div id="synonyms-[^]+?(?=<script)/.exec(htm),
tweak: function tsrs_tweak(cn, ojax){
var doc = cn.ownerDocument;
if(doc.getElementById('sph'))
Array.forEach(cn.getElementsByTagName('a'),
function(a, i){ b4a(a, i, tsrs_button) })
else {
let q = /[^/]+$/.exec(ojax.url)[0]
let hr = 'http://www.visualthesaurus.com/?lang=en&word=' + q
let sr = 'http://syndication.visualthesaurus.com/ddc/large/'+ q +'.gif'
let div = doc.createElement('div')
div.innerHTML =
'<a href="'+ H(hr) +'" accesskey=v><img src="'+ H(sr) +'">'
cn.appendChild(div)
}
function tsrs_button(u){
ojax.url = u
pajax(cn, ojax)
}
},
})({
name: 'yahoo dictionary',
desc: 'Yahoo!\u8f9e\u66f8',
url : DY,
css : '\
.title-jk-ex {display:none}\
h2 {font-size:108%; margin:0 0 0.8ex}\
h3 {font-size:100%; margin:0 0 0.4ex}\
ol {padding-bottom:0.4ex}\
h2 + p {float:right}\
.logo img {background-color:#fff}\
',
logo: '<img src="http://i.yimg.jp/images/dic/diclogo_3.gif">',
req : function dicy_req(x) 'search?ei=UTF-8&stype=partial&p='+ x,
load: function dicy_load(htm)
/<div id=\"DSm\d[^]+?(?=<div id=\"S)/.exec(htm),
})({
name: 'urban dictionary',
desc: 'Urban Dictionary',
url : UD,
css : '\
a[onclick], .zazzle_links, .index, .tools, td:first-child {display:none}\
.word {font:bold larger sans-serif}\
',
logo: '<img src="http://static3.urbandictionary.com/images/logo.gif">',
req : function ud_req(x) 'define.php?term='+ x,
load: function ud_load(htm){
var m =
/<table id='entries'[^]+?(?=<div class="pagination")/.exec(htm) ||
/<div id='not_defined_yet'>[^]+?<\/div>/.exec(htm)
return m && m[0].replace(/<\/?noscript>/g, '')
}
})({
name: 'wiktionary',
desc: 'Wiktionary',
url : WK,
css : '\
h2, h3, h4, p {margin:0.3ex 0}\
table {opacity:0.9}\
.noprint, th, td {color:#333}\
.noprint a, table a {color:#666}\
.wikitable tr > *, .audiotable tr > * {color:inherit}\
.editsection, .audiofile button {display:none}\
.audiofile > kbd {font:bold 16px monospace}\
',
logo: '<img src="'+ WK +'images/wiktionary-en.png">',
req : function wk_req(x) 'w/index.php?search='+ x,
load: function wk_load(htm){
var m =
/<h2><span[^]+?(?=<div class=[\"\']printfooter)/.exec(htm) ||
/<div class=.search(?:didy|resu)[^]+?(?=<div id=.searchmen)/.exec(htm)
return m ? CmdUtils.absUrl(m[0], WK) : htm
},
tweak: function wk_tweak(cn){
Array.forEach(cn.querySelectorAll('.audiofile button'), function(b, i){
var {parentNode} = b, kbd = this.createElement('kbd');
var input = cn.ownerDocument.createElement('input');
input.type = 'image';
input.src = 'http://en.wiktionary.org/w/extensions/OggHandler/play.png';
input.title = /\bhttp:[^\"]+/.exec(b.getAttribute('onclick'));
input.addEventListener('click', wk_onclick, false);
kbd.textContent = input.accessKey = (i+10).toString(36).toUpperCase();
parentNode.insertBefore(kbd, parentNode.firstChild);
parentNode.insertBefore(input, kbd);
}, cn.ownerDocument);
function wk_onclick(e){
e.preventDefault();
audio(e.target.title, e.target);
}
},
});
$.extend(feed, {author: 'satyr', license: 'MIT'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment