Ubiquity是Mozilla Labs 推出的一项新服务插件。利用Ubiquity 你可以在网页上使用命令行的方式控制or插入各种你需要的网络服务(Mashups wiki 解释)。例如在编写文本时插入Google Maps 、在浏览网站时将好东西Twitter 给好友、直接选中某段文字即时翻译、或者调出选中关键词Wikipedia 。来源Mozilla Labs,这里是官方说明 Ubiquity 的基本理念便是尽可能的减少你打开新Tab 的数量和时间,让你在一个页面上操作各种网络服务。
下面是两个我自己写的Ubiquity的命令,一个是百度搜索(命令为:Baidu),一个是Flex Flex Community Help(命令为:Flex)搜索以及一个查询Dict.cn的命令(命令为:Dict)。将下面的代码复制到你的Ubiquity的自定义命令框中即可。
makeSearchCommand({
name: "Baidu",
url: "http://www.baidu.com/s?wd={QUERY}",
icon: "http://www.baidu.com/favicon.ico",
homepage: "http://blog.eshangrao.com/",
author: { name: "feiy", email: "eshangrao@gmail.com"},
contributors: ["feiy"],
license: "MPL",
description: "Searches Baidu for your words.",
preview: function(pblock, directObject) {
var searchTerm = directObject.text;
var pTemplate = "Searches Baidu for <b>${query}</b>";
var pData = {query: searchTerm};
pblock.innerHTML = CmdUtils.renderTemplate(pTemplate, pData);
var url = "http://www.baidu.com/s";
var params = { wd: searchTerm };
jQuery.ajax({
type: "get",
url: url,
data: params,
charset1: "gb2312",
dataType: "html",
error: function() {
displayMessage("Searchs Baidu Error");
},
success: function(data) {
var numToDisplay = 3;
var f_cache_p=data.indexOf("cache.baidu.com");
var f_table_p=data.substring(0,f_cache_p).lastIndexOf("<table");
var l_cache_p=data.lastIndexOf("cache.baidu.com");
var l_table_p=data.substr(l_cache_p).indexOf("</table>");
var result_html=data.substring(f_table_p,l_cache_p+l_table_p+"</table>".length);
var tables=result_html.split("</table>",numToDisplay);
var filter_html=tables.join("</table>");
//var decoder=Components.classes["@mozilla.org/intl/utf8converterservice;1"].getService(Components.interfaces.nsIUTF8ConverterService);
//pblock.innerHTML = decoder.convertStringToUTF8(filter_html,"GB2312",false);
pblock.innerHTML = filter_html
}
});
}
});
makeSearchCommand({
name: "Flex ",
url: "http://community.adobe.com/help/search.html?q={QUERY}&lbl=flex_product_adobelr&x=0&y=0",
homepage: "http://blog.eshangrao.com/",
author: { name: "feiy", email: "eshangrao@gmail.com"},
contributors: ["feiy"],
license: "MPL",
description: "Flex Community Help Searches for your words."
});
makeSearchCommand({
name: "Dict",
url: "http://dict.cn/search.php?q={QUERY}",
icon: "http://www.dict.cn/favicon.ico",
homepage: "http://blog.eshangrao.com/",
author: { name: "feiy", email: "eshangrao@gmail.com"},
contributors: ["feiy"],
license: "MPL",
description: "Dict.CN Dictionary"
});
其中Baidu搜索有预览功能,但是因为Baidu是gb2312的编码,会显示为乱码,现在没有一个很好的基于Javascript的编码转换功能,我试过用XPConnect调用xpcom的@mozilla.org/intl/utf8converterservice服务来转换编码(注释掉的两行代码),但也不成功,也许朋友们有更好的方法,欢迎回复!
Add By 20080909,应
7 Comments at "Baidu、Dict.cn和Flex Community Help搜索的Ubiquity命令"
百度那个有BUG,要对关键字encodeuricomponent后才行,不然搜中文乱码
另外能不能拜托写个dict.cn的查词命令^^
哦才看到后面的的文字,原来是编码问题才对。。。
makeSearchCommand({
name: “Dict”,
url: “http://dict.cn/search.php?q={QUERY}”,
description: “Dict.CN Dictionary”
});
下面是查询dict.cn的完成代码,以下代码已经加入到正文代码中:
makeSearchCommand({
name: “Dict”,
url: “http://dict.cn/search.php?q={QUERY}”,
icon: “http://www.dict.cn/favicon.ico”,
homepage: “http://blog.eshangrao.com/”,
author: { name: “feiy”, email: “eshangrao@gmail.com”},
contributors: ["feiy"],
license: “MPL”,
description: “Dict.CN Dictionary”
});
CmdUtils.CreateCommand({
name: “baidu”,
icon: “http://www.baidu.com/favicon.ico”,
takes: {”检索内容”: noun_arb_text},
preview: function(pblock,search_word){
CmdUtils.getWindowInsecure().showResutl = function(p)
{
pblock.innerHTML = “”;
for (i=0;i<5;i++)
{
pblock.innerHTML += ““+p.list[i].title+”“+p.list[i].comment+”"+p.list[i].uri+” “+p.list[i].sizekb+”K “+p.list[i].lastmod+” - “;
}
}
//var document = Application.activeWindow.activeTab.document;
var ele = document.createElement(”script”);
ele.type = “text/javascript”;
ele.src = “http://www.baidu.com/s?wd=%s&cl=3&tn=vista_pg&ie=utf-8&rn=50&s=”.replace(/%s/,search_word.text);
document.body.appendChild(ele);
},
})
这段代码可以取得中文,但是需要当前是command editor页面内。但跳出就不行了。
刚开始玩这个,加上你的命令,哈
[...] 《Baidu、Dict.cn和Flex Community Help搜索的Ubiquity命令》 [...]
Comment Now!