开发者

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:

tog开发者_开发百科gleSidebar("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.


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.


toggleSidebar("mySidebar", false); doesn't work because the second param means "forceOpen" not an open/close flag.

As someone answered on Mozillazine this is because Firefox just preserves sidebar state across sessions. Are you sure you need to be different here?

Here's the code in Firefox that opens the sidebar on startup:

http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#999 (the lines may be off when you access this later - it's the link to the latest version of the source)

1000     let box = document.getElementById("sidebar-box");
1001     if (box.hasAttribute("sidebarcommand")) {

And here's where the sidebarcommand attribute gets persisted:

http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#1422

1373 function BrowserShutdown()
...
1426   if (!enumerator.hasMoreElements()) {
1427     document.persist("sidebar-box", "sidebarcommand");

I suggest trying this:
If current window is the last one and your sidebar is open,

  • either close the sidebar before BrowserShutdown (it's registered as an unload handler for the window)
  • trick the BrowserShutdown to persist the right attributes (corresponding to the "sidebar not opened" case)
  • or after BrowserShutdown overwrite the persisted values with the ones you need.

If you come up with the working solution, please share it with others, preferably on MDC: https://developer.mozilla.org/En/Code_snippets/Sidebar


See the answers at How to close a sidebar in firefox for how to close it at shutdown and, just in case, at shutdown.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜