背景故事:
我试图动态生成一个 打开搜索 搜索插件为Firefox基于用户输入的值作为一个更大的附加组件的一部分。我没有包括围绕它的表单和cruft,因为我已经将其缩小到一个简单的失败测试用例,试图导入任何XML。

密码:
简化JS

var browserSearchService = Components
        .classes["@mozilla.org/browser/search-service;1"]
        .getService(Components.interfaces.nsIBrowserSearchService);

var EngineProperties = {
                xml :   'http://localhost/search.xml',
                dataType: 3,
                iconURL : 'http://localhost/logo.png',
                confirm : false,
                callback : function addEngineCallback(){
                    console.log('Jason is the greatest');           
                }
            }

browserSearchService.addEngine( EngineProperties.xml,
                                            EngineProperties.dataType,
                                            EngineProperties.iconURL,
                                            EngineProperties.confirm,
                                            EngineProperties.callback);

实际XML

<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
                       xmlns:moz="http://www.mozilla.org/2006/browser/search/">
  <ShortName>Jason</ShortName>
  <Description>Powered By Jason</Description>
  <InputEncoding>UTF-8</InputEncoding>
  <Image width="16" height="16" type="image/x-icon">http://localhost/logo.png</Image>

  <URL method="get" type="text/html" template="http://search.mywebsearch.com/mywebsearch/GGmain.jhtml?ptb=100000487&amp;ind=1406730191685&amp;n=14787A74345&amp;st=bar&amp;searchfor={searchTerms}" />
  <URL method="get" type="application/x-moz-keywordsearch" 
    template="http://search.mywebsearch.com/mywebsearch/GGmain.jhtml?&amp;ptb=100000487&amp;ind=1406730191685&amp;n=14787A74345&amp;st=bar&amp;searchfor={searchTerms}" />
  <Url method="get" type="application/x-suggestions+json" 
    template="http://ssmsp.ask.com/query?q={searchTerms}&amp;li=ff&amp;sstype=prefix"/>

  <moz:SearchForm>http://search.mywebsearch.com/mywebsearch/GGmain.jhtml</moz:SearchForm>
</OpenSearchDescription>

(从 Mycroft项目)

从我所看到的错误应该表明一个无效的XML文件,但对于我的生活,我找不到任何错误。我已经在Firefox中加载了它,修复了我发现的所有拼写错误和语法错误(以前有 & 而不是 &amp;, ,浏览器显示和解析它很好,但它不会加载为开放的搜索搜索引擎。

FF不支持localhost,也许?我在这里画空白.

提前感谢您的任何见解!

有帮助吗?

解决方案 3

问题:
如所示 诺蒂达特, dataType 应该是1,即使它是opensearch.

第二,不能通过 iconURLaddEngine.不确定这是否适用于所有图标,但肯定会传递一个png或一个 data URI 两者都失败了。

第三, callback 需要是表单的对象:

callback={
    onError   : function(err){/**/},
    onSuccess : function(err){/**/}
}

第四,文件类型应该是 .osdx, ,不 .xml

URLurlUrl 不要紧,从来没有改变和扩展工作。

最后,在测试时,确保您有 browser.search.log 设置为 trueabout:config.

您可以在错误报告中看到更多信息 这里.

希望这能帮助下一个被困住的人 Components.classes["@mozilla.org/browser/search-service;1"].getService(Components.interfaces.nsIBrowserSearchService).addEngine()-土地。

更新资料:设置 confirmtrue 结果:

[Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)
[nsIURI.host]"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"  
location: "JS frame :: resource://gre/components/nsSearchService.js :: 
SRCH_SVC_confirmAddEngine :: line 1370"  
data: no]

所以,不要那样做。

很高兴这个API如此容易访问。:|

其他提示

这是安全方面的问题。我以前被这个击中了。我正在做的是从本地路径(或资源路径或我不记得的东西)加载一个用于复盖和xbl的xml文件,我会得到xml错误,就像它是语法错误,但没有任何东西。我想不通。

最后我创建了一个 chrome.manifest 文件,并给出了xml文件的路径,如 chrome://myaddon/content/myxml.xml 而且成功了。超级令人沮丧,错误应该解释更多,它浪费了我的时间,我试图修复xml语法。..

所以创建这个清单,而不是在这里更改你的代码:

var EngineProperties = {
                xml :   'chrome://myaddon/content/search.xml',
                dataType: 3,
                iconURL : 'chrome://myaddon/content/logo.png',
                confirm : false,
                callback : function addEngineCallback(){
                    console.log('Jason is the greatest');           
                }
            }

请注意我如何使用chrome路径删除本地路径。

所以ya显然必须从chrome路径加载xml文件才能正常工作。

现在,如果你想在网上托管这个东西并这样安装它,那么你必须使用非私有的安装方式(这意味着从html页面中删除这个javascript)。这是哪个: https://developer.mozilla.org/en-US/docs/Adding_search_engines_from_web_pages

window.external.AddSearchProvider('http://localhost/search.xml');

但是,如果你想安装它,就像你在你的主题帖子中所做的那样,这是xpcom安装,你必须使用chrome path

好吧,我深入研究了这一点,发现这是一件非常轻微的事情。

  1. dataTypeEngineProperties 应该是 1 哪个是 Ci.nsISearchEngine.DATA_XML 你在用 3 哪个是为了 Ci.nsISearchEngine.TYPE_OPENSEARCH.是的,我知道你的是一个opensearch xml文件,但它的xml所以使用使用1。
  2. 在XML更改中 <URL<Url 注意小写r和l。奇怪。

无论如何,你可以在这里安装这个插件,它安装你的搜索引擎:https://github.com/yajd/PortableTester/tree/a9ed2432cf4fab4362b71d2c805d97caac2cd237

使用方法 https://addons.mozilla.org/en-US/firefox/addon/github-extension-installer/ 直接从回购安装插件.

最后,我也不知道是什么原因,但是回调从来没有在addEngine之后调用,这太奇怪了,我不知道如何让它工作:(

另外我不确定如果不是chrome路径的安全错误是否为真,不确定但可能在其他情况下,但可能不在这里。你应该能够做localhost或或本地文件路径,如 file:///C:/blah.xml

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top