JavaScript 使用 UserAgentData API 检测移动设备类型
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论