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

1

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?

Carliecarlile answered 19/5, 2017 at 19:13 Comment(0)
B
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

Babarababassu answered 19/5, 2017 at 21:12 Comment(1)
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.Carliecarlile

© 2022 - 2024 — McMap. All rights reserved.