Skip to content

Instantly share code, notes, and snippets.

@satyr
Last active February 4, 2016 13:26
Show Gist options
  • Save satyr/8346 to your computer and use it in GitHub Desktop.
Save satyr/8346 to your computer and use it in GitHub Desktop.
英辞郞
const Name = 'eijiro', EOW = 'http://eow.alc.co.jp/',
Base = '<style>\
body {line-height:1.3}\
ul, ol {padding-left:1.2em}\
ul, ol, li {margin:0 0 0.2em}\
font {color:inherit}\
a img {border:none}\
.button {padding:0; border-width:1px; text-transform:uppercase}\
.button[disabled] {background-color:transparent; color:inherit}\
.button, #paging > a {font:bold 92% monospace}\
#paging > a {margin:0 0.3em}\
.searchwordfont {color:inherit; font-weight:bolder}\
.logo > img {vertical-align:bottom}\
.error {font-style:oblique; line-height:1.8}\
.loading {opacity:0.9}\
.loading + .logo {opacity:0.4}\
.error + .logo {opacity:0.7}\
#navi {text-align:right; margin:0 0 1ex 0; width:99%}\
#navi img {width:auto; height:4ex; margin:0 0.2em; vertical-align:top}\
#q {position:absolute; font:bold 128% sans-serif; top:0; left:0.3em}\
#resultsList .button, .sas .button {margin-right:0.2em}\
#resultsList > div[style], .tango, .itemPostion .smallredfont, #eijiro_img\
{display:none}\
</style>'+
'<div class='+ Name +'>'+
'<div id='+ Name +'></div>'+
'<a class=logo href='+ EOW +' accesskey="/">'+
'<img src="http://eowimg.alc.co.jp/content/img/logo_eow.gif"></a></div>',
A2BMap = {'\u25C4': [',', '<,'], '\u25BA': ['.', '.>']},
H = Utils.escapeHtml,
qurl = (x, i) => (x = x.trim()) &&
EOW + encodeURIComponent(x) + (i > 1 ? '?pg='+ i : '')
function ng(c){
c.textContent = Array.slice(arguments, 1).join(' ');
c.className = 'error';
}
function ok(cn, htm, go){
if(!/<div id="resultsArea"[^]+?<\/ul>/.test(htm))
return ng(cn, '??');
var $t = $(RegExp.lastMatch), q = $t.find('#searchWord').text();
var pg = $t.find('#paging').remove().children().map(function ej_navix(){
var {href} = this.querySelector('a') || this
, t = this.textContent.trim()
, k = t.slice(-1)
if(!href) return '<a href="'+ H(qurl(q, t)) +'" accesskey="'+ k +'">'+
'<b>'+ H(t) +'</b></a>'
var [key, lbl] = A2BMap[t] || [k, t], pg = /\d+/.exec(href);
return '<input type=button class=button'+
' accesskey="'+ key +'" page='+ pg +' value="'+ H(lbl) +'">'
}).get().join('')
var rl = $t.find('#resultsList').find('.refvocab').each(linx).end()[0];
cn.innerHTML = cn.className = '';
($(cn)
.append(
$('<div id="navi"><div id="q">'+ q +'</div></div>')
.append('<span id="paging">'+ pg +'</span>',
$t.find('#itemsNumber').contents()))
.append($t.find('.sas').find('a[href^="javascript:"]').each(idiomx).end())
.append(rl));
cn.querySelector('#paging').addEventListener('click', function ej_page(e){
let b = e2b(e)
b && go(b.getAttribute('page'))
}, false);
if(rl){
rl.removeAttribute('ondblclick')
rl.addEventListener('dblclick', wclix, false)
}
}
function e2b(e){
var b = e.target;
if(b.type !== 'button') return null;
e.preventDefault();
b.blur();
b.disabled = true;
return b;
}
function idiomx(){
var {href} = this
if(~href.indexOf('changeIdiom')){
this.parentNode.nextSibling.style.display = 'inline'
this.outerHTML = ''
} else
this.href = qurl(unescape((/"(.*?)"/.exec(href) || ',?')[1]))
}
function linx(){
var txt = this.textContent;
this.innerHTML = txt.link(qurl(txt));
}
function wclix(e){
var s = e.view.getSelection() +'';
if(!s) return;
var U = context.chromeWindow.gUbiquity;
U.__textBox.value = Name +' '+ s;
U.__processInput();
}
CmdUtils.CreateCommand({
names: [Name, 'eow'],
icon: EOW +'favicon.ico',
description:
'\u82F1\u8F9E\u90DE on the WEB'.link(EOW) +' + '+
'Incremental Search'.link(
'http://eowimg.alc.co.jp/content/help/tips/tip013.html'),
help: 'Double-clicking words may work.',
argument: {
name: 'eow Incremental Search',
label: '?',
rankLast: true,
suggest: function nt_eow_suggest(txt, htm, cb, sx){
var ss = [CmdUtils.makeSugg(txt, htm, null, .4, sx)];
sx || ss.push($.ajax({
url: 'http://eow.alc.co.jp/eow/sg/?q='+ encodeURIComponent(txt),
dataType: 'xml',
success: function nt_eow_success(xml){
var words = xml.getElementsByTagName('word');
if(1 in words) cb([
for (w of Array.slice(words, 1))
CmdUtils.makeSugg(decodeURIComponent(w.textContent), null, 0, .3)
])
}}));
return ss;
},
},
execute: function ej_execute(args){
Utils.openUrlInBrowser(qurl(args.object.text) || EOW);
},
preview: function ej_preview(pb, args, url, page){
var cn, doc = pb.ownerDocument;
while(!(cn = doc.getElementById(Name))) pb.innerHTML = Base;
var u = url || qurl(args.object.text, page);
if(!u) return void(cn.innerHTML = cn.className = '')
cn.className = 'loading';
CmdUtils.previewAjax(pb, {
url: u,
success: function ej_success(htm){
ok(cn, htm, function go(pg){ ej_preview(pb, args, '', pg) });
doc.defaultView.scrollTo(0, 0);
var query = '#resultsList a, .sas a', i = 10
for(let a of Array.slice(cn.querySelectorAll(query))){
a.href = EOW + a.pathname.slice(1) + a.search
let b = doc.createElement('input');
b.type = b.className = 'button';
b.value = b.accessKey = i.toString(36);
b.addEventListener('click', ej_inline, false);
a.parentNode.insertBefore(b, a);
if(++i > 35) break;
}
function ej_inline(e){ ej_preview(pb, args, e2b(e).nextSibling.href) }
},
error: function ej_error(x, s){
x.status && ng(cn, x.status, x.statusText);
},
});
},
author: 'satyr', license: 'MIT',
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment