简体   繁体   English

Firefox说无法从中下载搜索插件

[英]Firefox says could not download the search plugin from

Backstory: 背景故事:
I'm trying to dynamically generate an OpenSearch search plugin for Firefox based on user entered values as part of a larger add-on. 我正在尝试根据用户输入的值动态生成Firefox的OpenSearch搜索插件,作为更大插件的一部分。 I'm not including the forms and cruft surrounding it, because I've narrowed it down to a simple failing test case trying to import any XML. 我不包括它周围的形式和残余,因为我已经将它缩小到一个简单的失败测试用例,试图导入任何XML。

Code: 码:
Simplified JS 简化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);

Actual XML 实际的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 ) (来自Mycroft项目

From what I've seen the error should indicate an invalid XML file, but for the life of me I can't find anything wrong with this. 从我所看到的错误应该表明一个无效的XML文件,但对于我的生活,我找不到任何错误。 I've loaded it in Firefox fixed all the typos and syntactical errors I found (used to have & instead of &amp; , and the browser displays and parses it fine, but yet it won't load as an open search search engine. 我已经在Firefox中加载它修复了我发现的所有拼写错误和语法错误(曾经有&而不是&amp; ;,浏览器显示并解析它很好,但它不会作为开放搜索搜索引擎加载。

Does FF not support localhost, maybe? FF不支持localhost吗? I'm drawing a blank here. 我在这里画一个空白。

Thanks in advance for any insight! 提前感谢任何见解!

It's a security thing. 这是安全的事情。 I was hit by this before. 之前我被这个击中了。 What I was doing was loading a xml file for overriding and xbl from a local path (or resource path or something I can't remember) and I would get xml error like it was syntax error but there was nothing. 我正在做的是从本地路径(或资源路径或我不记得的东西)加载xml文件以覆盖和xbl,我会得到xml错误,就像语法错误但没有任何东西。 I couldn't figure it out. 我无法理解。

Finally I created a chrome.manifest file and gave the path to the xml file like chrome://myaddon/content/myxml.xml and it worked. 最后,我创建了一个chrome.manifest文件并给出了像chrome://myaddon/content/myxml.xml这样的xml文件的路径,并且它有效。 Super frustrating, the error should explain more, it wasted my time so much i was trying to fix the xml syntax... 超级沮丧,错误应该解释更多,它浪费我的时间这么多我试图修复xml语法...

So create that manifest than change your code here: 所以创建清单而不是在这里更改代码:

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');           
                }
            }

notice how i removed the local path with the chrome path. 注意我是如何使用chrome路径删除本地路径的。

so ya apparently xml files have to be loaded from chrome paths in order to work right. 所以你显然必须从chrome路径加载xml文件才能正常工作。

Now if you would like to host this thing on the net and install it like that, then you have to use the non-privelaged way of installing (which means excecute this javascript from an html page). 现在,如果你想在网上托管这个东西并安装它,那么你必须使用非privelaged安装方式(这意味着从html页面优化这个javascript)。 Which is this: https://developer.mozilla.org/en-US/docs/Adding_search_engines_from_web_pages 这是: https//developer.mozilla.org/en-US/docs/Adding_search_engines_from_web_pages

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

but if you want to install it like you are doing in your topic post, which is xpcom install, you have to use chrome path 但是如果你想像你在主题帖子中那样安装它,那就是xpcom install,你必须使用chrome路径

Ok I looked deeply into this and found out it was a very slight thing. 好吧,我深入研究了这一点,发现这是一件非常轻微的事情。

  1. dataType in EngineProperties should be 1 which is Ci.nsISearchEngine.DATA_XML you were using 3 which is for Ci.nsISearchEngine.TYPE_OPENSEARCH . EngineProperties dataType应为1 ,即您使用的Ci.nsISearchEngine.DATA_XML 3 ,即Ci.nsISearchEngine.TYPE_OPENSEARCH Yeah I know yours is an opensearch xml file but its xml so use use 1. 是的我知道你的是一个opensearch xml文件,但是它的xml使用1。
  2. In the XML change <URL to <Url notice the lower case r and l. 在XML更改<URL<Url注意小写r和l。 weird. 奇怪的。

anyways you can install this addon here it installs your search engine: https://github.com/yajd/PortableTester/tree/a9ed2432cf4fab4362b71d2c805d97caac2cd237 无论如何你可以在这里安装这个插件安装你的搜索引擎: https//github.com/yajd/PortableTester/tree/a9ed2432cf4fab4362b71d2c805d97caac2cd237

Use https://addons.mozilla.org/en-US/firefox/addon/github-extension-installer/ addon to install straight from repo. 使用https://addons.mozilla.org/en-US/firefox/addon/github-extension-installer/ addon直接从repo安装。

LASTLY for I don't know what reason, but callback never calls after addEngine, it's so weird I have no idea how to get that to work :( 最后因为我不知道是什么原因,但回调从未在addEngine之后调用,它是如此奇怪我不知道如何让它工作:(

Also I'm not sure if the security error if not chrome path is true, not sure but it might be in other cases, but maybe not here. 此外,我不确定是否安全错误,如果不是铬路径是真的,不确定,但它可能在其他情况下,但也许不在这里。 You should be able to do localhost or or local file paths like file:///C:/blah.xml 您应该能够执行localhost或本地文件路径,例如file:///C:/blah.xml

Problems: 问题:
As indicated by Noitidart , dataType should be 1 even though it's opensearch . Noitidart所示即使opensearchdataType应为1

Second, cannot pass the iconURL in addEngine . 其次,无法在iconURL中传递addEngine Not sure if that's true for all icons, but definitely passing it a png or a data URI both failed. 不确定这是否适用于所有图标,但绝对传递它的png或data URI都失败了。

Third, callback needs to be an object of the form: 第三, callback需要是表单的对象:

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

Fourth the filetype should be .osdx , not .xml 第四,文件类型应该是.osdx ,而不是.xml

URL to url to Url does not matter, never changed and extension working. urlUrl URL无关紧要,永远不会更改并且扩展程序正常工作。

Lastly, when testing, ensure that you have browser.search.log set to true in about:config . 最后,在测试时,请确保在about:config中将browser.search.log设置为true

You can see more info at the bug report here . 您可以在此处查看错误报告中的更多信息。

Hope this helps the next guy stuck in Components.classes["@mozilla.org/browser/search-service;1"].getService(Components.interfaces.nsIBrowserSearchService).addEngine() -land. 希望这有助于下一个人陷入Components.classes["@mozilla.org/browser/search-service;1"].getService(Components.interfaces.nsIBrowserSearchService).addEngine()

Update: Setting confirm to true results in: 更新:将confirm设置为true导致:

[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]

So, don't do that. 所以,不要这样做。

It's nice how that API is so easily accessible. 这个API如此易于访问,这很好。 :| :|

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM