Rajashekhar Rangappa
Rajashekhar Rangappa

Reputation: 526

How can i get the firefox add-on version number?

My extension has package.json file containing install attribute. Is there any way to get the add-on version.

Upvotes: 1

Views: 261

Answers (3)

user2895783
user2895783

Reputation:

For a non bootstrap (classic XUL Overlay), it is easier than I thought.

Effectively, you 'interrupt' the existing load function (main_load) fired by the document load event by introducing a new load function with the async AddonManager.getAddonByID call. Wait for that to complete, before continuing to load your add-on using your previous function (just renamed, in this case to main_load_post, to indicate that we want to call this after the load).

In the main.js (corresponding to the main.xul file):

var myAddon = null;
window.addEventListener( 'load', main_load, false );

And elsewhere in same file (where ID is your addon ID):

// new main_load function to interrupt loading and assign to myAddon.
function main_load ( ) {
    AddonManager.getAddonByID( 'ID', function ( addon ) {
        myAddon = addon;
        main_load_post( );
    } );
}

// formerly 'main_load', renamed and called by the async callback.
function main_load_post ( ) {
    // main addon initialization: load prefs, setup pref observer, etc.
    // Note: your prefs.observe function will call something like main_load_prefs( ), etc
}

function main_load_prefs ( ) {
    // successful myAddon reference
    console.log( '***VERSION***: ' + myAddon.version );
}

// NOTE: Any myAddon reference must come from a call chain traced back to main_load_post( ).
// NOTE: You can't put myAddon references in global space outside function calls,
//       because the AddonManager callback may not have fired.
// console.log( '***VERSION***: ' + myAddon.version ); // This will always fail.

// NOTE: Nor can you call the function containing myAddon in a global context.
// main_load_prefs( );  // This will always fail.

Upvotes: 1

erosman
erosman

Reputation: 7721

AddonManager

You can get it from AddonManager module

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

AddonManager.getAddonByID("YOUREXTENSIONID", function(addon) {
  var version = addon.version;
});

More info:
AddonManager
Code Samples

Note: (from AddonManager)

The majority of the methods are asynchronous meaning that results are delivered through callbacks passed to the method. The callbacks will be called just once but that may be before or after the method returns.

So the result may not be available immediately. I run AddonManager.getAddonByID at start-up and use the data later on, when needed.

Addon-SDK

In addon SDK, you can get them from:

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

Bootstrapped Addon

As pointed out bellow by Noitidart, certain addon data is available via data.id, data.version, data.installPath, data.resourceURI and data.oldVersion in the Bootstrapped extensions

function startup(data, reason) { }
function shutdown(data, reason) { }
function install(data, reason) { }
function uninstall(data, reason) { }

Upvotes: 3

Noitidart
Noitidart

Reputation: 37228

If you are making a a bootstrap addon then the version is available via the startup function in the aData argument. What I do is I store the aData on startup into a global for future use.

var globalAData;
function startup(aReason, aData) {
    globalAData = aData;
    var version = aData.version;
}

From: https://gist.github.com/Noitidart/9025999#comment-1120821

This is the stuff that is contained in aData:

Key                 Value
id                  Bootstrap-Skeleton@jetpack
version             1.1
installPath         [xpconnect wrapped nsIFile]
resourceURI         [xpconnect wrapped nsIURI]
oldVersion          1.1

A note about the oldVersion key: Available only if there was a previously installed instance, could be same version like in this case. On reinstall and similar situations, the version and oldVersion are equal.

Upvotes: 1

Related Questions