views:

305

answers:

1

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#"&gt;
  <Description about="urn:mozilla:install-manifest">
    ...
    <em:version>1.0</em:version>
    ...
  </Description>
</RDF>
+4  A: 

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("[email protected]").version;

You would have to replace [email protected] with the appropriate ID for your extension. Extended doesn't seem to initialise the gExtensionManager object, so not sure if that's necessary.

robertc
Excellent. That is a full answer! I did have to initialise my own gExtensionManager object.
Mat
Well it wasn't a full answer because I wasn't actually sure it would work :)
robertc
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)
MatrixFrog