arantius
arantius

Reputation: 1801

How can a (Firefox) WebExtension know its own version?

I'm porting a legacy Firefox extension to WebExtensions. I want to know at run time the version number of the extension itself. Right now I'm doing:

let extensionVersion = (function() {
  var xhr = new XMLHttpRequest();
  xhr.overrideMimeType('application/json');
  xhr.open('GET', browser.extension.getURL('manifest.json'), false);
  xhr.send(null);
  var manifest = JSON.parse(xhr.responseText);
  return manifest.version;
})();

This dirty hack which relies on synchronous XHR. Is there a better way?

Upvotes: 5

Views: 295

Answers (1)

Daniel Herr
Daniel Herr

Reputation: 20508

There is a dedicated function for retrieving the manifest:

browser.runtime.getManifest().version

https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/getManifest

Upvotes: 7

Related Questions