JavaScript 使用 UserAgentData API 检测移动设备类型

发布于 2025-01-15 08:02:20 字数 528 浏览 3 评论 0原文

我正在使用 UserAgentData 浏览器 API 来检测用户是否通过移动设备访问了我的页面。在支持它的浏览器中,它非常简单并且运行良好:

const isMobile = window.navigator.userAgentData?.mobile

问题是,如果设备是移动设备,我还想检测它的类型,无论是手机还是平板电脑。我不想解析 navigator.userAgent 字符串(或使用执行相同操作的现有库)。我也不想查看设备屏幕宽度/高度和方向,因为据我所知,设备像素密度在这里可能会产生误导。因此,我使用来自 userAgentData API 的高熵数据来获取设备模型:

const detailedUserAgentData = await window.navigator.userAgentData?.getHighEntropyValues(['model']);

这里我有设备模型名称,但仍然不知道如何处理它 - 是否有任何公开可用的、开源的、定期更新的数据库我可以通过名称获取设备类型吗?

I'm using UserAgentData browser API to detect whether user has visited my page from mobile device or not. In browsers which supports it, it is quite simple and works well:

const isMobile = window.navigator.userAgentData?.mobile

Problem is that if device is mobile I want to also detect its type, whether it is phone or tablet. I do not want to parse navigator.userAgent string (or use existing libraries which do the same thing). I also do not want to look at device screen width/height and orientation, as as I understand device pixel density can be misleading here. So I'm using high entropy data from userAgentData API to obtain device model:

const detailedUserAgentData = await window.navigator.userAgentData?.getHighEntropyValues(['model']);

Here I have device model name, but still don't know what to do with it - is there any publicly available, open source, regularly updated data base from which I could obtain device type just by its name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文