问题
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.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 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>
(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!
回答1:
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. 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.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
回答2:
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.com/yajd/PortableTester/tree/a9ed2432cf4fab4362b71d2c805d97caac2cd237
Use https://addons.mozilla.org/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
回答3:
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.org/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/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. :|
来源:https://stackoverflow.com/questions/25083992/firefox-says-could-not-download-the-search-plugin-from