How to close a sidebar in firefox

大憨熊 提交于 2019-12-07 22:56:44

问题


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


回答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);



回答2:


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)



来源:https://stackoverflow.com/questions/3356467/how-to-close-a-sidebar-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!