Reputation: 36300
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?
Upvotes: 18
Views: 9731
Reputation: 349232
You can extract get the current Chrome version from the user agent string:
var chromeVersion = /Chrome\/([0-9.]+)/.exec(navigator.userAgent)[1];
Note that the Chrome version has been redacted since Chrome 105 for privacy reasons, so you only get the major version here.
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...
}
Upvotes: 21
Reputation: 538
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+
}
Upvotes: 0