Ubiquity + dict.cn 查字典

关键字: ubiquity, dict.cn

前天装上了个ubiquity,觉得满好玩的,于是就想做一个查词的东西,因为它里面默认的东西用的是Gtrans,对于单个单词的释义感觉有时看着云里雾里的,于是就萌生了自己写一个命令的想法,然后就做了。

看了几分钟教程,就开始折腾了,先从一个HelloWorld开始,然后慢慢扩展。其实没啥难的,按着例子把想要的东西加上就是了。赞一下dict.cn,不止提供了地址栏直接查询,还加上了XML接口。一开始用的是普通的查询,结果页面返回的结果是GBK编码的,在ubiquity里面直接乱码,当时把我郁闷的不行了。还给它的客服扔了封信,问能不能提供XML或者JSON的接口,结果后来去它的论坛里面逛了一圈,属于直接提供了。一开始我用的还是一个简单的查询页面,返回结果还是GBK的编码,当时被郁闷弄傻了,直接从论坛上找到了admin的邮箱,扔了封邮件过去,然后等发完邮件回来一试XML接口的,好了,于是只好汗自己了,呵呵。

代码没啥东西,只是自己在里面加了一个template,所以看上去很不清爽,由于现在的代码只能扔在sandbox里面,模板啥的也不能发布,所以将就着用吧,0.1.1版本嘛。最后赞一下,开发还是满简单的,这个东西用了一天左右的时间吧。只是调试起来很不爽,大概是我还没找到方法吧,呵呵。点开看代码,嗯。

 1 CmdUtils.CreateCommand
2 ({
3     name: "dictcn",
4     author: { name: "Hu Ziming", email: "hzmangel@gmail.com"},
5     contributors: ["Hu Ziming"],
6     description: "Consulting the word from <a>http://dict.cn</a>.",
7     help: "Consulting the word from <a>http://dict.cn</a>.",
8     preview: "Consulting the word from <a>http://dict.cn</a>",
9     takes: {"eng_word": noun_arb_text},
10     license: "MPL",
11     preview: function( pblock, direct_obj)
12     {
13         var eng_word = direct_obj.text;
14         var url = "http://dict.cn/ws.php?q=";
15         url += eng_word;
16
17         var pre_template = "<style> #container {background-color: #73bFE6;} #word {font-family: arial black; color: maroon;font-size: x-large;} #explanation {color: midnightblue; font-size: medium; font-family: tahoma;padding:3px;} #sent {color: maroon;} .orig {background-color: #B6C5F2;padding: 2px; color: firebrick;} .trans {background-color: #2D4488; color: white; padding: 2px;} </style> <div id=\"container\"> <div id=\"word\">${w.word}</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> </div>{/for}";
18
19         jQuery.get(url, function(data)
20         {
21             var rslt = jQuery(data).find("dict");
22             var sent_len = rslt.find("sent").length;
23
24             var sents = new Array(sent_len);
25
26             //pblock.innerHTML = rslt.find("sent").slice(0).find("orig").text();
27
28             for (i = 0; i < sent_len; i++)
29             {
30                 sents[i] = {
31                     orig : rslt.find("sent").slice(i).find("orig").text(),
32                     trans : rslt.find("sent").slice(i).find("trans").text(),
33                 };
34             }
35
36
37             var foobar = {
38                 word  : rslt.find("key").text(),
39                 prons : rslt.find("pron").text(),
40                 def   : rslt.find("def").text(),
41                 sents : sents
42             };
43
44       
0;     pblock.innerHTML = CmdUtils.renderTemplate(pre_template, {w:foobar});
45
46         }, "xml");
47     },
48
49     execute: function( direct_obj )
50     {
51         var eng_word = direct_obj.text;
52         var url = "http://dict.cn/";
53         url += eng_word;
54
55         Utils.openUrlInBrowser( url );
56     }
57
58 });

This entry was posted in Happy coding. Bookmark the permalink.

One Response to Ubiquity + dict.cn 查字典

  1. azuwis says:

    非常感谢你的 ubiquity command,我发现一个 bug,处理 sents 的地方应该用 eq,而不是 slice。

    或者整个 sents 用下面得到:

    var sents = rslt.find(“sent”).map(function(){
    return {
    orig : jQuery(“orig”, this).text(),
    trans : jQuery(“trans”, this).text(),
    }});

    另外,希望你能发布这个 ubiquity command,谢谢。

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>