如何根据支持的列表检查当前浏览器 [英] How to check current browser against the list of supported ones

查看:14
本文介绍了如何根据支持的列表检查当前浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用于测试 Web 应用程序的浏览器列表.如果用户的浏览器不受支持或经测试无法与我们的应用程序配合使用,我有一项任务要通知用户.

We have a list of browsers we test our webapp in. I have a task to notify the user if his browser isn't supported or tested to work well with our app.

我们在项目中有一个 browserslist 配置,我正在寻找一种方法来根据列表测试当前浏览器.我试过 browserlist-useragent 但我们不能用 webpack 编译它,因为它使用了 net, fs, tls 和我们不想包含在我们的包中的其他本地节点模块.

We have a browserslist configuration in the project and I'm looking for a way to test current browser against the list. I tried browserlist-useragent but we can't compile it with webpack due to the fact it uses net, fs, tls and other native node modules we don't want to include in our bundle.

有没有更好的方法可以避免在许多不同的地方复制受支持的浏览器列表,而只使用已经存在的浏览器列表配置来检测用户是否使用受支持的浏览器?

Is there any better way do avoid copying the list of supported browsers in many different places and just use browserlist configuration that already exists to detect if user uses supported one or not?

推荐答案

我会考虑使用这个实用程序"包 https://github.com/browserslist/browserslist-useragent-regexp使用 browserslist 的项目,然后使用此包建议的脚本生成的文件,然后在其他任何地方使用.实际上,您想要的只是生成文件中的正则表达式.这可以放置在浏览器检测所需的任何客户端或服务器代码中.我已经把我的放在了一些经典的 asp 中.

I'd consider using this "utility" package https://github.com/browserslist/browserslist-useragent-regexp in the project that uses browserslist, and then use the file generated by the script suggested by this package to then use anywhere else. In reality, all you want is the regex in the generated file. This can be placed in any client or server code you need for browser detection. I've placed mine in some classic asp.

另请注意,在 Windows PC 上,提供的说明不会生成包含 RegEx 的文件.相反,您需要运行 npx browserslist-useragent-regexp --allowHigherVersions 以在您的控制台上显示合适的 RegEx,然后在需要的地方添加.

Also note that on a Windows PC, the instructions provided don't result in a file containing a RegEx. Instead, you'd need to run npx browserslist-useragent-regexp --allowHigherVersions to display a suitable RegEx on your console, and then add that where needed.

这篇关于如何根据支持的列表检查当前浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆