Skip to content

Instantly share code, notes, and snippets.

/x.js
Created Feb 3, 2010

Embed
What would you like to do?
CmdUtils.CreateCommand({
name: ["dict", "fanyi", "ydcd", "youdao"],
author: {
name: "youdao",
email: "feedback@corp.youdao.com"
},
contributors: ["Youdaoer"],
icon : "http://shared.ydstatic.com/images/favicon.ico",
description: "使用<a href=\"http://dict.youdao.com\">有道海量词典</a>翻译",
help: "使用<a href=\"http://dict.youdao.com\">有道海量词典</a>翻译",
license: "MPL",
arguments: [{role: 'object', nountype: noun_arb_text, label: 'words'}],
preview: function(pblock, args) {
var eng_word = jQuery.trim(args.object.text);
var url = "http://dict.youdao.com/search?keyfrom=ubiquity&doctype=xml&q=";
url += encodeURIComponent(eng_word);
var fanyiurl = "http://fanyi.youdao.com/translate?keyfrom=ubiquity&doctype=xml&i=";
fanyiurl += encodeURIComponent(eng_word);
var pre_template = "\
<style> \
#container {margin: 0px 5px 5px 0px;} \
.nav {color: gray ;margin: 5px 0px 0px 0px;} \
li {list-style-type:decimal;margin: 0px 0px 5px 0px;} \
#word {margin: 5px 0px 0px 20px;} \
.basictrans {list-style-type:none;margin-left:-20px;} \
.fanyiresult {margin: 5px 5px 5px 0px;} \
.copyright {color: gray; font-size: 90%; margin: 10px 5px 5px 0px;} \
.sent .trans {margin: 2px 0px 0px 0px;} \
</style> \
{if hasDictResult} \
<div id=\"container\"> \
{if w.def && w.def.length > 0} \
<div class=\"nav\">基本翻译</div> \
<div id=\"word\"><b>${w.word}</b> {if w.prons && w.prons.length > 0}&nbsp;[${w.prons}]{/if}</div> \
<ul> \
{for foo in w.def} \
{if foo && foo.trans} \
<li class=\"basictrans\"> \
<span class=\"def\">${foo.trans}</span> \
</li> \
{/if} \
{/for} \
</ul> \
{/if} \
{if w.netdef && w.netdef.length > 0} \
<div class=\"nav\">网络释义</div> \
<ul> \
{for foo in w.netdef} \
{if foo && foo.orig} \
<li class=\"nettrans\"> \
<span class=\"orig\"><b>${foo.orig}:</b>&nbsp;</span> \
<span class=\"trans\">${foo.trans}</span> \
</li> \
{/if} \
{/for} \
</ul> \
{/if} \
{if w.sents && w.sents.length > 0} \
<div class=\"nav\">例句</div> \
<ul> \
{for foo in w.sents} \
{if foo && foo.orig} \
<li class=\"sent\"> \
<div class=\"orig\">${foo.orig}</div> \
<div class=\"trans\">${foo.trans}</div> \
</li> \
{/if} \
{/for} \
</ul> \
{/if} \
</div> \
<div class=\"copyright\">powered by 有道海量词典(http://dict.youdao.com)</div> \
{elseif hasFanyiResult} \
<div class=\"nav\">翻译结果:</div> \
<div class=\"fanyiresult\">${w}</div> \
<div class=\"copyright\">powered by 有道翻译(http://fanyi.youdao.com)</div> \
{else} \
没有搜索到结果 \
{/if} \
";
pblock.innerHTML = _("正在查询 <a href=\"http://dict.youdao.com\">有道海量词典</a> ...")
jQuery.get(url, function(data){
var rslt = jQuery(data).find("yodaodict");
var trans = rslt.find("custom-translation").find("translation").map(function(){
return {
trans: jQuery.trim(jQuery("content", this).text())
}
});
var sents = rslt.find("example-sentences").find("sentence-pair").map(function(){
return {
orig : jQuery.trim(jQuery("sentence", this).text()),
trans : jQuery.trim(jQuery("sentence-translation", this).text())
}
});
var nettrans = rslt.find("yodao-web-dict").find("web-translation").map(function(){
nettransitem = "";
jQuery(this).find("trans").each(function(){
if (nettransitem.length > 0) nettransitem += " | ";
nettransitem += jQuery.trim(jQuery("value", this).text());
});
return {
orig: jQuery.trim(jQuery("key", this).text()),
trans: nettransitem
}
});
var foobar = {
word : jQuery.trim(rslt.find("original-query").text()),
prons : jQuery.trim(rslt.find("phonetic-symbol").text()),
def : trans,
netdef: nettrans,
sents : sents
};
if ((trans && trans.length > 0) || (nettrans && nettrans.length > 0)) {
//has dict result
pblock.innerHTML = CmdUtils.renderTemplate(pre_template, {
hasDictResult : true,
w:foobar
});
} else {
//goto search fanyi.youdao.com
jQuery.get(fanyiurl, function(data){
var fanyi = jQuery.trim(jQuery(data).find("response").find("translation").text());
if (fanyi && fanyi.length > 0 && fanyi != eng_word) {
pblock.innerHTML = CmdUtils.renderTemplate(pre_template, {
hasDictResult : false,
hasFanyiResult: true,
w: fanyi
});
}
}, "xml");
}
}, "xml");
},
execute: function(args){
var eng_word = args.object.text;
var url = "http://dict.youdao.com/search?keyfrom=ubiquity&q=";
url += eng_word;
Utils.openUrlInBrowser(url);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.