Read the version from manifest.json [duplicate]

…衆ロ難τιáo~ 提交于 2020-06-08 06:42:50

问题


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.


回答1:


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);



回答2:


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.




回答3:


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



回答4:


Since Chrome 22, you shoud use chrome.runtime

console.log(chrome.runtime.getManifest().version);



回答5:


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



来源:https://stackoverflow.com/questions/14149209/read-the-version-from-manifest-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!