开发者

Can I access a property from my manifest.json in my extension's JavaScript files?

开发者 https://www.devze.com 2023-04-09 07:41 出处:网络
I\'d like to refer to the version number as defined in my manifest.json in my extension\'s JavaSc开发者_JAVA百科ript files.

I'd like to refer to the version number as defined in my manifest.json in my extension's JavaSc开发者_JAVA百科ript files.

Is there any way to do this?


Since chrome 22 you should use chrome.runtime.getManifest(). See docs here.

So now it is as simple as:

var manifest = chrome.runtime.getManifest();
console.log(manifest.name);
console.log(manifest.version);


I think that this is what you're looking for http://www.martinsikora.com/accessing-manifest-json-in-a-google-chrome-extension

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号