Localized Search in Firefox Search Box: Release v.03.1

From CDOT Wiki
Revision as of 20:46, 14 February 2008 by Sinker (talk | contribs)
Jump to: navigation, search

Main Project Page

Localized Search in Firefox Search Box

Release v.03.1

This release version (v.03.1) dynamically removes the search engine when the user navigates away from the site. If the user chooses to manually remove the search plugin from the list of installed engines, the search engine is then placed on the list of available engines as an "Add <Search Engine>" searchbar menu item. The user may now manually "Add" the available search engine as usual and it will not be dynamically removed from the list of installed engines on the searchbar list when the user navigates to another web site.


Patch File - localsearchpatch_v031.txt

? localsearchpatch_v031.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.962
diff -u -8 -p -r1.962 browser.js
--- browser/base/content/browser.js	13 Feb 2008 10:40:03 -0000	1.962
+++ browser/base/content/browser.js	14 Feb 2008 20:54:15 -0000
@@ -97,16 +97,19 @@ var gContextMenu = null;
 
 var gChromeState = null; // chrome state before we went into print preview
 
 var gSanitizeListener = null;
 
 var gAutoHideTabbarPrefListener = null;
 var gBookmarkAllTabsHandler = null;
 
+// Global variable that holds a 'dynamically added' nsISearchEngine object.
+var gAddedEngine = null;
+
 #ifdef XP_MACOSX
 var gClickAndHoldTimer = null;
 #endif
 
 #ifndef XP_MACOSX
 var gEditUIVisible = true;
 #endif
 
@@ -2780,16 +2783,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"].
                         getService(Ci.nsIBrowserSearchService);
     if (searchService.getEngineByName(engine.title))
       hidden = true;
+    else
+      // Dynamically "Add" the web site's search engine plugin.
+      gAddedEngine = 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)
@@ -3849,18 +3855,28 @@ nsBrowserStatusHandler.prototype =
     this.onProgressChange(gBrowser.webProgress, 0, 0, aTotalProgress, 1);
   },
 
   startDocumentLoad : function(aRequest)
   {
     // clear out feed data
     gBrowser.mCurrentBrowser.feeds = null;
 
+    var searchService = Cc["@mozilla.org/browser/search-service;1"].
+                        getService(Ci.nsIBrowserSearchService);
+    // Remove the dynamically added search engine if it is on the list.
+    try {
+	    if (gAddedEngine)
+        searchService.removeEngine(searchService.getEngineByName(gAddedEngine.name));
+        gAddedEngine = null;
+    } catch (e) {
+    }
+
     // clear out search-engine data
-    gBrowser.mCurrentBrowser.engines = null;    
+    gBrowser.mCurrentBrowser.engines = null;  
 
     const nsIChannel = Components.interfaces.nsIChannel;
     var urlStr = aRequest.QueryInterface(nsIChannel).URI.spec;
     var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                     .getService(Components.interfaces.nsIObserverService);
     try {
       observerService.notifyObservers(content, "StartDocumentLoad", urlStr);
     } catch (e) {
Index: browser/components/search/nsIBrowserSearchService.idl
===================================================================
RCS file: /cvsroot/mozilla/browser/components/search/nsIBrowserSearchService.idl,v
retrieving revision 1.20
diff -u -8 -p -r1.20 nsIBrowserSearchService.idl
--- browser/components/search/nsIBrowserSearchService.idl	11 Sep 2007 16:07:11 -0000	1.20
+++ browser/components/search/nsIBrowserSearchService.idl	14 Feb 2008 20:54:15 -0000
@@ -187,19 +187,21 @@ interface nsIBrowserSearchService : nsIS
    * @param confirm
    *        A boolean value indicating whether the user should be asked for
    *        confirmation before this engine is added to the list.  If this
    *        value is false, the engine will be added to the list upon successful
    *        load, but it will not be selected as the current engine.
    *
    * @throws NS_ERROR_FAILURE if the type is invalid, or if the description
    *         file cannot be successfully loaded.
+   *
+   * @returns the created engine.
    */
-  void addEngine(in AString engineURL, in long dataType, in AString iconURL,
-                 in boolean confirm);
+  nsISearchEngine addEngine(in AString engineURL, in long dataType, in AString iconURL,
+                 	    in boolean confirm);
 
   /**
    * Adds a new search engine, without asking the user for confirmation and
    * without starting to use it right away.
    *
    * @param name
    *        The search engine's name. Must be unique. Must not be null.
    *
Index: browser/components/search/nsSearchService.js
===================================================================
RCS file: /cvsroot/mozilla/browser/components/search/nsSearchService.js,v
retrieving revision 1.108
diff -u -8 -p -r1.108 nsSearchService.js
--- browser/components/search/nsSearchService.js	29 Jan 2008 19:39:20 -0000	1.108
+++ browser/components/search/nsSearchService.js	14 Feb 2008 20:54:17 -0000
@@ -2731,16 +2731,17 @@ SearchService.prototype = {
       var engine = new Engine(uri, aDataType, false);
       engine._initFromURI();
     } catch (ex) {
       LOG("addEngine: Error adding engine:\n" + ex);
       throw Cr.NS_ERROR_FAILURE;
     }
     engine._setIcon(aIconURL, false);
     engine._confirm = aConfirm;
+    return engine;
   },
 
   removeEngine: function SRCH_SVC_removeEngine(aEngine) {
     ENSURE_ARG(aEngine, "no engine passed to removeEngine!");
 
     var engineToRemove = null;
     for (var e in this._engines)
       if (aEngine.wrappedJSObject == this._engines[e])


Code Modifications

1. browser.js

  • Declare a global variable named gAddedEngine to hold a dynamically "Added" nsISearchEngine object and set its initial value to be null.
  • addEngine() function: If the auto-detected search plugin is NOT on the search service's list of engines, then dynamically "Add" the search engine and assign it to the gAddedEngine global variable.
  • startDocumentLoad() function: When the user navigates to a different web site, get an instance of nsIBrowserSearchService and assign it to the variable searchService. Use a try-catch block to first check that the global variable gAddedEngine has a non-null value. Then, use the search service's getEngineByName() function to "get" the engine if it is on the list and use the search service's removeEngine() function to remove it from the list. Set the global variable gAddedEngine to null.


2. nsIBrowserSearchService.idl


3. nsSearchService.js


How to Use the Patch

1. Copy and paste all of the text from the patch file into a text file and name the file localsearchpatch_v031.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.


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


4. Navigate to a different web site that offers a search engine plugin. Observe that the search engine that was dynamically "Added" at the previous web site is no longer on the list of installed engines but the search plugin for the current web site has been dynamically loaded and it appears as the current engine for searching the site.


5. Open the Search Engine Manager's dialog and "Remove" the dynamically "Added" search engine. Notice that it is no longer on the list of installed engines and it is now listed as an "Add <Search Engine>" menu item. If you choose to "Add" the engine back to the list of installed engines, it will remain as an installed engine when you navigate to a different web site.


6. Navigate to a web site that offers a search engine plugin and then to a different site that does NOT offer a search plugin. Observe that the dynamically "Added" search engine is no longer on the list of installed engines.