Open main menu

CDOT Wiki β

Localized Search in Firefox Search Box

Revision as of 20:53, 10 February 2008 by Sinker (talk | contribs) ('''Release v.02''')

Project Name

Localized Search in Firefox Search Box


Project Description

Firefox features a search box that can be customized to work with different search engines (e.g., Google, Yahoo!, Wikipedia, etc.). Most websites have their own search facilities, but the location of the search box on the page varies considerably. This project creates a mechanism for Firefox to pick up a web site's search mechanism and make it accessible to the user through the Firefox search box so that users have a consistent place to look for site-specific search.


Project Leader

Project Details

Release v.01

This release demonstrates my understanding of how some searchbar features currently work in the Firefox browser. I've uploaded a patch file that outputs a series of dump("DIAGNOSTIC MESSAGE: function name - what the source code is doing") statements to the terminal window while running a debug build of Firefox (i.e. Minefield). Specifically, what functions/methods are called and what is happening in the source code with respect to the searchbar menu when the following events occur:

  • The browser auto-detects a site that has a search plugin available.
  • The user selects the button to display the search engine list when a search plugin is available.
  • The user selects the option to "Add this search engine".
  • The user changes the current search engine by selecting it from the search bar drop-down list (menu).
  • The user moves the position of a search engine using the Search Engine Manager.
  • The user removes an auto-detected search engine from the list of "installed" search engines.


Downloading and applying the patch

1. Download the patch file to the root mozilla directory of your source tree and unzip the file there. I suggest renaming the file localsearchpatch_v01.txt.

  • NOTE: This patch requires that you use a DEBUG build of firefox. To see the output messages from the dump() statements in the patch, you have to enable it by setting the preference browser.dom.window.dump.enabled to true. You can set this preference in about:config or in a user.js file. This preference is not listed in about:config by default, so you may need to create it. Enter about:config in the browser's address bar -> right-click the empty content area -> New -> Boolean.
  • You can preview the patch here: Localized Search in Firefox Search Box: Release v.01

2. Apply the patch.

 $ cd mozilla
 $ patch -p0 < localsearchpatch_v01.txt
  • NOTE: Use the -p0 option to strip 0 leading directories from each filename in the patch because it is being applied in the same directory where the patch was created.


3. Build your source tree.

 $ cd mozilla
 $ make -f client.mk build


4. Run the browser to test the patch.

 $ cd objdir/dist/bin
 $ ./firefox --ProfileManager --no-remote


How to use the patch

1. Navigate to a web site that offers a search engine plugin:

Note the DIAGNOSTIC MESSAGE in the terminal window.


2. Click the searchbar's dropdown menu button.

  • Note the DIAGNOSTIC MESSAGE in the terminal window and the "Add this engine" option on the searchbar menu.


3. Click the "Add this engine" option on the searchbar menu.

  • Note the DIAGNOSTIC MESSAGE in the terminal window and the position of the newly added search engine on the searchbar menu.


4. Select "Manage Search Engines..." from the searchbar menu and select the newly installed engine from the list. Click the "Remove" button and then click "OK".

  • Note the DIAGNOSTIC MESSAGES in the terminal window.


5. Select "Manage Search Engines..." from the searchbar menu and select one of the installed engines on the list. Next, click the "Move Up" and "Move Down" buttons and then click "OK".

  • Note the DIAGNOSTIC MESSAGES in the terminal window.


6. Click the searchbar's dropdown menu button and select a different search engine as the current engine.

  • Note the DIAGNOSTIC MESSAGES in the terminal window.


How to remove the patch

  • You can back-out a patch by using the patch command with the -R or --reverse option. This swaps the new file with the old one to reverse the patch.
 $ cd mozilla
 $ patch -R -p0 < localsearchpatch_v01.txt


Release v.02

The target for this release was to dynamically "Add" a search engine plugin when Firefox loads a web page with an available search plugin, and then propagate the search engine to the top of the searchbar's menu as the current engine. The propagation of a newly added search engine as the current engine in the searchbar's menu was already a behavioral feature of Firefox 3 so there were no changes required for this part of my release target. Determining how to dynamically "Add" an available search engine plugin was challenging to achieve, but in the end, it only required a few modifications to the code in the browser.js file.


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
? patch.txt
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	10 Feb 2008 20:07:04 -0000
@@ -2771,38 +2771,30 @@ const BrowserSearch = {
     if (!this.searchBar)
       return;
     var browser = gBrowser.getBrowserForDocument(targetDoc);
     // Append the URI and an appropriate title to the browser data.
     var iconURL = null;
     if (gBrowser.shouldLoadFavIcon(browser.currentURI))
       iconURL = browser.currentURI.prePath + "/favicon.ico";
 
-    var hidden = false;
     // 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"].
                         getService(Ci.nsIBrowserSearchService);
-    if (searchService.getEngineByName(engine.title))
-      hidden = true;
 
-    var engines = (hidden ? browser.hiddenEngines : browser.engines) || [];
-
-    engines.push({ uri: engine.href,
-                   title: engine.title,
-                   icon: iconURL });
-
-    if (hidden)
+    if (!searchService.getEngineByName(engine.title)) {
+      searchService.addEngine(engine.href, Components.interfaces.nsISearchEngine.DATA_XML, iconURL, false);
+      var engines = browser.hiddenEngines || [];
+      engines.push({ uri: engine.href,
+                     title: engine.title,
+                     icon: iconURL });
       browser.hiddenEngines = engines;
-    else {
-      browser.engines = engines;
-      if (browser == gBrowser || browser == gBrowser.mCurrentBrowser)
-        this.updateSearchButton();
     }
   },
 
   /**
    * Update the browser UI to show whether or not additional engines are 
    * available when a page is loaded or the user switches tabs to a page that 
    * has search engines.
    */


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 using the nsIBrowserSearchService object to check if the new search plugin is NOT on the list. If it is not on the list, I "Add" the search plugin by calling the addEngine() function 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(). I then push the anonymous JavaScript engine object onto the the list of "hidden" search engines, the browser.hiddenEngines array. It is not necessary for me to add any of the auto-detected search plugins to the browser.engines array at this point. My objective is to dynamically "Add" the auto-detected search engines, therefore they are all considered to be "hidden" engines for now and they are not displayed on the searchbar as "Add <Search Engine>" menu items.


Release Target for v.03

  • When the user navigates away from the web page with the search engine plugin, dynamically remove the search plugin.


Release Target for v.04

  • TBA


Release Target for v.05

  • TBA


Release Target for v.06

  • TBA


Release Target for v.07

  • TBA


Release Target for v.08

  • TBA


Release Target for v.09

  • TBA


Release Target for v1.0

  • TBA


Resources

Project-Related Bugs

Project News

For up-to-date information about my project, visit my blog: Kerry's Open Source Blog

Release Target for v.03

  • When the user navigates away from the web page with the search engine plugin, dynamically remove the search plugin.


Release Target for v.04

  • TBA


Release Target for v.05

  • TBA


Release Target for v.06

  • TBA


Release Target for v.07

  • TBA


Release Target for v.08

  • TBA


Release Target for v.09

  • TBA


Release Target for v1.0

  • TBA


Resources

Project-Related Bugs

Project News

For up-to-date information about my project, visit my blog: Kerry's Open Source Blog