Firefox说无法下载搜索插件

Firefox says could not download the search plugin from

本文关键字:搜索 插件 下载 Firefox      更新时间:2023-09-26

背景故事:
我正在尝试动态地为Firefox生成一个OpenSearch搜索插件,该插件基于用户输入的值,作为一个更大的插件的一部分。我没有包括围绕它的表单和繁琐的东西,因为我已经把它缩小到一个简单的失败测试用例,试图导入任何XML。

Code:
简化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>

(From the Mycroft Project)

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

FF可能不支持localhost ?我想不起来了。

提前感谢任何见解!

这是安全问题。我之前被这个击中过。我所做的是从本地路径(或资源路径或我不记得的东西)加载xml文件重写和xbl,我会得到xml错误,就像它是语法错误,但没有什么。我想不明白。

最后,我创建了一个chrome.manifest文件,并给出了chrome://myaddon/content/myxml.xml这样的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路径删除本地路径的。

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

现在,如果你想在网络上托管这个东西并像这样安装它,那么你必须使用非特权安装方式(这意味着从html页面执行此javascript)。这是:https://developer.mozilla.org/en-US/docs/Adding_search_engines_from_web_pages

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

但是如果你想安装它,就像你在你的主题帖子,也就是xpcom安装,你必须使用chrome路径

好的,我深入调查了一下,发现这是一件非常轻微的事情。

    EngineProperties中的
  1. dataType应该是1,这是Ci.nsISearchEngine.DATA_XML,您使用的是3,这是Ci.nsISearchEngine.TYPE_OPENSEARCH。是的,我知道你的是一个opensearch xml文件,但它是xml,所以使用use 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/插件直接从repo安装。

最后,我不知道什么原因,但回调从来没有调用addEngine后,这是如此奇怪,我不知道如何得到它的工作:(

我也不确定如果安全错误,如果不是chrome路径是真的,不确定,但它可能是在其他情况下,但也许不是在这里。你应该可以使用localhost或者本地文件路径比如file:///C:/blah.xml

问题:
如Noitidart所示,即使是opensearch, dataType也应该是1。

第二,不能通过addEngine中的iconURL。我不确定这是否适用于所有图标,但肯定传递png或data URI都失败了。

第三,callback需要是如下形式的对象:

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

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

URL to url to Url不要紧,never change and extension working.

最后,在测试时,确保在about:config中将browser.search.log设置为true

你可以在这里的bug报告中看到更多信息。

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

更新:设置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是如此容易访问,真是太好了。: |