

GitHub - browserslist/browserslist-useragent-regexp: A utility to compile browse...
source link: https://github.com/browserslist/browserslist-useragent-regexp
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

browserslist-useragent-regexp
A utility to compile browserslist query to a regex to test browser useragent. Simplest example: you can detect supported browsers on client-side.
- Create
.browserslistrc
config, for example like this:
last 2 versions
not dead
- Add script to
package.json
:
{
"scripts": {
"supportedBrowsers": "echo \"export default $(browserslist-useragent-regexp --allowHigherVersions);\" > supportedBrowsers.js"
}
}
for Windows users
{
: {
:
}
}
- Run this script, to compile regex:
pnpm supportedBrowsers
# or
npm run supportedBrowsers
# or
yarn supportedBrowsers
supportedBrowsers.js
:
export default /Edge?\/(10[5-9]|1[1-9]\d|[2-9]\d\d|\d{4,})(\.\d+|)(\.\d+|)|Firefox\/(10[4-9]|1[1-9]\d|[2-9]\d\d|\d{4,})\.\d+(\.\d+|)|Chrom(ium|e)\/(10[5-9]|1[1-9]\d|[2-9]\d\d|\d{4,})\.\d+(\.\d+|)|Maci.* Version\/(15\.([6-9]|\d{2,})|(1[6-9]|[2-9]\d|\d{3,})\.\d+)([,.]\d+|)( Mobile\/\w+|) Safari\/|Chrome.+OPR\/(9\d|\d{3,})\.\d+\.\d+|(CPU[ +]OS|iPhone[ +]OS|CPU[ +]iPhone|CPU IPhone OS|CPU iPad OS)[ +]+(15[._]([6-9]|\d{2,})|(1[6-9]|[2-9]\d|\d{3,})[._]\d+)([._]\d+|)|Opera Mini|Android:?[ /\-](10[6-9]|1[1-9]\d|[2-9]\d{2}|\d{4,})(\.\d+|)(\.\d+|)|Mobile Safari.+OPR\/(6[4-9]|[7-9]\d|\d{3,})\.\d+\.\d+|Android.+Firefox\/(10[5-9]|1[1-9]\d|[2-9]\d\d|\d{4,})\.\d+(\.\d+|)|Android.+Chrom(ium|e)\/(10[6-9]|1[1-9]\d|[2-9]\d\d|\d{4,})\.\d+(\.\d+|)|Android.+(UC? ?Browser|UCWEB|U3)[ /]?(13\.([4-9]|\d{2,})|(1[4-9]|[2-9]\d|\d{3,})\.\d+)\.\d+|SamsungBrowser\/(1[7-9]|[2-9]\d|\d{3,})\.\d+|Android.+MQQBrowser\/(13(\.([1-9]|\d{2,})|)|(1[4-9]|[2-9]\d|\d{3,})(\.\d+|))(\.\d+|)|K[Aa][Ii]OS\/(2\.([5-9]|\d{2,})|([3-9]|\d{2,})\.\d+)(\.\d+|)/;
- Import regex from created file:
import supportedBrowsers from './supportedBrowsers.js';
if (supportedBrowsers.test(navigator.userAgent)) {
alert('Your browser is supported.');
}
Install
pnpm add -D browserslist-useragent-regexp
# or
npm i -D browserslist-useragent-regexp
# or
yarn add -D browserslist-useragent-regexp
As was written in article "Smart Bundling: Shipping legacy code to only legacy browsers": you can determinate, which bundle you should give to browser from server with browserslist-useragent
. But in this case you must have your own server with special logic. Now, with browserslist-useragent-regexp
, you can move that to client-side.
Development was inspired by this proposal from Mathias Bynens.
How to make differential resource loading and other optimizations with browserslist-useragent-regexp
you can read in article "Speed up with Browserslist".
Demo (sources, build script).
Also, testing useragents using generated regex is faster than using the matchesUA
method from browserslist-useragent.
pnpm browserslist-useragent-regexp [query] [...options]
# or
npx browserslist-useragent-regexp [query] [...options]
# or
yarn exec -- browserslist-useragent-regexp [query] [...options]
Also, short alias is available:
pnpm bluare [query] [...options]
Option | Description | Default |
---|---|---|
query | Manually provide a browserslist query. Specifying this overrides the browserslist configuration specified in your project. | |
‑‑help, -h | Print this message. | |
‑‑verbose, -v | Print additional info about regexes. | |
‑‑ignorePatch | Ignore differences in patch browser numbers. | true |
‑‑ignoreMinor | Ignore differences in minor browser versions. | false |
‑‑allowHigherVersions | For all the browsers in the browserslist query, return a match if the useragent version is equal to or higher than the one specified in browserslist. | false |
‑‑allowZeroSubversions | Ignore match of patch or patch and minor, if they are 0. | false |
JS API basics
Module exposes two main methods:
getUserAgentRegexes(options)
Compile browserslist query to regexes for each browser.
getUserAgentRegex(options)
Compile browserslist query to one regex.
Options
Option | Type | Default | Description |
---|---|---|---|
browsers | string | string[] |
— | Manually provide a browserslist query (or an array of queries). Specifying this overrides the browserslist configuration specified in your project. |
env | string |
— | When multiple browserslist environments are specified, pick the config belonging to this environment. |
ignorePatch | boolean |
true |
Ignore differences in patch browser numbers. |
ignoreMinor | boolean |
false |
Ignore differences in minor browser versions. |
allowHigherVersions | boolean |
false |
For all the browsers in the browserslist query, return a match if the useragent version is equal to or higher than the one specified in browserslist. |
allowZeroSubversions | boolean |
false |
Ignore match of patch or patch and minor, if they are 0. |
Regex info object
Property | Type | Description |
---|---|---|
family | string |
Browser family. |
requestVersions | [number, number, number][] |
Versions provided by browserslist. |
regex | RegExp |
Regex to match useragent with family and versions. |
sourceRegex | RegExp |
Original useragent regex, without versions. |
version | [number, number, number] | null |
Useragent version of regex. |
minVersion | [number, number, number] | null |
Useragent min version of regex. |
maxVersion | [number, number, number] | null |
Useragent max version of regex. |
Other
Recommend
-
52
11.28限定某个目录禁止php解析本节内容应用于对静态文件目录或可写的目录进行优化设置,通过限制解析/访问权限来避免别恶意攻击,提高安全性。编辑虚拟主机配置文件:[[email protected]]#vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf<Direct...
-
36
Go 的 fake-useragent 了解一下 有的网站会根据 User-Agent 的不同,跳转到不同(PC、M)的站点,也有根据版本的不同给出不一样的提示等等,而 User-Agent 的变化更是爬虫里的基础姿势 使用 Go 编写网络爬虫或需...
-
35
本文转载自: https://www.jianshu.com/p/0af14da1fbc6。 作者: 莫云溪。 UIWebView
-
16
readme.md UAParser.js
-
7
Christoph Wurst Free and open source softwa...
-
9
CrazyPants Useragent String, Paperback Edition.CrazyPants Useragent String, Paperback Edition. 16 Sep 2013 My colleague Karl got ahold of a ton of interesting log data from a b...
-
8
FrontendAutoprefixer 7.0 and Browserslist 2.0May 06, 2017A...
-
19
sass-compile.vim sass、compassの自動コンパイルを提供するプラグインです。 scssファイルの保存時などに自動的にコンパイルコマンドを実行します。 compass watchなどと違い、保存毎にコマンドを発行するため、 プロセスが残らず、複数の案件の切替...
-
4
Browserslist The config to share target browsers and Node.js versions between different front-end tools. It is used in: All tools will find target browsers automatically, when you add the following to package.json
-
8
Target browsers config for modern web apps
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK