17

I know I can add the

minimum_chrome_version property to my manifest file to require at least this version of chrome

But what I am looking for is to allow installation of my extension on any chrome version but then from the background page or from my options page check the version of the chrome browser version and based on than either enable or disable some features of my extension that depend on certain minumim version.

Surprisingly I was not able to find a way to do this, even googling for this did not help.

Does anyone know how to check the version of client's chrome browser that my extension is running on?

bdukes
  • 152,002
  • 23
  • 148
  • 175
Dmitri
  • 34,780
  • 9
  • 39
  • 55

3 Answers3

21

You can extract get the current Chrome version from the user agent string:

var chromeVersion = /Chrome\/([0-9.]+)/.exec(navigator.userAgent)[1];

When using this to "detect" features, keep in mind the availability of some features vary between channels. In particular, many new features are already available on beta channels, but not on the stable channel, even though releases on both channels have the same version string. Instead of using the version to disable features, you can detect the presence of the APIs, e.g.:

if (chrome.declarativeWebRequest) {
    // Use declarativeWebRequest API...
} else {
    // fall back to webRequest API...
}
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 3
    This sounds like a hacky way to extract chrome version. I mean chrome api should provide a way to get the actual version, not the use reported by useragent. I'm not sure if useragent can even be spoofed by user. – Dmitri Oct 10 '13 at 12:30
  • @Dmitri The user agent can temporarily be spoofed via the developer tools. This requires the user to 1. Open the developer tools for the background page. 2. Click on the gears icon in the bottom-right corner. 3. Select a custom user agent in the Overrides tab. The user agent will be overridden until the developer tools are closed. Since these steps require a lot of attention and efforts, and it's not in the interest of a user to change this value, you can assume that the user agent is rock-solid when read from the background page of a Chrome extension. – Rob W Oct 10 '13 at 12:34
  • Per the Chrome Extensions team, this is the correct answer. – EricLaw Aug 31 '16 at 22:33
  • It will break with an out-of-range if the user has configured the user agent (e.g. using [user agent switcher](https://chrome.google.com/webstore/detail/user-agent-switcher/aedikcfpfonanffanecfolneiaoakmlc?hl=en#:~:text=Just%20right%20click%20on%20any,devices%20or%20search%20engine%20spiders. ). @ubuntugx 's answer below is better. – jsalvata Feb 10 '21 at 18:26
2

from fastClick

var chromeVersion=(/Chrome\/([0-9]+)/.exec(navigator.userAgent)||[,0])[1];
ubuntugx
  • 21
  • 5
0

That function checks if current browser User Agent is Chrome with major version of at least specified number or above version 115 if the number version is not specified.


export function isThisChromeVersionOrAbove(chromeVersionNumber = 115)
{
    const chromeRegex = /^Mozilla\/5\.0 \((.+[^\)]+)\) AppleWebKit\/[\d\.]+ \(KHTML, like Gecko\) C(hrome|riOS)\/(?<version>\d+)[\d\.]+[ Mobile]*[\/15E148]* Safari\/[\d\.]+$/;

    const regexMatchedGroups = chromeRegex.exec(navigator.userAgent);

    if (regexMatchedGroups)
    {
        const version= parseInt(regexMatchedGroups.groups.version, 10);

        if (version >= chromeVersionNumber)
        {
            // Chrome at specified version or above
            console.log(`general.js >> Version of Chrome is ${ version } which is above ${ chromeVersionNumber} `);
            return true;
        }

        else
        {
            return false;
            // Chrome below specified version
        }
    }
    else
    {
        return false;
        // Not Chrome
    }
}

Use it as:

if(isThisChromeVersionOrAbove(100))
{
 // do something if the version of Chrome is 100+
}

or like this

if(isThisChromeVersionOrAbove())
{
 // do something if the version of Chrome is 115+
}
konieckropka
  • 109
  • 1
  • 7