我正在开发一个 Chrome 扩展程序,我需要检测 Chrome 正在运行哪个操作系统,但我似乎找不到任何有关如何执行此操作的信息。请帮忙。 谢谢你。
最近添加,您可以使用Chrome自己的API中的getPlatformInfo方法:
chrome.runtime.getPlatformInfo(function(info) {
// Display host OS in the console
console.log(info.os);
});
您需要在
window.navigator.appVersion
中搜索操作系统名称和版本。
如果您只是想了解一个平台,请参阅此答案。您可以通过搜索其他用户代理来增强代码。
这里是 Chrome 用户代理字符串的详细列表(点击链接查看它们的含义)。
我想
navigator.platform
会更合适,但对我来说,我经常使用的基于navigator.platform
的以下脚本就足够了:
const ua = navigator.userAgent
const getBrowser = (): 'Opera' | 'Chrome' | 'Firefox' | 'Safari' | 'IE' | 'Edge' | 'Unknown' | undefined => {
if (ua.indexOf('Opera') > -1) {
return 'Opera'
}
if (ua.indexOf('Chrome') > -1) {
return 'Chrome'
}
if (ua.indexOf('Firefox') > -1) {
return 'Firefox'
}
if (ua.indexOf('Safari') > -1) {
return 'Safari'
}
if (ua.indexOf('MSIE') > -1) {
return 'IE'
}
if (ua.indexOf('Trident') > -1) {
return 'IE'
}
if (ua.indexOf('Edge' || 'Chrome') > -1) {
return 'Edge'
}
return 'Unknown'
}
const getOS = (): 'Windows' | 'Mac' | 'Linux' | 'Android' | 'iOS' | 'Unknown' => {
if (ua.indexOf('Windows') > -1) {
return 'Windows'
}
if (ua.indexOf('Mac') > -1) {
return 'Mac'
}
if (ua.indexOf('Linux') > -1) {
return 'Linux'
}
if (ua.indexOf('Android') > -1) {
return 'Android'
}
if (ua.indexOf('iPhone') > -1) {
return 'iOS'
}
if (ua.indexOf('iPad') > -1) {
return 'iOS'
}
if (ua.indexOf('iPod') > -1) {
return 'iOS'
}
return 'Unknown'
}
export { getBrowser, getOS }