ua-browser 1.3.1 → 1.4.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +228 -0
- package/README.md +95 -85
- package/dist/index.cjs +292 -44
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +120 -17
- package/dist/index.d.ts +120 -17
- package/dist/index.min.js +2 -2
- package/dist/index.min.js.map +1 -1
- package/dist/index.mjs +288 -45
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/README.zh-CN.md +0 -200
package/README.en.md
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# ua-browser
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/ua-browser)
|
|
4
|
+
[](https://www.npmjs.com/package/ua-browser)
|
|
5
|
+
[](./LICENSE)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
7
|
+
|
|
8
|
+
Detect browser, OS, device type, rendering engine, CPU architecture, bots, headless browsers, and Mini Programs from User Agent strings. Zero dependencies. Works in both browser and Node.js environments.
|
|
9
|
+
|
|
10
|
+
**[📖 Documentation](https://yangtianxia.github.io/ua-browser/)** · **[🎮 Playground](https://yangtianxia.github.io/ua-browser/playground)** · **[中文](./README.md)**
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **Comprehensive UA detection** — browser (incl. Arc / Brave), OS (incl. visionOS / tvOS), engine, device type (Mobile / Tablet / PC / TV / Console / XR), CPU arch, bots, headless browsers
|
|
15
|
+
- **Robust device detection** — hardware signals (WebGL GPU renderer, CSS safe-area-inset, DPR, vibration/motion APIs) correctly identify mobile devices even when desktop mode is enabled
|
|
16
|
+
- **Multi-signal arch detection** — `getEnvContext()` collects Client Hints, WebGL renderer, and font probes to accurately distinguish Apple Silicon from Intel Mac
|
|
17
|
+
- **SSR Client Hints** — `parseHeaders()` + `ACCEPT_CH` for precise server-side detection (CPU arch, platform) in Chrome / Edge 90+
|
|
18
|
+
- **AI bot recognition** — 40+ built-in bot rules: GPTBot, ClaudeBot, PerplexityBot, CCBot, messaging link-preview bots (Slack, Discord, Telegram, WhatsApp), and more
|
|
19
|
+
- **Condition matching** — `satisfies(info, { os: 'iOS', device: 'Mobile' })` helper with TypeScript type checking
|
|
20
|
+
- **Zero dependencies** — no runtime dependencies, tiny bundle size after gzip
|
|
21
|
+
- **Pure function** — `parseUA()` has no global state, works seamlessly with SSR / Node.js
|
|
22
|
+
- **TypeScript** — full type definitions with precise literal union types (`BrowserName`, `OsName`, etc.)
|
|
23
|
+
- **Tree-shakeable** — named exports + `sideEffects: false`, unused code eliminated by Vite / Rollup / webpack 5+
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm i ua-browser
|
|
29
|
+
# pnpm
|
|
30
|
+
pnpm add ua-browser
|
|
31
|
+
# yarn
|
|
32
|
+
yarn add ua-browser
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
import uaBrowser from 'ua-browser'
|
|
39
|
+
|
|
40
|
+
const info = uaBrowser()
|
|
41
|
+
|
|
42
|
+
console.log(info)
|
|
43
|
+
// {
|
|
44
|
+
// browser: 'Chrome',
|
|
45
|
+
// version: '124.0.0.0',
|
|
46
|
+
// versionMajor: 124,
|
|
47
|
+
// engine: 'Blink',
|
|
48
|
+
// os: 'Windows',
|
|
49
|
+
// osVersion: '10',
|
|
50
|
+
// device: 'PC',
|
|
51
|
+
// arch: 'x86_64',
|
|
52
|
+
// isWebview: false,
|
|
53
|
+
// isHeadless: false,
|
|
54
|
+
// isBot: false,
|
|
55
|
+
// botName: 'unknown',
|
|
56
|
+
// language: 'en-US',
|
|
57
|
+
// platform: 'Win32',
|
|
58
|
+
// connectionType: 'unknown'
|
|
59
|
+
// }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
> To parse an arbitrary UA string, use the named export: `parseUA('Mozilla/5.0 ...')`
|
|
63
|
+
|
|
64
|
+
## Usage
|
|
65
|
+
|
|
66
|
+
### Browser (recommended: `detect`)
|
|
67
|
+
|
|
68
|
+
Use `detect()` for accurate device and arch detection — it collects hardware signals in addition to parsing the UA string:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import uaBrowser from 'ua-browser'
|
|
72
|
+
|
|
73
|
+
const result = await uaBrowser.detect()
|
|
74
|
+
console.log(result.device) // 'Mobile' — correct even in desktop mode
|
|
75
|
+
console.log(result.arch) // 'arm64' or 'x86_64'
|
|
76
|
+
|
|
77
|
+
if (result.device === 'Mobile') {
|
|
78
|
+
// redirect to mobile version
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Browser (sync: `uaBrowser`)
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import uaBrowser from 'ua-browser'
|
|
86
|
+
|
|
87
|
+
const { browser, os, device } = uaBrowser()
|
|
88
|
+
|
|
89
|
+
if (device === 'Mobile') {
|
|
90
|
+
// redirect to mobile version
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Node.js / SSR
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { parseUA } from 'ua-browser'
|
|
98
|
+
|
|
99
|
+
const ua = req.headers['user-agent'] ?? ''
|
|
100
|
+
const { browser, os, isBot } = parseUA(ua)
|
|
101
|
+
|
|
102
|
+
if (isBot) {
|
|
103
|
+
// block or allow crawlers
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### CDN
|
|
108
|
+
|
|
109
|
+
```html
|
|
110
|
+
<script src="https://cdn.jsdelivr.net/npm/ua-browser/dist/index.min.js"></script>
|
|
111
|
+
<script>
|
|
112
|
+
const { browser, os } = uaBrowser()
|
|
113
|
+
</script>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Multi-signal Detection
|
|
117
|
+
|
|
118
|
+
`getEnvContext()` collects Client Hints, WebGL renderer, and other browser signals in one async call. Use it when you need to reuse the context object or compose it with other options:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import { getEnvContext, parseUA } from 'ua-browser'
|
|
122
|
+
|
|
123
|
+
const ctx = await getEnvContext()
|
|
124
|
+
const result = parseUA(navigator.userAgent, { ctx })
|
|
125
|
+
|
|
126
|
+
console.log(result.device) // 'Mobile' — correct even in desktop mode
|
|
127
|
+
console.log(result.arch) // 'arm64' or 'x86_64'
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### SSR Client Hints
|
|
131
|
+
|
|
132
|
+
Set the `ACCEPT_CH` response header so Chrome / Edge 90+ browsers send Client Hints on subsequent requests, then use `parseHeaders` for precise server-side detection:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { parseHeaders, ACCEPT_CH } from 'ua-browser'
|
|
136
|
+
|
|
137
|
+
// First response — tell the browser to send Client Hints
|
|
138
|
+
res.setHeader('Accept-CH', ACCEPT_CH)
|
|
139
|
+
|
|
140
|
+
// Subsequent requests — accurate arch / OS detection
|
|
141
|
+
const result = parseHeaders(req.headers)
|
|
142
|
+
console.log(result.arch) // 'x86_64' (from Sec-CH-UA-Arch)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Accurate Windows 10 / 11 Detection
|
|
146
|
+
|
|
147
|
+
Windows 10 and 11 share the same UA string. Use the Client Hints API to distinguish them:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { parseUA, getWindowsVersion, getNavContext } from 'ua-browser'
|
|
151
|
+
|
|
152
|
+
const nav = getNavContext()
|
|
153
|
+
const windowsVersion = await getWindowsVersion(nav)
|
|
154
|
+
const result = parseUA(navigator.userAgent, { nav, windowsVersion })
|
|
155
|
+
|
|
156
|
+
console.log(result.osVersion) // '10' or '11'
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## API
|
|
160
|
+
|
|
161
|
+
### Default export `uaBrowser()`
|
|
162
|
+
|
|
163
|
+
Detects the current browser environment, automatically injecting the `navigator` context (language, platform, MIME types, etc.).
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
uaBrowser() // reads navigator.userAgent automatically
|
|
167
|
+
parseUA(customUA) // parse any UA string without browser context
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Named exports (tree-shakeable)
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import {
|
|
174
|
+
parseUA, // pure function, ideal for SSR / Node.js
|
|
175
|
+
getNavContext, // read current browser navigator context
|
|
176
|
+
getWindowsVersion, // async: accurately distinguish Windows 10 / 11
|
|
177
|
+
getLanguage, // extract browser language from NavContext
|
|
178
|
+
getEnvContext, // collect all browser signals (Client Hints, WebGL, etc.)
|
|
179
|
+
parseHeaders, // parse UA and Client Hints from HTTP headers (SSR)
|
|
180
|
+
ACCEPT_CH, // response header constant to request Client Hints
|
|
181
|
+
isWebview, // detect Android Webview / iOS WKWebView
|
|
182
|
+
detectBot, // standalone bot detection
|
|
183
|
+
detectBrowser, // standalone browser detection
|
|
184
|
+
detectOS, // standalone OS detection
|
|
185
|
+
detectEngine, // standalone rendering engine detection
|
|
186
|
+
detectDevice, // standalone device type detection
|
|
187
|
+
detectArch, // standalone CPU architecture detection
|
|
188
|
+
detectHeadless, // standalone headless browser detection
|
|
189
|
+
satisfies, // condition-matching helper
|
|
190
|
+
VERSION, // current library version
|
|
191
|
+
} from 'ua-browser'
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Return value `EnvOption`
|
|
195
|
+
|
|
196
|
+
| Field | Type | Description |
|
|
197
|
+
| :-- | :-- | :-- |
|
|
198
|
+
| `browser` | `BrowserName` | Browser name |
|
|
199
|
+
| `version` | `string` | Browser version |
|
|
200
|
+
| `versionMajor` | `number` | Browser major version (`parseInt(version)`) |
|
|
201
|
+
| `engine` | `EngineName` | Rendering engine |
|
|
202
|
+
| `os` | `OsName` | Operating system |
|
|
203
|
+
| `osVersion` | `string` | OS version |
|
|
204
|
+
| `device` | `DeviceName` | Device type: `Mobile` \| `Tablet` \| `PC` \| `TV` \| `Console` \| `XR` |
|
|
205
|
+
| `arch` | `ArchName` | CPU architecture |
|
|
206
|
+
| `isWebview` | `boolean` | Whether running in Android Webview or iOS WKWebView |
|
|
207
|
+
| `isHeadless` | `boolean` | Whether running in a headless / automated browser |
|
|
208
|
+
| `isBot` | `boolean` | Whether the UA belongs to a bot / crawler |
|
|
209
|
+
| `botName` | `BotName` | Bot name |
|
|
210
|
+
| `language` | `string` | Browser language, e.g. `en-US` |
|
|
211
|
+
| `platform` | `string` | Platform identifier, e.g. `Win32` |
|
|
212
|
+
| `connectionType` | `string` | Network type: `4g` \| `3g` \| `2g` \| `slow-2g` \| `unknown` |
|
|
213
|
+
|
|
214
|
+
> All fields return `'unknown'` when undetected — never an empty string or `null`.
|
|
215
|
+
|
|
216
|
+
## Supported
|
|
217
|
+
|
|
218
|
+
Over 70 browsers, 20 operating systems, and 40+ bot rules built in. See the **[full support list](https://yangtianxia.github.io/ua-browser/guide/support-list)**.
|
|
219
|
+
|
|
220
|
+
Highlights:
|
|
221
|
+
- **Browsers** — Chrome, Safari, Arc, Brave, Firefox, Edge, Samsung Internet, UC, WeChat, DingTalk, TikTok, Bilibili, Kuaishou, Xiaohongshu, Feishu and more
|
|
222
|
+
- **OS** — Windows, macOS, Android, iOS, visionOS, tvOS, HarmonyOS, OpenHarmony, Tizen, KaiOS and more
|
|
223
|
+
- **Bots** — GPTBot, ClaudeBot, PerplexityBot, CCBot; messaging bots (Slack, Discord, Telegram, WhatsApp) and more
|
|
224
|
+
- **Devices** — Mobile, Tablet, PC, TV (Samsung Smart TV, HbbTV), Console (PS5, Xbox, Switch), XR (Vision Pro, Quest)
|
|
225
|
+
|
|
226
|
+
## License
|
|
227
|
+
|
|
228
|
+
[MIT](./LICENSE) © yangtianxia
|
package/README.md
CHANGED
|
@@ -5,22 +5,23 @@
|
|
|
5
5
|
[](./LICENSE)
|
|
6
6
|
[](https://www.typescriptlang.org/)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
通过 User Agent 检测浏览器、操作系统、设备类型、渲染内核、CPU 架构、爬虫、无头浏览器及小程序运行环境。零依赖,支持浏览器与 Node.js 双环境。
|
|
9
9
|
|
|
10
|
-
**[📖
|
|
10
|
+
**[📖 文档](https://yangtianxia.github.io/ua-browser/)** · **[🎮 Playground](https://yangtianxia.github.io/ua-browser/playground)** · **[English](./README.en.md)**
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## 特性
|
|
13
13
|
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
- **SSR Client Hints** — `parseHeaders()` + `ACCEPT_CH
|
|
17
|
-
- **AI
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
- **
|
|
14
|
+
- **全面 UA 检测** — 浏览器(含 Arc / Brave)、OS(含 visionOS / tvOS)、渲染内核、设备类型(Mobile / Tablet / TV / PC / Console / XR)、CPU 架构、爬虫、无头浏览器
|
|
15
|
+
- **多信号架构检测** — `getEnvContext()` 采集 Client Hints、WebGL 渲染器、字体探针,精确区分 Apple Silicon 与 Intel Mac
|
|
16
|
+
- **SSR Client Hints** — `parseHeaders()` + `ACCEPT_CH`,在 Chrome / Edge 90+ 中实现服务端精准检测(CPU 架构、平台等)
|
|
17
|
+
- **AI 爬虫识别** — 内置 GPTBot、ClaudeBot、PerplexityBot、CCBot 等 40+ 种爬虫规则,含消息应用链接预览 Bot(Slack、Discord、Telegram)
|
|
18
|
+
- **条件匹配** — `satisfies(info, { os: 'iOS', device: 'Mobile' })` 辅助函数,支持 TypeScript 类型检查
|
|
19
|
+
- **零依赖** — 无任何运行时依赖,gzip 后体积极小
|
|
20
|
+
- **纯函数** — `parseUA()` 无全局状态,天然支持 SSR / Node.js
|
|
21
|
+
- **TypeScript** — 完整类型定义,`BrowserName`、`OsName` 等均为精确字面量联合类型
|
|
22
|
+
- **Tree-shakeable** — 所有功能按需导入,不引入多余代码
|
|
22
23
|
|
|
23
|
-
##
|
|
24
|
+
## 安装
|
|
24
25
|
|
|
25
26
|
```sh
|
|
26
27
|
npm i ua-browser
|
|
@@ -30,7 +31,7 @@ pnpm add ua-browser
|
|
|
30
31
|
yarn add ua-browser
|
|
31
32
|
```
|
|
32
33
|
|
|
33
|
-
##
|
|
34
|
+
## 快速上手
|
|
34
35
|
|
|
35
36
|
```typescript
|
|
36
37
|
import uaBrowser from 'ua-browser'
|
|
@@ -39,27 +40,29 @@ const info = uaBrowser()
|
|
|
39
40
|
|
|
40
41
|
console.log(info)
|
|
41
42
|
// {
|
|
42
|
-
// browser:
|
|
43
|
-
// version:
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
//
|
|
48
|
-
//
|
|
49
|
-
//
|
|
50
|
-
//
|
|
51
|
-
//
|
|
52
|
-
//
|
|
53
|
-
//
|
|
54
|
-
//
|
|
43
|
+
// browser: 'Chrome',
|
|
44
|
+
// version: '124.0.0.0',
|
|
45
|
+
// versionMajor: 124,
|
|
46
|
+
// engine: 'Blink',
|
|
47
|
+
// os: 'Windows',
|
|
48
|
+
// osVersion: '10',
|
|
49
|
+
// device: 'PC',
|
|
50
|
+
// arch: 'x86_64',
|
|
51
|
+
// isWebview: false,
|
|
52
|
+
// isHeadless: false,
|
|
53
|
+
// isBot: false,
|
|
54
|
+
// botName: 'unknown',
|
|
55
|
+
// language: 'zh-CN',
|
|
56
|
+
// platform: 'Win32',
|
|
57
|
+
// connectionType: 'unknown'
|
|
55
58
|
// }
|
|
56
59
|
```
|
|
57
60
|
|
|
58
|
-
>
|
|
61
|
+
> 解析任意 UA 字符串请使用命名导出:`parseUA('Mozilla/5.0 ...')`
|
|
59
62
|
|
|
60
|
-
##
|
|
63
|
+
## 使用
|
|
61
64
|
|
|
62
|
-
###
|
|
65
|
+
### 浏览器环境
|
|
63
66
|
|
|
64
67
|
```typescript
|
|
65
68
|
import uaBrowser from 'ua-browser'
|
|
@@ -67,7 +70,7 @@ import uaBrowser from 'ua-browser'
|
|
|
67
70
|
const { browser, os, device } = uaBrowser()
|
|
68
71
|
|
|
69
72
|
if (device === 'Mobile') {
|
|
70
|
-
//
|
|
73
|
+
// 跳转移动版
|
|
71
74
|
}
|
|
72
75
|
```
|
|
73
76
|
|
|
@@ -80,7 +83,7 @@ const ua = req.headers['user-agent'] ?? ''
|
|
|
80
83
|
const { browser, os, isBot } = parseUA(ua)
|
|
81
84
|
|
|
82
85
|
if (isBot) {
|
|
83
|
-
//
|
|
86
|
+
// 拦截或放行爬虫
|
|
84
87
|
}
|
|
85
88
|
```
|
|
86
89
|
|
|
@@ -93,9 +96,9 @@ if (isBot) {
|
|
|
93
96
|
</script>
|
|
94
97
|
```
|
|
95
98
|
|
|
96
|
-
###
|
|
99
|
+
### 多信号架构检测
|
|
97
100
|
|
|
98
|
-
`getEnvContext()`
|
|
101
|
+
`getEnvContext()` 一次性采集 Client Hints、WebGL 渲染器等多维信号,可区分 Apple Silicon 与 Intel Mac:
|
|
99
102
|
|
|
100
103
|
```typescript
|
|
101
104
|
import { getEnvContext, parseUA } from 'ua-browser'
|
|
@@ -103,27 +106,27 @@ import { getEnvContext, parseUA } from 'ua-browser'
|
|
|
103
106
|
const ctx = await getEnvContext()
|
|
104
107
|
const result = parseUA(navigator.userAgent, { ctx })
|
|
105
108
|
|
|
106
|
-
console.log(result.arch) // 'arm64'
|
|
109
|
+
console.log(result.arch) // 'arm64' 或 'x86_64'
|
|
107
110
|
```
|
|
108
111
|
|
|
109
112
|
### SSR Client Hints
|
|
110
113
|
|
|
111
|
-
|
|
114
|
+
通过响应头告知 Chrome / Edge 90+ 上报 Client Hints,再用 `parseHeaders` 在服务端精准解析:
|
|
112
115
|
|
|
113
116
|
```typescript
|
|
114
117
|
import { parseHeaders, ACCEPT_CH } from 'ua-browser'
|
|
115
118
|
|
|
116
|
-
//
|
|
119
|
+
// 第一次响应时写入 Accept-CH
|
|
117
120
|
res.setHeader('Accept-CH', ACCEPT_CH)
|
|
118
121
|
|
|
119
|
-
//
|
|
122
|
+
// 后续请求携带 Client Hints 后,精准识别架构等信息
|
|
120
123
|
const result = parseHeaders(req.headers)
|
|
121
|
-
console.log(result.arch) // 'x86_64'
|
|
124
|
+
console.log(result.arch) // 'x86_64'(来自 Sec-CH-UA-Arch)
|
|
122
125
|
```
|
|
123
126
|
|
|
124
|
-
###
|
|
127
|
+
### 精确区分 Windows 10 / 11
|
|
125
128
|
|
|
126
|
-
Windows 10
|
|
129
|
+
Windows 10 和 11 的 UA 字符串相同,需借助 Client Hints API 异步获取:
|
|
127
130
|
|
|
128
131
|
```typescript
|
|
129
132
|
import { parseUA, getWindowsVersion, getNavContext } from 'ua-browser'
|
|
@@ -132,68 +135,75 @@ const nav = getNavContext()
|
|
|
132
135
|
const windowsVersion = await getWindowsVersion(nav)
|
|
133
136
|
const result = parseUA(navigator.userAgent, { nav, windowsVersion })
|
|
134
137
|
|
|
135
|
-
console.log(result.osVersion) // '10'
|
|
138
|
+
console.log(result.osVersion) // '10' 或 '11'
|
|
136
139
|
```
|
|
137
140
|
|
|
138
141
|
## API
|
|
139
142
|
|
|
140
|
-
###
|
|
143
|
+
### 默认导出 `uaBrowser()`
|
|
141
144
|
|
|
142
|
-
|
|
145
|
+
检测当前浏览器环境,自动注入 `navigator` 上下文(语言、平台、MIME 类型等)。
|
|
143
146
|
|
|
144
147
|
```typescript
|
|
145
|
-
uaBrowser() //
|
|
146
|
-
|
|
148
|
+
uaBrowser() // 自动读取 navigator.userAgent
|
|
149
|
+
parseUA(customUA) // 解析任意 UA 字符串(无浏览器上下文依赖)
|
|
147
150
|
```
|
|
148
151
|
|
|
149
|
-
###
|
|
152
|
+
### 命名导出(按需引入)
|
|
150
153
|
|
|
151
154
|
```typescript
|
|
152
155
|
import {
|
|
153
|
-
parseUA, //
|
|
154
|
-
getNavContext, //
|
|
155
|
-
getWindowsVersion, //
|
|
156
|
-
getLanguage, //
|
|
157
|
-
getEnvContext, //
|
|
158
|
-
parseHeaders, //
|
|
159
|
-
ACCEPT_CH, //
|
|
160
|
-
isWebview, //
|
|
161
|
-
detectBot, //
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
156
|
+
parseUA, // 纯函数,适合 SSR / Node.js
|
|
157
|
+
getNavContext, // 读取当前浏览器 navigator 上下文
|
|
158
|
+
getWindowsVersion, // 异步精确区分 Windows 10 / 11
|
|
159
|
+
getLanguage, // 从 NavContext 获取浏览器语言
|
|
160
|
+
getEnvContext, // 采集所有浏览器环境信号(Client Hints、WebGL 等)
|
|
161
|
+
parseHeaders, // 从 HTTP 请求头解析 UA 及 Client Hints(SSR)
|
|
162
|
+
ACCEPT_CH, // 响应头常量,告知浏览器上报 Client Hints
|
|
163
|
+
isWebview, // 检测 Android Webview / iOS WKWebView
|
|
164
|
+
detectBot, // 独立爬虫检测
|
|
165
|
+
detectBrowser, // 独立浏览器检测
|
|
166
|
+
detectOS, // 独立操作系统检测
|
|
167
|
+
detectEngine, // 独立渲染引擎检测
|
|
168
|
+
detectDevice, // 独立设备类型检测
|
|
169
|
+
detectArch, // 独立 CPU 架构检测
|
|
170
|
+
detectHeadless, // 独立无头浏览器检测
|
|
171
|
+
satisfies, // 条件匹配辅助函数
|
|
172
|
+
VERSION, // 当前版本号
|
|
165
173
|
} from 'ua-browser'
|
|
166
174
|
```
|
|
167
175
|
|
|
168
|
-
###
|
|
176
|
+
### 返回值 `EnvOption`
|
|
169
177
|
|
|
170
|
-
|
|
|
178
|
+
| 字段 | 类型 | 说明 |
|
|
171
179
|
| :-- | :-- | :-- |
|
|
172
|
-
| `browser` | `BrowserName` |
|
|
173
|
-
| `version` | `string` |
|
|
174
|
-
| `
|
|
175
|
-
| `
|
|
176
|
-
| `
|
|
177
|
-
| `
|
|
178
|
-
| `
|
|
179
|
-
| `
|
|
180
|
-
| `
|
|
181
|
-
| `
|
|
182
|
-
| `
|
|
183
|
-
| `
|
|
184
|
-
| `
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
-
|
|
196
|
-
-
|
|
180
|
+
| `browser` | `BrowserName` | 浏览器名称 |
|
|
181
|
+
| `version` | `string` | 浏览器版本 |
|
|
182
|
+
| `versionMajor` | `number` | 浏览器主版本号(`parseInt(version)`) |
|
|
183
|
+
| `engine` | `EngineName` | 渲染内核 |
|
|
184
|
+
| `os` | `OsName` | 操作系统 |
|
|
185
|
+
| `osVersion` | `string` | 系统版本 |
|
|
186
|
+
| `device` | `DeviceName` | 设备类型:`Mobile` \| `Tablet` \| `PC` \| `TV` \| `Console` \| `XR` |
|
|
187
|
+
| `arch` | `ArchName` | CPU 架构 |
|
|
188
|
+
| `isWebview` | `boolean` | 是否为 Android Webview / iOS WKWebView |
|
|
189
|
+
| `isHeadless` | `boolean` | 是否为无头 / 自动化浏览器 |
|
|
190
|
+
| `isBot` | `boolean` | 是否为爬虫 / 机器人 |
|
|
191
|
+
| `botName` | `BotName` | 爬虫名称 |
|
|
192
|
+
| `language` | `string` | 浏览器语言,如 `zh-CN` |
|
|
193
|
+
| `platform` | `string` | 平台标识,如 `Win32` |
|
|
194
|
+
| `connectionType` | `string` | 网络类型:`4g` \| `3g` \| `2g` \| `slow-2g` \| `unknown` |
|
|
195
|
+
|
|
196
|
+
> 所有字段在无法识别时统一返回 `'unknown'`,不返回空字符串或 `null`。
|
|
197
|
+
|
|
198
|
+
## 支持范围
|
|
199
|
+
|
|
200
|
+
内置超过 70 种浏览器、20 种操作系统、40+ 种爬虫规则,详见 **[内置支持列表](https://yangtianxia.github.io/ua-browser/guide/support-list)**。
|
|
201
|
+
|
|
202
|
+
部分覆盖:
|
|
203
|
+
- **浏览器** — Chrome、Safari、Arc、Brave、Firefox、Edge、Samsung Internet、UC、微信、钉钉、抖音、哔哩哔哩、快手、小红书、飞书等
|
|
204
|
+
- **操作系统** — Windows、macOS、Android、iOS、visionOS、tvOS、HarmonyOS、OpenHarmony、Tizen、KaiOS 等
|
|
205
|
+
- **AI 爬虫** — GPTBot、ClaudeBot、PerplexityBot、CCBot;消息应用 Bot(Slack、Discord、Telegram、WhatsApp)等
|
|
206
|
+
- **设备** — Mobile、Tablet、PC、TV(含三星 Smart TV、HbbTV 标准)、Console(PS5、Xbox、Switch)、XR(Vision Pro、Quest)
|
|
197
207
|
|
|
198
208
|
## License
|
|
199
209
|
|