Monthly Archives: July 2010

Getting the version number of your own Chrome Extension

UPDATE As pointed out by commenter Andreas, you can now use a simpler way:
chrome.runtime.getManifest().version
The code below no longer is necessary but kept as reference.

Following on from yesterday’s post about getting the version number of your own firefox extension, what if you were now developing a Google Chrome extension and want the same thing? Google Chrome’s extension API is much more limited that Firefox’s. There’s no explicit extension-metadata-getting API that I know of. However, we do know that the version information is tucked away in manifest.json. With this knowledge and coupled with a few friendly APIs (XMLHttpRequest & JSON.parse) we can now have the equivalent function for chrome:

[js]
function getVersion(callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(‘GET’, ‘manifest.json’);
xmlhttp.onload = function (e) {
var manifest = JSON.parse(xmlhttp.responseText);
callback(manifest.version);
}
xmlhttp.send(null);
}

// to use
var version;
getVersion(function (ver) { version = ver; });
// version is populated after an indeterminate amount of time
[/js]

As XMLHttpRequest is asynchronous, our method needs a callback to receive the version information. You can also get whatever other information you want in your mainfest.json. So there you go.

Getting your Firefox extension version number

Sometimes you just want to show the version number (or any other meta data) of your own Firefox addon as specified in the install.rdf. You want to display this in your about page for example. In Firefox 2 and 3, you can to use this function:
[js]
function getVersion(addonID) {
var extMan = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
var ext = extMan.getItemForID(addonID);
ext.QueryInterface(Components.interfaces.nsIUpdateItem);
return ext.version;
}

// usage
var version = getVersion("{my addon id}");
[/js]

Inputting your addon ID, you will get your version number back. Inside the method, the variable ext (nsIUpdateItem) has a lot more metadata properties you can use as well if you so choose.

However, in the new Firefox version 4, the addon APIs have totally changed. The call to get the addon metadata has changed from synchronous to asynchronous which tends throws a spanner into the works. There seems to be a tendency to migrate all javascript data calls to asynchronous to avoid freezing the UI. Thus, rewriting getVersion means rewriting all the consumers to expect asynchronous operations. Here’s the new version that supports Firefox versions 2 to 4.
[js]
function getVersion(addonID, callback) {
var ascope = { };

if (typeof(Components.classes["@mozilla.org/extensions/manager;1"]) != ‘undefined’) {
var extMan = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
var ext = extMan.getItemForID(addonID);
ext.QueryInterface(Components.interfaces.nsIUpdateItem);
callback(ext.version);
return;
}

if (typeof(Components.utils) != ‘undefined’ && typeof(Components.utils.import) != ‘undefined’) {
Components.utils.import("resource://gre/modules/AddonManager.jsm", ascope);
}

ascope.AddonManager.getAddonByID(addonID, function (addon) { callback(addon.version); } );

}

// usage:
var version;
getVersion("{my addon id}", function(ver) { version = ver; });
// you don’t know when version will be populated

[/js]

Generally, in my use cases, I need to get the version and I don’t want to continue on until I have the version. Other than rewriting, the only workaround is to fetch the version number on extension start up and cache it somewhere. Thus all subsequent get version calls will get that cached number synchronously. Such a pain :/