Question

Is there something in firefox addon through which we can register a callback which gets invoked when the addon is closed by clicking the x button on the left?

What I need is, when a user closes the addon bar using the x button, my extension loaded on that bar should be notified. Now what happens is, even though the user closes the addon bar, it is not getting closed; instead it just hides.

If we can be informed through a callback that the user has clicked on x button, then i could listen to that in the extension.

No correct solution

OTHER TIPS

Yes sir there absolutely is: MutationObserver.

Copy paste this to a scratchpad file in Browser envirnoment and then as addon bar is closed and opened you will see a message.

// select the target node
var win = Services.wm.getMostRecentWindow('navigator:browser');
var target = win.document.querySelector('#addon-bar');

// create an observer instance
var observer = new win.MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
      if (mutation.attributeName == 'collapsed') {
          Services.prompt.alert(null,'title','addon bar toggled it WAS = ' + mutation.oldValue);
      }
  });    
});

// configuration of the observer:
var config = { attributes:true, attributeOldValue:true };

// pass in the target node, as well as the observer options
observer.observe(target, config);

// later, you can stop observing
//observer.disconnect();

The easiest way to do this is to attach a command handler to the button in question. If your code runs inside the browser window, this will do:

 var closeButton = document.getElementById("addonbar-closebutton");
 closeButton.addEventListener("command", function(event) {
   // Add-on bar is being closed, do something
 }, false);

Note that this code is bound to stop working very soon as the add-on bar is being removed from Firefox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top