5

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?

arantius
  • 1,715
  • 1
  • 17
  • 28

1 Answers1

7

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

Daniel Herr
  • 19,083
  • 6
  • 44
  • 61
  • Thank you. I've got issues with the wiki/organization version of Mozilla's documentation. So not discoverable. I explicitly looked for a method to give me this and didn't find it. – arantius May 24 '17 at 15:26