Code Samples

  • Revision slug: Addons/Add-on_Manager/Code_Samples
  • Revision title: Code Samples
  • Revision id: 89374
  • Created:
  • Creator: zafergurel
  • Is current revision? No
  • Comment correction of the code. addonLocation should be a path, not a nsIFile object.; one or more formatting changes

Revision Content

Getting the directory where your addon is located

If you need to determine the directory in which your add-on is installed, code like the following will do the trick. Simply replace YOUREXTENSIONID with your add-on's ID.

Components.utils.import("resource://gre/modules/AddonManager.jsm");  

AddonManager.getAddonByID("YOUREXTENSIONID", function(addon) {
  var addonLocation = addon.getResourceURI("").QueryInterface(Components.interfaces.nsIFileURL).file.path;
});

Accessing file and version information

Components.utils.import("resource://gre/modules/AddonManager.jsm");

AddonManager.getAddonByID("my-addon@foo.com", function(addon) {
  alert("My extension's version is " + addon.version);
  alert("Did I remember to include that file.txt file in my XPI? " +
        addon.hasResource("file.txt") ? "YES!" : "No");
  alert("Let's pretend I did, it's available from the URL " + addon.getResourceURI("file.txt").spec);
});

Uninstall an add-on

Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("youraddon@youraddon.com", function(addon) {
  addon.uninstall();
});

Listening for add-on uninstall

This examples sets a variable beingUninstalled that you can check when you get a profile-before-change message to do cleanup for your addon on uninstall.

var beingUninstalled;

let listener = {
  onUninstalling: function(addon) {
    if (addon.id == "youraddon@youraddon.com") {
      beingUninstalled = true;
    }
  },
  onOperationCancelled: function(addon) {
    if (addon.id == "youraddon@youraddon.com") {
      beingUninstalled = (addon.pendingOperations & AddonManager.PENDING_UNINSTALL) != 0;
    }
  }
}

try {
  Components.utils.import("resource://gre/modules/AddonManager.jsm");
  AddonManager.addAddonListener(listener);
} catch (ex) {}

Revision Source

<h3 id="Getting_the_directory_where_your_addon_is_located">Getting the directory where your addon is located</h3>
<p>If you need to determine the directory in which your add-on is installed, code like the following will do the trick. Simply replace <code>YOUREXTENSIONID</code> with your add-on's ID.</p>
<pre class="brush: js">Components.utils.import("resource://gre/modules/AddonManager.jsm");  

AddonManager.getAddonByID("YOUREXTENSIONID", function(addon) {
  var addonLocation = addon.getResourceURI("").QueryInterface(Components.interfaces.nsIFileURL).file.path;
});
</pre>
<h3 id="Accessing_file_and_version_information">Accessing file and version information</h3>
<pre class="brush: js">Components.utils.import("resource://gre/modules/AddonManager.jsm");

AddonManager.getAddonByID("my-addon@foo.com", function(addon) {
  alert("My extension's version is " + addon.version);
  alert("Did I remember to include that file.txt file in my XPI? " +
        addon.hasResource("file.txt") ? "YES!" : "No");
  alert("Let's pretend I did, it's available from the URL " + addon.<u><code><a href="../../../../en/Addons/Add-on_Manager/Addon#getResourceURI%28%29">getResourceURI</a></code></u>("file.txt").spec);
});
</pre>
<h3 id="Uninstall_an_addon" name="Uninstall_an_addon">Uninstall an add-on</h3>
<pre class="brush: js">Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("youraddon@youraddon.com", function(addon) {
  addon.uninstall();
});
</pre>
<h3 id="Listening_for_addon_uninstall" name="Listening_for_addon_uninstall">Listening for add-on uninstall</h3>
<p>This examples sets a variable <code>beingUninstalled</code> that you can check when you get a profile-before-change message to do cleanup for your addon on uninstall.</p>
<pre class="brush: js">var beingUninstalled;

let listener = {
  onUninstalling: function(addon) {
    if (addon.id == "youraddon@youraddon.com") {
      beingUninstalled = true;
    }
  },
  onOperationCancelled: function(addon) {
    if (addon.id == "youraddon@youraddon.com") {
      beingUninstalled = (addon.pendingOperations &amp; AddonManager.PENDING_UNINSTALL) != 0;
    }
  }
}

try {
  Components.utils.import("resource://gre/modules/AddonManager.jsm");
  AddonManager.addAddonListener(listener);
} catch (ex) {}
</pre>
Revert to this revision