2

I have a sidebar inside my firefox addon. I want the following behavior for this sidebar - I should force close the sidebar if it is open when the browser is being closed (so that the next time the browser is opened the sidebar is not in an open state). I am trying to do this:

uninit: function() {
    var sidebarWindow = document.getElementById("sidebar").contentWindow;
    if (sidebarWindow.location.href == "chrome://myaddon/content/mysidebar.xul") {
        // Act on the sidebar content
        toggleSidebar('mySampleSidebar');
    }
}

I call this uninit for the window.unload event:

window.addEventListener("unload", function() { myobj.uninit()}, false);

Can someone tell me how to achieve this, as what I am trying to do is not working.

Thanks Kapil

Kapil
  • 572
  • 1
  • 5
  • 23

2 Answers2

1

In your firefox sidebar overlay javascript add

toggleSidebar();

in the "load" event listener function.

See here for example:

sidebar.onFirefoxLoad = function(event) {
  document.getElementById("contentAreaContextMenu")
          .addEventListener("popupshowing", function (e) 
             { sidebar.showFirefoxContextMenu(e); }, false);

           toggleSidebar();
};

window.addEventListener("load", sidebar.onFirefoxLoad, false);
Vinothkumar Arputharaj
  • 4,567
  • 4
  • 29
  • 36
  • Why are you adding it to load? I guess what you are doing is just closing it when the browser opens rather than when it closes? Note you are not checking the sidebar though, so it could be someone else's sidebar. – studgeek Aug 25 '11 at 17:44
  • After thinking about this for a bit, I think the idea of doing on load also is a good addition and a good concept to have, so I'm upvoting this. However, I do still think catching it on unload is the better default case and I definitely think you should be checking that its your sidebar like @Kapil is doing. – studgeek Aug 26 '11 at 00:22
0

Your code is correct for closing your sidebar, but I think unload is too late to change the startup state of the browser window (browser.xul), because browser.xul has already been unloaded (and its state, including sidebar state, has already been stored away).

Instead use beforeunload. I tested the following and it seems to work fine:
window.addEventListener("unload", myobj.uninit, false)

On rare occasions the browser process could be killed so unload would not be called (user kills it or it crashes). I'm not sure if occasionally stores the state of the sidebar like it does tabs, but if it does it could open and have the sidebar visible in that rare case. To handle that case, you can add what @Vinothkumar suggested.

window.addEventListener("load", myobj.uninit, false)

studgeek
  • 14,272
  • 6
  • 84
  • 96