Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

JavaScript listens to Enable/Disable event of an extension in Firefox4

5.00/5 (1 vote)
5 Jan 2011CPOL 14.2K  
JavaScript can listen to the enable/disable event from Firefox4 and can send to XPCOM components

Introduction


This article explains how a script will receive a notification whenever user will enable or disable an extension in Firefox 4.


Background


The background is in a previous version of Firefox displayed extensions in a separate window but in case of Firefox4, an Add-ons Manager Tab will display all attached extensions with enable/disable button.


JavaScript code


A developer first needs to add addEventListener for the main window and needs to add an addon listener which will listen to each event of the extensions. Refer to the below code:


var listener = {
    onDisabling: function(addon,needsRestart) {  
    if (addon.id == "{{9974FB49-725B-4e4b-AE33-3C0781F1F41D}}") {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
     .getService(Components.interfaces.nsIObserverService);
    observerService.notifyObservers(null, "user-notification-disable", "Ext is disabled");
	  }
  }  
}       
var listener = {
    onEnabling: function(addon,needsRestart) {  
    if (addon.id == "{{9974FB49-725B-4e4b-AE33-3C0781F1F41D}}") {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
     .getService(Components.interfaces.nsIObserverService);
    observerService.notifyObservers(null, "user-notification-disable", "Ext is Enabled");
	  }
  }  }
var Hide = { 
onLoad: function() { 
	try { 
	    Components.utils.import("resource://gre/modules/AddonManager.jsm"); 
	    AddonManager.addAddonListener(listener); 
       } 
	catch (ex) {} 
   } 
}; 
window.addEventListener("load", function(e) { Hide.onLoad(e); }, false);

Points of Interest


Script can send a notification to XPCOM component for further action on enable/disable event.

History


This is the first version of the article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)