Brian
Brian

Reputation: 25823

How can I Detect when a Firefox WebPanel is closed?

I am interested in opening a webPanel on the right side of the Firefox window. Based on an MDN article, I determined that this could be done by setting the browser element's direction style. However, I wish to clear out this setting after the webPanel is closed. Is there a way I can detect this? Thus far, the only way I can think of is to poll sidebarWindow.location.href (to detect if the sidebar is changed) and sidebarHidden (to detect if the sidebar is closed).

var browser = document.getElementById('browser');
browser.style.direction = "rtl";

var sidebarWindow = document.getElementById("sidebar").contentWindow;
var sidebarBox = document.getElementById('sidebar-box');
var sidebarHidden = sidebarBox.collapsed || sidebarBox.hidden;

sidebarWindow.addEventListener("unload", function (event) {
    alert("1"); //This code fires when the web panel is opened
                //but not when it is closed.
});
sidebarBox.addEventListener("unload", function (event) {
    alert("2"); //This code does not fire.
});
sidebarWindow.addEventListener("close", function (event) {
    alert("3"); //This code does not fire.
});
sidebarBox.addEventListener("close", function (event) {
    alert("4"); //This code does not fire.
});  

openWebPanel('Test', 'http://www.google.com');

Upvotes: 1

Views: 256

Answers (1)

nmaier
nmaier

Reputation: 33162

IIRC there are essentially three ways a sidebar can be "closed":

  1. The user closes it using the GUI (X-box) or keyboard shortcut. In this case, the web panel will not necessarily get unloaded, so there is no unload event.
  2. Another document is loaded into the web panel. In this case you might get an unload.
  3. The user opens another panel. There is not necessarily an unload.

Should you go forward with your implementation, you need to make sure your code handles all three correctly.

  1. and 3. should be observable by the <broadcaster id="viewWebPanelsSidebar"> changing the checked attribute (see the implementation of toggleSidebar()), so you could have another element observing and acting on onbroadcast.

  2. should listen for unload and act accordingly.

To get proper unload events, I think the following should do the trick:

sidebar.contentDocument.getElementById("web-panels-browser")).
  addEventListener("unload", ...);

But my memory there is a bit wonky, so you might need to fiddle with that a bit. (The sizebar has a <xul:browser id="web-panels-browser"> which displays the actual content...)

After having said all that: I think it is a bad idea to mess with the sidebar like this. The MDN wiki(!) has bad advice in this case.

  • The sidebar was not designed to be messed with like this.
  • There are other add-ons "competing" with yours when it comes to messing with the sidebar.
  • The sidebar code is, for the most part, pretty archaic and under-maintained. Getting things like your requirement to work correctly is pretty hard. There still might be other code (in add-ons) that could dismiss the sidebar that you and I didn't think of.
  • The sidebar might not be the best place to display your content in the first place (what that content would be you didn't say). If it's something like context-help, dictionary/definition lookup results, login forms, then it won't be a good fit.
  • Some users might not like that their always-on bookmarks/history sidebar gets replaced by yours. You could handle this by re-opening the previous one, but that will only complicate matters further.

You might be better off using some other way to display information - e.g. a new tab, a panel, a new sidebar like the social sidebar... E.g the social sidebar is not only on the right, it actually is a standalone sidebar not part of the "main" sidebar.

Upvotes: 1

Related Questions