Javascript 如何检测已安装的 Chrome 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4900436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect the installed Chrome version?
提问by Skizit
I'm developing a Chrome extension and I'm wondering is there a way that I can detect which version of Chrome the user is using?
我正在开发 Chrome 扩展程序,我想知道有没有一种方法可以检测用户使用的是哪个版本的 Chrome?
回答by serg
Get major version of Chrome as an integer:
以整数形式获取 Chrome 的主要版本:
function getChromeVersion () {
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return raw ? parseInt(raw[2], 10) : false;
}
I've updated the original answer, so that it does not throw an exception in other browsers, and does not use deprecated features.
我已经更新了原始答案,因此它不会在其他浏览器中引发异常,并且不会使用已弃用的功能。
You can also set minimum_chrome_version
in the manifest to not let users with older versions install it.
您还可以minimum_chrome_version
在清单中设置不允许旧版本的用户安装它。
回答by drmrbrewer
Here is a version, based on the answer from @serg, that extracts all of the elements of the version number:
这是一个基于@serg 的答案的版本,它提取了版本号的所有元素:
function getChromeVersion () {
var pieces = navigator.userAgent.match(/Chrom(?:e|ium)\/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/);
if (pieces == null || pieces.length != 5) {
return undefined;
}
pieces = pieces.map(piece => parseInt(piece, 10));
return {
major: pieces[1],
minor: pieces[2],
build: pieces[3],
patch: pieces[4]
};
}
The naming of the elements in the object that is returned is based on thisconvention, though you can of course adapt it to be based on thisinstead.