Accessing manifest.json in a Google Chrome extension

I found quiet useful to have access to manifest.json information in my Chrome extension. For example when you want to migrate user data from an old extension version to a new one. I don't really know why it's not available in Chrome by default.

Fortunatelly, solution is very simple. Since manifest.json is just an ordinary JavaScript object we can load it using XHR and parse it with JSON.parse():

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
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:

 1 
 2 
 3 
 4 
 5 
// current version 
chrome.manifest.version

// default locale
chrome.manifest.default_locale

Alternative way

 1 
 2 
// current version 
chrome.app.getDetails().version

I don't know why it's not listed among other APIs but it works since Chrome 13 beta, although it seems like it's a little buggy.

blog comments powered by Disqus