Search code examples
javascriptfirefoxfirefox-addonsidebarfirefox-sidebar

How can I prevent Firefox showing my extension's sidebar on startup?


If my own sidebar is left open when Firefox is closed, it is shown again on startup. I find this undesirable and would rather it remain hidden until opened manually. Is it possible to stop this happening?

I tried adding this bit of code to my extension's initialisation function in order to close the sidebar if it does appear:

toggleSidebar("mySidebar", false);

This doesn't seem too work too consistently - it seems to ignore the false parameter and just toggles the sidebar! When it does work correctly it has unwanted side effects - I need to open and close the sidebar once before it will show any content. Weird, but I assume part of Firefox's view as to the sidebar's visibility has got out of sync.

It seems others are having the same trouble on the MozillaZine forums.


Solution

  • You could also add an observer to the shutdown process, and close the sidebar there. I had the same issue as you, and ended up going that route, since we already had an observer set up to do some other cleanup.

    The code looks like this, during the initialization of the main overlay:

        var current = this;
        var quitObserver = {
       QueryInterface: function(aIID) {
          if (aIID.equals(Components.interfaces.nsIObserver) || aIID.equals(Components.interfaces.nsISupports)) {
             return this;
          }
          throw Components.results.NS_NOINTERFACE;
       },
       observe: function(aSubject,aTopic,aData ) {
          var mainWindow = current._windowMediator.getMostRecentWindow("navigator:browser");
          var sidebar  = mainWindow.document.getElementById("sidebar");
          if (sidebar.contentWindow.location.href == "chrome://daisy/content/sidebar.xul") {
             mainWindow.toggleSidebar('openDaisySidebar', false);
          }
       }
    };
    
    setTimeout(function() {
       var mainWindow = current._windowMediator.getMostRecentWindow("navigator:browser");
       var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
       observerService.addObserver(quitObserver,"quit-application-granted",false);
    },2000);
    

    Hope that helps.