Skip to content

Instantly share code, notes, and snippets.

@hzmangel
Forked from azuwis/x
Created November 17, 2008 09:16
Show Gist options
  • Save hzmangel/25703 to your computer and use it in GitHub Desktop.
Save hzmangel/25703 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
name: ["dictcn", "dcn"],
author: {
name: "Hu Ziming",
email: "[email protected]"
},
contributors: ["Hu Ziming", "azuwis"],
icon : "http://dict.cn/favicon.ico",
description: "Consulting the word from <a href=\"http://dict.cn\">http://dict.cn</a>.",
help: "Consulting the word from <a href=\"http://dict.cn\">http://dict.cn</a>.",
license: "MPL",
arguments: [{role: "object", nountype: noun_arb_text, label: "eng_word"}],
preview: function(pblock, args) {
var eng_word = args.object.text;
var url = "http://dict.cn/ws.php?q=";
url += eng_word;
var pre_template = "\
<style> \
#container {background-color: #D4EBF7; margin: 10pt; padding 5pt;} \
#word {background-color: #3399CC; color: #FFFFFF; font-family: arial black; font-size: x-large; padding: 3pt;} \
#explanation {color: #000000; font-size: medium; font-family: tahoma;padding:3px 0;} \
.orig {background-color: #FFFFFF; color: #000000; margin: 0;} \
.trans {background-color: #DB6F24; color: #FFFFFF; margin: 0;} \
</style> \
<div id=\"container\"> \
<div id=\"word\">${w.word} (${w.prons})</div> \
<div id=\"explanation\">${w.def}</div> \
{for foo in w.sents} \
<div id=\"sent\"> \
<div class=\"orig\">${foo.orig}</div> \
<div class=\"trans\">${foo.trans}</div> \
</div> \
{/for} \
</div>";
pblock.innerHTML = _("Getting results from <a href=\"http://dict.cn\">http://dict.cn</a>")
jQuery.get(url, function(data){
var rslt = jQuery(data).find("dict");
var sents = rslt.find("sent").map(function(){
return {
orig : jQuery("orig", this).text(),
trans : jQuery("trans", this).text()
}
});
var foobar = {
word : rslt.find("key").text(),
prons : rslt.find("pron").text(),
def : rslt.find("def").text(),
sents : sents
};
pblock.innerHTML = CmdUtils.renderTemplate(pre_template, {
w:foobar
});
}, "xml");
},
execute: function(args){
var eng_word = args.object.text;
var url = "http://dict.cn/";
url += eng_word;
Utils.openUrlInBrowser(url);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment