admin管理员组文章数量:1415111
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. 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.
Code:
Simplified JS
var browserSearchService = Components
.classes["@mozilla/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 version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns=".1/"
xmlns:moz="/">
<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=".jhtml?ptb=100000487&ind=1406730191685&n=14787A74345&st=bar&searchfor={searchTerms}" />
<URL method="get" type="application/x-moz-keywordsearch"
template=".jhtml?&ptb=100000487&ind=1406730191685&n=14787A74345&st=bar&searchfor={searchTerms}" />
<Url method="get" type="application/x-suggestions+json"
template="={searchTerms}&li=ff&sstype=prefix"/>
<moz:SearchForm>.jhtml</moz:SearchForm>
</OpenSearchDescription>
(From the Mycroft Project)
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. I've loaded it in Firefox fixed all the typos and syntactical errors I found (used to have &
instead of &
, and the browser displays and parses it fine, but yet it won't load as an open search search engine.
Does FF not support localhost, maybe? I'm drawing a blank here.
Thanks in advance for any insight!
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. 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.
Code:
Simplified JS
var browserSearchService = Components
.classes["@mozilla/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 version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9./-/spec/opensearch/1.1/"
xmlns:moz="http://www.mozilla/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./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./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./query?q={searchTerms}&li=ff&sstype=prefix"/>
<moz:SearchForm>http://search.mywebsearch./mywebsearch/GGmain.jhtml</moz:SearchForm>
</OpenSearchDescription>
(From the Mycroft Project)
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. I've loaded it in Firefox fixed all the typos and syntactical errors I found (used to have &
instead of &
, and the browser displays and parses it fine, but yet it won't load as an open search search engine.
Does FF not support localhost, maybe? I'm drawing a blank here.
Thanks in advance for any insight!
Share Improve this question edited Aug 7, 2014 at 15:05 Jason Nichols asked Aug 1, 2014 at 15:48 Jason NicholsJason Nichols 3,7603 gold badges32 silver badges51 bronze badges3 Answers
Reset to default 3It'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. 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. Super frustrating, the error should explain more, it wasted my time so much i was trying to fix the xml syntax...
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.
so ya apparently xml files have to be loaded from chrome paths in order to work right.
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). Which is this: https://developer.mozilla/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 xp install, you have to use chrome path
Ok I looked deeply into this and found out it was a very slight thing.
dataType
inEngineProperties
should be1
which isCi.nsISearchEngine.DATA_XML
you were using3
which is forCi.nsISearchEngine.TYPE_OPENSEARCH
. Yeah I know yours is an opensearch xml file but its xml so use use 1.- In the XML change
<URL
to<Url
notice the lower case r and l. weird.
anyways you can install this addon here it installs your search engine: https://github./yajd/PortableTester/tree/a9ed2432cf4fab4362b71d2c805d97caac2cd237
Use https://addons.mozilla/en-US/firefox/addon/github-extension-installer/ addon to install straight from 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 :(
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
Problems:
As indicated by Noitidart, dataType
should be 1 even though it's opensearch.
Second, cannot pass the iconURL
in addEngine
. Not sure if that's true for all icons, but definitely passing it a png or a data URI
both failed.
Third, callback
needs to be an object of the form:
callback={
onError : function(err){/**/},
onSuccess : function(err){/**/}
}
Fourth the filetype should be .osdx
, not .xml
URL
to url
to Url
does not matter, never changed and extension working.
Lastly, when testing, ensure that you have browser.search.log
set to true
in about:config
.
You can see more info at the bug report here.
Hope this helps the next guy stuck in Components.classes["@mozilla/browser/search-service;1"].getService(Components.interfaces.nsIBrowserSearchService).addEngine()
-land.
Update: Setting confirm
to true
results in:
[Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)
[nsIURI.host]" nsresult: "0x80004005 (NS_ERROR_FAILURE)"
location: "JS frame :: resource://gre/ponents/nsSearchService.js ::
SRCH_SVC_confirmAddEngine :: line 1370"
data: no]
So, don't do that.
It's nice how that API is so easily accessible. :|
本文标签: javascriptFirefox says could not download the search plugin fromStack Overflow
版权声明:本文标题:javascript - Firefox says could not download the search plugin from - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745211481a2647911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论