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,应Jarod要求,增加了查询Dict.cn的命令,命令名称Dict。