67

Is there a way for a Chrome extension to read properties from manifest.json? I'd like to be able to read the version number and use it in the extension.

Kos
  • 4,890
  • 9
  • 38
  • 42
Ben McCann
  • 18,548
  • 25
  • 83
  • 101
  • What about doing a ajax request in towards `./manifest.json`. Did you try that ? Try doing it with jQuery, with GET or JSON or simple AJAX functions. Should let us know what you tried also. – Hamed Al-Khabaz Jan 04 '13 at 00:11
  • I believe reading version from manifest.json is not good idea, if you do not reload or refresh addon in extensions tab by just reading it will show you correct version but in fact previous version is still in action and newer was not loaded yet. – sairfan Nov 19 '20 at 15:20

5 Answers5

122

You can just use chrome.runtime.getManifest() to access the manifest data - you don't need to GET it and parse it.

var manifestData = chrome.runtime.getManifest();
console.log(manifestData.version);
console.log(manifestData.default_locale);
Marv
  • 748
  • 11
  • 27
nfm
  • 19,689
  • 15
  • 60
  • 90
16

The snipet chrome.app.getDetails() is not working anymore, is returning an error:

TypeError: Object # has no method 'getDetails'

You want to use chrome.runtime.getManifest() instead.

Protomen
  • 9,471
  • 9
  • 57
  • 124
Ondrej
  • 416
  • 4
  • 8
8

Since Chrome 22, you shoud use chrome.runtime

console.log(chrome.runtime.getManifest().version);
tardyp
  • 1,142
  • 11
  • 9
  • 2
    This is essentially Ondrej's answer. And a code snippet only makes sense for a complete js/html/css page, which an extension isn't. – Teepeemm Sep 09 '15 at 13:33
6

I use this way.

chrome.manifest = (function() {

    var manifestObject = false;
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            manifestObject = JSON.parse(xhr.responseText);
        }
    };
    xhr.open("GET", chrome.extension.getURL('/manifest.json'), false);

    try {
        xhr.send();
    } catch(e) {
        console.log('Couldn\'t load manifest.json');
    }

    return manifestObject;

})();

And that's all. This short code snippet loads manifest object and put's it among other chrome.* APIs. So, now you can get any information you want:

// current version 
chrome.manifest.version

// default locale
chrome.manifest.default_locale
Sudarshan
  • 18,140
  • 7
  • 53
  • 61
BenoitParis
  • 3,166
  • 4
  • 29
  • 56
  • 3
    Note that this method is still appropriate if you want to store arbitrary JSON config somewhere, as adding custom keys/values to your manifest.json will raise a warning. I have a separate data.json that stores things like the server's URL (which is different in dev/production), and I parse it with the above method. – nfm Jan 18 '13 at 00:27
  • @nfm I've tried this, but it seems to ignore the file, I get an error when trying to load it, do I need to reference it somewhere in the manifest.json? – Mojimi Jun 10 '19 at 17:44
0

It's actually simple.

function yourFunction() {
return chrome.app.getDetails().version;
}

where you can name yourFunction whatever you like.

Then to call it, just insert yourfunction() wherever you want to call the version number.

For example, if your version number is 9.07, then yourfunction() actually equals the numeric value of 9.07

user2651403
  • 478
  • 5
  • 17