5

I use

chrome.tabs.create({url:"URL here"})

to open a new tab in my LRG. To this URL I want to append the version number of the extension, which is specified in the manifest.json file:

"version": "1.2",

How can I access the version number in javascript at the time of creating the new tab?

Lazer
  • 90,700
  • 113
  • 281
  • 364

3 Answers3

7

Try in your extension:

chrome.app.getDetails().version

I don't know why it's not among other APIs but it works in my Chrome 13 beta. Rather test it in older versions of Chrome :).

EDIT: It's probably a little buggy

martin
  • 93,354
  • 25
  • 191
  • 226
4

You can fetch your own manifest and the version by using the following:

var url = chrome.extension.getURL("manifest.json");
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(e) {
  if(xhr.readyState == 2 && xhr.status == 200) {
     var manifest = JSON.parse(xhr.responseText);
     alert("Version: " + manifest.version);
  }
};

xhr.open("GET", url);
xhr.send();

Once you have the version number you can do your tab stuff that you need to do.

Shalom Craimer
  • 20,659
  • 8
  • 70
  • 106
Kinlan
  • 16,315
  • 5
  • 56
  • 88
  • I use this and it works well. Here's a slightly more complete post on this method: http://martinsikora.com/accessing-manifest-json-in-a-google-chrome-extension – Nicholas Dec 19 '12 at 04:34
  • I can't actually recall why I used 2... I know I used it in the past for progress events. – Kinlan Mar 24 '13 at 17:45
  • Hi, i can ask here i think, is it possible to update extension/app when i check if update is available? – Mirza Delic Apr 26 '15 at 09:42
0

I don't have enough rep to comment but in regard to Kinlan's XMLHttpRequest answer:

I found this very useful, but I did notice it should wait for readyState == 4 (not 2).

Worked great for me and seems like it should be robust.

Nicholas
  • 447
  • 1
  • 4
  • 18