问题
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