11

How do I programatically get my own Firefox extension's version number with Javascript?

My extension has an install.rdf file containing the version number similar to below. I want to extract the contents of the <em:version> tag.

<?xml version="1.0" encoding="UTF-8"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
 xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <Description about="urn:mozilla:install-manifest">
    ...
    <em:version>1.0</em:version>
    ...
  </Description>
</RDF>
Cheekysoft
  • 35,194
  • 20
  • 73
  • 86
Mat
  • 82,161
  • 34
  • 89
  • 109

4 Answers4

12

In Firefox 4 (Gecko 2) the API has changed, so if you need to port to Firefox 4, this is the code (from here):

try {
    // Firefox 4 and later; Mozilla 2 and later
    Components.utils.import("resource://gre/modules/AddonManager.jsm");
    AddonManager.getAddonByID("extension-guid@example.org", function(addon) {
        alert("My extension's version is " + addon.version);
  });
}
catch (ex) {
    // Firefox 3.6 and before; Mozilla 1.9.2 and before
    var em = Components.classes["@mozilla.org/extensions/manager;1"]
             .getService(Components.interfaces.nsIExtensionManager);
    var addon = em.getItemForID("extension-guid@example.org");
    alert("My extension's version is " + addon.version);
}
Ryan Li
  • 9,020
  • 7
  • 33
  • 62
  • Big change is that the Firefox 4 API is asynchronized, while Firefox 3 API is synchronized. If you need blocking, put dependent code in the callback. – Reci Apr 05 '11 at 01:53
  • @Crend King: that's true and an imperfect workaround is to save the version number somewhere inside the extension when the extension starts up and retrieve in synchronous manner when you need it. Asynchronous calls are really infectious. – Ryan Li Apr 05 '11 at 07:11
  • Version should work fine with your approach, since it is a property of the extension. However, when you try to get user-related information such as extension's installed location, you cannot bypass AddonManager ("ProfD/extensions/" is not always correct). – Reci Apr 06 '11 at 01:34
  • Alternative way to emulate synchronous call is to use [`nsIThread.processNextEvent()`](https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIThread#processNextEvent%28%29). [`nsIThreadManager.currentThread`](https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIThreadManager#Attributes) is the right thread. – Reci Apr 21 '11 at 22:56
  • Thank you, this helped me as well. SO rocks! – Ryan May 20 '11 at 16:51
8

I've not got the full answer, but I found the Extended extension and had a look at the source code as it seemed like a good starting point, and from Googling some of the methods in that I found this snippet on MDC. The key bit of code would seem to be this:

var gExtensionManager = Components.classes["@mozilla.org/extensions/manager;1"]
                        .getService(Components.interfaces.nsIExtensionManager);
var current = gExtensionManager.getItemForID("extension@guid.net").version;

You would have to replace extension@guid.net with the appropriate ID for your extension.

Firefox 4 requires different code, see the other answer.

Nickolay
  • 31,095
  • 13
  • 107
  • 185
robertc
  • 74,533
  • 18
  • 193
  • 177
  • Excellent. That is a full answer! I did have to initialise my own gExtensionManager object. – Mat Jul 06 '09 at 20:08
  • Well it wasn't a full answer because I wasn't actually sure it would work :) – robertc Jul 06 '09 at 22:18
  • 1
    I don't know of any advantage to doing it one way or the other but this is even shorter: `let version = Application.extensions.get('extension@id').version;` (from http://stackoverflow.com/questions/1965310/detecting-firefox-extension-version) – Tyler Oct 01 '10 at 23:28
  • @MatrixFrom: the FUEL API you mention has also changed in Firefox 4, because AddonManager API is now asynchronous. So no benefit to it, other than saving a few bytes in exchange for using a less-tested and less well-known API. – Nickolay Jan 09 '11 at 18:38
1

With Add-on SDK its simple as:

var version = require("sdk/self").version;

msangel
  • 9,895
  • 3
  • 50
  • 69
1

In Web Extensions use the following:

browser.runtime.getManifest().version
vladikoff
  • 1,496
  • 1
  • 14
  • 25