相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'd like to refer to the version number as defined in my manifest.json in my extension's JavaScript 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);
                +1 , make sure to test if its available first.  if(typeof ( chrome.runtime.getManifest ) == 'function'){
– fedmich
                Feb 18, 2013 at 9:18
                Because of Chrome automatic update feature that can't be disabled by an ordinary user I suppose currently nobody uses version below 22. But it is a good idea to include to the manifest: "minimum_chrome_version" : "22.0.0.0"
– Konstantin Smolyanin
                Feb 21, 2013 at 13:07

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
                Perfect, this worked.  The only thing I had to change was the name of the IIFE to manifest instead of chrome.manifest.
– bittersweetryan
                Sep 27, 2011 at 18:57
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章