Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 781
bittersweetryan Avatar asked Sep 27 '11 17:09

bittersweetryan


People also ask

What can you do with manifest JSON?

Using manifest. json , you specify basic metadata about your extension such as the name and version, and can also specify aspects of your extension's functionality (such as background scripts, content scripts, and browser actions).

How do I run a manifest JSON file?

First check if manifest. json is applied in the browser. For that open developer window by pressing shortcut F12. In Application tab, click Manifest option and see if it displays information that you have set.

What is manifest JSON in Chrome extension?

The manifest. json file contains information that defines the extension. The format of the information in the file is JSON. You can read more about what it contains in Google Chrome developer documentation: Manifest File Format. You will probably also want to read: Overview of Google Chrome Extensions.


2 Answers

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); 
like image 73
Konstantin Smolyanin Avatar answered Oct 11 '22 10:10

Konstantin Smolyanin


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 
like image 44
sanbor Avatar answered Oct 11 '22 10:10

sanbor