Localized Search in Firefox Search Box: Release v.02

From CDOT Wiki
Jump to: navigation, search

Main Project Page

Localized Search in Firefox Search Box

Patch File

  • The modifications to the code in browser.js are shown below in a copy of the patch file for this release:
? localsearchpatch_v02.txt
? nohup.out
? objdir-ff-debug
Index: browser/base/content/browser.js
RCS file: /cvsroot/mozilla/browser/base/content/browser.js,v
retrieving revision 1.961
diff -u -8 -p -r1.961 browser.js
--- browser/base/content/browser.js	10 Feb 2008 06:57:05 -0000	1.961
+++ browser/base/content/browser.js	11 Feb 2008 19:57:26 -0000
@@ -2780,16 +2780,19 @@ const BrowserSearch = {
     // If this engine (identified by title) is already in the list, add it
     // to the list of hidden engines rather than to the main list.
     // XXX This will need to be changed when engines are identified by URL;
     // see bug 335102.
     var searchService = Cc["@mozilla.org/browser/search-service;1"].
     if (searchService.getEngineByName(engine.title))
       hidden = true;
+    else
+      // Dynamically "Add" the web site's search engine plugin.
+      searchService.addEngine(engine.href, Components.interfaces.nsISearchEngine.DATA_XML, iconURL, false);
     var engines = (hidden ? browser.hiddenEngines : browser.engines) || [];
     engines.push({ uri: engine.href,
                    title: engine.title,
                    icon: iconURL });
     if (hidden)

Code Modifications

Prior to my code modifications, the addEngine() function received a reference to an anonymous JavaScript engine object which was passed as a parameter to the function by the onLinkAdded() function that created it. Then, addEngine() used an nsIBrowserSearchService object to determine if the search engine was already on the list of engines. The addEngine() function used the title property of the engine object to "get" an nsISearchEngine object with a corresponding name property. If the search engine was on the list, then it was considered to be a "hidden" engine and it was pushed onto the browser.hiddenEngines array. This array is used by the search.xml file to determine how to populate and display the search engines on the searchbar menu. If the search engine was not on the list, it was pushed onto the "non-hidden", browser.engines array. The search engine would then be displayed as an "Add <Search Engine>" item on the searchbar menu and the searchbar button's background color would be changed to blue after a call to the updateSearchButton() function.

My code modifications alter the original addEngine() function by adding the new search plugin if is NOT on the list. The search plugin is dynamically "Added" by calling the addEngine() function defined in the nsSearchService.js file. This function completes the important step of creating a new nsISearchEngine object. It is this type of object that is required by important methods in search.xml, such as observe(),offerNewEngine() and hideNewEngine(). The remainder of the code follows the normal code paths.

How to Use the Patch (Screenshots)

1. Copy and paste all of the text from the patch file into a text file and name the file localsearchpatch_v02.txt. Save the file to your root mozilla directory. Then, apply the patch and recompile your mozilla source code as described in the release v.01 notes.

2. Start-up Firefox 3 (i.e. Minefield) and go to a web site that offers a search engine plugin.

FFLocalSearchv02 ss01.png

  • Notice that the search plugin has been dynamically loaded and added to the searchbar's list of "hidden" engines. The newly added search engine appears as the current engine and it can be used to search the web site.

FFLocalSearchv02 ss10.png

  • Notice also that the "Add <Search Engine>" menu item is not displayed and the searchbar's drop-down list button did not turn blue because the search plugin was dynamically loaded.

3. Open the "Manage Search Engines" dialog box and "Remove" the dynamically loaded search engine.

FFLocalSearchv02 ss11.png

  • It will now appear on the the searchbar's list as an "Add <Search Engine>" menu item. It can then be added back as normal to the searchbar's list.

FFLocalSearchv02 ss12.png

4. Clicking Firefox's reload button will reload the web page and trigger the code for the search plugin to be dynamically added to the list if it is not already on the searchbar's list of "hidden" engines.

FFLocalSearchv02 ss10.png

Return to top