legege / searchboxsync

Update the search bar automatically from wherever you search
4 stars 2 forks source link

Clear out the search box when tab switching #12

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
From an email:

I've made a local modification to SearchBox Sync that I think would be useful 
in the mainstream 
version.  I modified the code to _clear_ the search box when switching to a tab 
that does not 
have search information.  The current version you provide leaves the search box 
populated with 
information not relevant to the active tab.  This modification clears that 
information out.  It's a 
relatively minor change to the update method.  It can certainly be done much 
more cleanly than 
this but this was the proof of concept I wrote and it works well enough.  The 
new version of 
update is below:

 this.update = function(aUrl) {
   if (!this.searchbox) {
     return;
   }

   var empty = true;
   var rules = searchboxsync.RuleService.rules;
   if (rules != null) {
     for (var i = 0; i < rules.length; i++) {
       if (rules[i].disabled == true) {
         continue;
       }

       var regex = searchboxsync.RuleService.makeUrlRegex(rules[i]);
       if (regex == null) {
         continue;
       }

       var terms = searchboxsync.Util.extractTerms(regex, aUrl);
       if (terms && terms != "") {
         this.searchbar.removeAttribute("empty");
         this.searchbox.value = terms;

         var evt = document.createEvent("Events");
         evt.initEvent("oninput", true, true);
         this.searchbar.dispatchEvent(evt);

         empty = false;
         break;
       }
     }
   }
   if (empty == true)
   {
       if (this.searchbox.value != "")
       {
           this.searchbox.value = "";

           var evt = document.createEvent("Events");
           evt.initEvent("oninput", true, true);
           this.searchbar.dispatchEvent(evt);
       }
   }
 }

Original issue reported on code.google.com by legege on 31 Jul 2009 at 2:15