I wanted to add a new search engine to be displayed in the drop down for search bar that appears in firefox nav-bar. And set this as the default searchEngine when user sets it through my extension's preferences.
For a non-restartless extension, through XUL, we use Components to do something like this:
Cc["@mozilla.org/browser/search-service;1"].getService(Ci.nsIBrowserSearchService).addEngineWithDetails(...); //Adds a new search engine
Cc["@mozilla.org/browser/search-service;1"].getService(Ci.nsIBrowserSearchService).currentEngine = ...; //sets the current search engine value.
How can I do something similar in a restartless extension created using Firefox addon-sdk? One problem I see is that there is no stable API to get and set firefox preferences listed at 'about:config'. But even if I use the unstable preferences service documented here, I am not able to do default search engine changes through extension. Help please!
Search in Firefox has two pieces you have to worry about.
First for the Search Input
You're actually going to use the same system for setting the search engine but you'll need to load the chrome module in the SDK.
var { Cc, Ci } = require("chrome");
var SearchService = Cc["@mozilla.org/browser/search-service;1"].getService(Ci.nsIBrowserSearchService);
// Add your engine to the list of engines in the drop down
SearchService.addEngineWithDetails('yoursearch', 'icon', 'yoursearchalias', 'your search description', 'get', 'http://stackoverflow.com/search?q={searchTerms}');
// Set your engine as the currentEngine so it's the default engine for the search input
SearchService.currentEngine = SearchService.getEngineByName('yoursearch');
Next for the URL bar
Update: As of Firefox 23 the
keyword.URL
preference has no effect, the above code will change the default behavior in both areas.
If you wanted to alter the search engine that's used for the URL bar you'll have to work with the user preferences.
var preferences = require('sdk/preferences/service');
// the 'search keyword' will be appended to the url you provide so strip out the
// {searchTerms} OpenSearch identifier
preferences.set('keyword.URL', 'http://stackoverflow.com/search?q=');
// When you want to set things back just reset the value
preferences.reset('keyword.URL');
Good luck!