有没有办法检测我网站的访问者是否正在运行 Chromium 而不是 Google Chrome?即使是基本的 UA 嗅探(我知道这是不好的做法)也足以满足我的特定情况,但似乎 Chromium 和 Chrome 共享相同的 UA 字符串 - 这是正确的吗?还有其他方法可以区分两者吗?
从 Chromium 84 开始,有一种名为 User-Agent Client Hints 的新方法 参考
您可以检查 userAgentData 属性是否存在并查找品牌数据。它将返回一个看起来像这样的数组。
[{
"brand": " Not;A Brand",
"version": "99"
}, {
"brand": "Google Chrome",
"version": "91"
}, {
"brand": "Chromium",
"version": "91"
}]
userAgentData.brands 将包含不同顺序的不同值,因此不要依赖于某个索引中出现的内容。相反,检查该属性是否存在于数组中。
if (navigator.userAgentData) {
let vendors = window.navigator.userAgentData.brands;
if (vendors.filter(e => e.brand === 'Google Chrome').length > 0) {
console.log('Chrome')
} else {
console.log('Chromium')
}
}
注:
这不再有效,因为现在所有基于 Chrome 的导航器都有所有插件。
新的 Chromium 版本也有 PDF 插件。
但他们也有 Chromium 插件,所以如果有任何插件以“Chromium”开头,那就是 Chromium:
function isChromium() {
for (var i = 0, u = "Chromium", l = u.length; i < navigator.plugins.length; i++) {
if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
return true;
}
return false;
}
此外,用它来识别 Microsoft Chredge(又名阿纳海姆)
function isEdg() {
for (var i = 0, u = "Microsoft Edg", l = u.length; i < navigator.plugins.length; i++) {
if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
return true;
}
return false;
}
Chrome 附带内置 PDF 阅读器,而 Chromium 则没有。
您可以使用 JavaScript 检测到这一点:
function isChrome() { // Actually, isWithChromePDFReader
for (var i=0; i<navigator.plugins.length; i++)
if (navigator.plugins[i].name == 'Chrome PDF Viewer') return true;
return false;
}
此方法并非 100% 可靠,因为用户可以将 PDF 阅读器二进制文件从 Chrome 复制到其 Chromium 目录,请参阅 Ask Ubuntu 上的此答案。
Chromium 和 Chrome 之间几乎没有区别(当然不是在渲染或 JavaScript 引擎上),那么为什么你想找出区别呢?
这是 Paul W. 答案的一个变体,适用于 Chromium 版本 42 及更高版本:
function isChromium() { // Actually, isWithChromiumPDFReader
for (var i=0; i<navigator.plugins.length; i++)
if (navigator.plugins[i].name == 'Chromium PDF Viewer') return true;
return false;
}
这当然仅在用户未禁用插件的情况下才有效。
这是另一种方法,使用
SpeechSynthesis
功能。
Google Chrome 浏览器提供 TTS 语音,而 Chromium 浏览器(包括 Brave)则不提供。可以使用
espeak
(在 Linux 上)手动安装语音,但是 Google 语音 均以 Google
开头,而手动安装的语音则不然。据我所知,Chrome 的声音是正当的,而不是免费的。
声音的集合是一个数组,其中每个声音如下所示:
{
voiceURI: "Google Deutsch",
name: "Google Deutsch",
lang: "de-DE",
localService: false,
default: true
}
我们只需要找到一个名字/URI 以 Google 开头的人...
function hasGoogleVoices() {
return window.speechSynthesis.getVoices()
.some(v => /^google/i.test(v.name));
}
(在 Linux 上针对 Chrome、Brave、Chromium 和 Firefox 进行了测试) 请有人检查一下 Safari 和 Windows。谢谢。
无法评论https://stackoverflow.com/a/68428992/14238203乔希回答。
在最新的 Chrome 和 Chromium(2021 年 10 月)上,某些解决方案对两者都返回 true,因此我必须找到不同的解决方案。
我采用了 https://stackoverflow.com/a/63724166/14238203 Fliptopbox 代码并实现了 Josh 答案。
const isChrome = navigator.userAgentData.brands.some((v) => /^google/i.test(v.brand));
乔什回答的问题是,如果您在加载页面时尝试此操作,则 getVoices() 返回空数组,直到加载所有语音(页面完成加载) 这里有一个承诺解决方案 - https://stackoverflow.com/a/59786665/14238203
对于我的用例来说,使用 getVoices() 有点麻烦,所以我使用了用户代理提示解决方案。
搜索后,这就是我能够使其工作的方法。您可以使用 navigator.userAgentData.brands[0].brand 来获取用户浏览器的品牌。由于并非所有浏览器(以及较旧的 Google Chrome 版本)都支持此对象,因此您可能需要将其包装在 try catch 中以防止出现未处理的异常错误。
if (userAgent.includes("Chrome")) {
try {
const userBrand = navigator.userAgentData.brands[0].brand;
if (userBrand === 'Google Chrome') {
// User is 100% using Google Chrome
} else {
// User is maybe using an older
// version of Google Chrome released in 2021 without support for the
//userAgentData or they're using a Chromium browser
}
} catch (error) {
// Since the navigator.userAgentData.brands doesn't exist
// We need to handle the error. If it doesn't exist, the user is not using
//Google Chrome or once again is using an older version of Google Chrome
}
}
我找到了一种不依赖已弃用的
navigator.plugins
API 的方法。
免责声明:当有人使用带有添加编解码器的自定义 Chromium 时,不能 100% 保证其正常工作。
免责声明2:最好还是使用基于UserAgent的方法。在我的特定用例中,我不能依赖用户代理。
const video = document.createElement('video') // Create a video element
const canPlayH264 = video.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"') // Verify h264 support
if (canPlayH264 === 'probably') {
console.log('Only Chrome can play h264')
} else {
console.log('Plain Chromium cannot play h264')
}