Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / Javascript
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
5 Jan 2011CPOL 13.8K   1   1
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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 nice going to user it Pin
Pranay Rana5-Jan-11 20:58
professionalPranay Rana5-Jan-11 20:58 
Reason for my vote of 5
nice going to user it

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.