How can I get the Firefox addon version in a content script? The global self object is available but it doesn't have a version number.
2 Answers
The addon version is available in the self module that is only accessible from addon scripts.
var self = require('sdk/self');
var version = self.version;
You can send the version to your content scripts via message passing on ports. The method for doing this depends on how the content scripts were added, as explained in the Content Scripts guide.
If you were using PageMod, you could do this:
main.js
var pageMods = require("sdk/page-mod");
var self = require("sdk/self");
var pageMod = pageMods.PageMod({
include: ['*'],
contentScriptFile: self.data.url("content-script.js"),
onAttach: function (worker) {
worker.port.emit('version', self.version);
}
});
content-script.js
self.port.on('version', function(version){
alert(version);
});
You can use the contentScriptOptions parameter when attaching workers (via page-mod, page-worker, tab attach method, etc.):
Pass the version to a content script via contentScriptOptions from an addon module (/lib/):
var worker=require("sdk/tabs").activeTab.attach({
contentScriptFile:...,
contentScriptOptions:{
version: require("sdk/self").version
}
});
Then obtain the version as a property of self.options in the content script (/data/):
var {version}=self.options;
To clarify: self in the content script is not the same as self via require("sdk/self") used in an addon module. The latter is where the addon version property is found, not the one in content script.