背景故事:
我正在尝试动态生成 OpenSearch基于用户输入值的 Firefox 搜索插件,作为更大的附加组件的一部分。我没有包括它周围的表单和细节,因为我已经将它缩小到一个尝试导入任何 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&ind=1406730191685&n=14787A74345&st=bar&searchfor={searchTerms}" />
<URL method="get" type="application/x-moz-keywordsearch"
template="http://search.mywebsearch.com/mywebsearch/GGmain.jhtml?&ptb=100000487&ind=1406730191685&n=14787A74345&st=bar&searchfor={searchTerms}" />
<Url method="get" type="application/x-suggestions+json"
template="http://ssmsp.ask.com/query?q={searchTerms}&li=ff&sstype=prefix"/>
<moz:SearchForm>http://search.mywebsearch.com/mywebsearch/GGmain.jhtml</moz:SearchForm>
</OpenSearchDescription>
(来自 Mycroft Project)
据我所见,该错误应该指示无效的 XML 文件,但就我的生活而言,我找不到任何问题。我已经将它加载到 Firefox 中,修复了我发现的所有拼写错误和语法错误(以前使用 &
而不是 &
,浏览器可以很好地显示和解析它,但是但它不会作为开放式搜索引擎加载。
FF 不支持本地主机吗?我在这里画了一个空白。
提前感谢您的任何见解!
最佳答案
这是一个安全问题。我之前被这个打击过。我正在做的是从本地路径(或资源路径或我不记得的东西)加载一个用于覆盖和 xbl 的 xml 文件,我会得到 xml 错误,就像它是语法错误但什么也没有。我想不通。
最后,我创建了一个 chrome.manifest
文件,并提供了 xml 文件的路径,如 chrome://myaddon/content/myxml.xml
并且它起作用了。 super 令人沮丧,错误应该解释得更多,它浪费了我很多时间,我试图修复 xml 语法...
因此创建 list 而不是在此处更改代码:
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 install,你必须使用 chrome 路径
关于javascript - Firefox 说无法下载搜索插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25083992/