Revision 89366 of Code Samples

  • Revision slug: Addons/Add-on_Manager/Code_Samples
  • Revision title: Code Samples
  • Revision id: 89366
  • Created:
  • Creator: Mh512
  • Is current revision? No
  • Comment 113 words added

Revision Content

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.getResourceURL("file.txt"));
});

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 = false;
    }
  }
}

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

 

Revision Source

<h3>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.getResourceURL("file.txt"));
});
</pre>
<h3 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 name="Listening_for_addon_uninstall">Listening for add-on uninstall</h3>
<p>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.</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 = false;
    }
  }
}

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