ua-parser-js 2.0.0-alpha.2 → 2.0.0-beta.1
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE.md +616 -0
- package/README.md +66 -0
- package/dist/ua-parser.min.js +3 -3
- package/dist/ua-parser.pack.js +3 -3
- package/package.json +60 -35
- package/src/enums/ua-parser-enums.js +236 -0
- package/src/enums/ua-parser-enums.mjs +240 -0
- package/src/extensions/ua-parser-extensions.js +248 -0
- package/src/extensions/ua-parser-extensions.mjs +252 -0
- package/src/main/ua-parser.d.ts +104 -0
- package/src/{ua-parser.js → main/ua-parser.js} +283 -206
- package/src/{ua-parser.mjs → main/ua-parser.mjs} +284 -209
- package/license.md +0 -21
- package/readme.md +0 -603
- package/src/enum/ua-parser-enum.js +0 -101
- package/src/enum/ua-parser-enum.mjs +0 -105
- package/src/extension/ua-parser-extension.js +0 -120
- package/src/extension/ua-parser-extension.mjs +0 -124
package/license.md
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2012-2023 Faisal Salman <<f@faisalman.com>>
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
13
|
-
copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
-
SOFTWARE.
|
package/readme.md
DELETED
@@ -1,603 +0,0 @@
|
|
1
|
-
<p align="center">
|
2
|
-
<img src="https://raw.githubusercontent.com/faisalman/ua-parser-js/gh-pages/images/logo.png" width="256" height="256">
|
3
|
-
</p>
|
4
|
-
|
5
|
-
<p align="center">
|
6
|
-
<a href="https://travis-ci.org/faisalman/ua-parser-js"><img src="https://travis-ci.org/faisalman/ua-parser-js.svg?branch=master"></a>
|
7
|
-
<a href="https://www.npmjs.com/package/ua-parser-js"><img src="https://img.shields.io/npm/v/ua-parser-js.svg"></a>
|
8
|
-
<a href="https://www.npmjs.com/package/ua-parser-js"><img src="https://img.shields.io/npm/dw/ua-parser-js.svg"></a>
|
9
|
-
<a href="https://www.jsdelivr.com/package/npm/ua-parser-js"><img src="https://data.jsdelivr.com/v1/package/npm/ua-parser-js/badge"></a>
|
10
|
-
<a href="https://cdnjs.com/libraries/UAParser.js"><img src="https://img.shields.io/cdnjs/v/UAParser.js.svg"></a>
|
11
|
-
</p>
|
12
|
-
|
13
|
-
# UAParser.js
|
14
|
-
|
15
|
-
JavaScript library to detect Browser, Engine, OS, CPU, and Device type/model from User-Agent & Client-Hints data that can be used either in browser (client-side) or node.js (server-side).
|
16
|
-
|
17
|
-
* Author : Faisal Salman <<f@faisalman.com>>
|
18
|
-
* Demo : https://faisalman.github.io/ua-parser-js
|
19
|
-
* Source : https://github.com/faisalman/ua-parser-js
|
20
|
-
|
21
|
-
***
|
22
|
-
|
23
|
-
### From Our Sponsors:
|
24
|
-
<table>
|
25
|
-
<thead>
|
26
|
-
</thead>
|
27
|
-
<tbody>
|
28
|
-
<tr>
|
29
|
-
<td align="center" width="200px" rowspan="2"><a href="https://www.npmjs.com/package/@51degrees/ua-parser-js"><img src="images/51degrees.svg" alt="51degrees" width="75%" height="75%" ></a></td>
|
30
|
-
<td align="left" width="400px"><a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">↗ @51degrees/ua-parser-js</a></td>
|
31
|
-
</tr>
|
32
|
-
<tr>
|
33
|
-
<td><br/><p>UAParser.js has been upgraded to detect comprehensive device data based on the User-Agent and User-Agent Client Hints.</p><p>This package supports all device types including Apple and Android devices and can be used either in a browser (client-side) or Node.js environment (server-side).</p><p>Visit <a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">↗ 51Degrees <u>UAParser</u></a> to get started.</p>
|
34
|
-
</td>
|
35
|
-
</tr>
|
36
|
-
<tr>
|
37
|
-
<td colspan="2">
|
38
|
-
<a href="https://opencollective.com/ua-parser-js">↗ Become a sponsor</a>
|
39
|
-
</td>
|
40
|
-
</tr>
|
41
|
-
</tbody>
|
42
|
-
</table>
|
43
|
-
|
44
|
-
---
|
45
|
-
|
46
|
-
# Version 2.0
|
47
|
-
What's new & breaking, please read [CHANGELOG](changelog.md) before upgrading.
|
48
|
-
|
49
|
-
# Documentation
|
50
|
-
### UAParser([user-agent:string][,extensions:object][,headers:object(since@2.0)])
|
51
|
-
|
52
|
-
In the browser environment you dont need to pass the user-agent string to the function, you can just call the funtion and it should automatically get the string from the `window.navigator.userAgent`, but that is not the case in nodejs. The user-agent string must be passed in' nodejs for the function to work. Usually you can find the user agent in: `request.headers["user-agent"]`.
|
53
|
-
|
54
|
-
|
55
|
-
## Constructor
|
56
|
-
When you call `UAParser` with the `new` keyword, `UAParser` will return a new instance with an empty result object, you have to call one of the available methods to get the information from the user-agent string.
|
57
|
-
Like so:
|
58
|
-
* `new UAParser([user-agent:string][,extensions:object][,headers:object(since@2.0)])`
|
59
|
-
```js
|
60
|
-
let parser = new UAParser("your user-agent here"); // you need to pass the user-agent for nodejs
|
61
|
-
console.log(parser); // {}
|
62
|
-
let parserResults = parser.getResult();
|
63
|
-
console.log(parserResults);
|
64
|
-
/** {
|
65
|
-
"ua" : "",
|
66
|
-
"browser" : {},
|
67
|
-
"engine" : {},
|
68
|
-
"os" : {},
|
69
|
-
"device" : {},
|
70
|
-
"cpu" : {}
|
71
|
-
} */
|
72
|
-
```
|
73
|
-
|
74
|
-
When you call UAParser without the `new` keyword, it will automatically call `getResult()` function and return the parsed results.
|
75
|
-
* `UAParser([user-agent:string][,extensions:object][,headers:object(since@2.0)])`
|
76
|
-
* returns result object `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
|
77
|
-
|
78
|
-
## Methods
|
79
|
-
|
80
|
-
#### Methods table
|
81
|
-
The methods are self explanatory, here's a small overview on all the available methods:
|
82
|
-
* `getResult()` - returns all function object calls, user-agent string, browser info, cpu, device, engine, os:
|
83
|
-
`{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`.
|
84
|
-
|
85
|
-
* `getBrowser()` - returns the browser name and version.
|
86
|
-
* `getDevice()` - returns the device model, type, vendor.
|
87
|
-
* `getEngine()` - returns the current browser engine name and version.
|
88
|
-
* `getOS()` - returns the running operating system name and version.
|
89
|
-
* `getCPU()` - returns CPU architectural design name.
|
90
|
-
* `getUA()` - returns the user-agent string.
|
91
|
-
* `setUA(user-agent)` - set a custom user-agent to be parsed.
|
92
|
-
|
93
|
-
---
|
94
|
-
|
95
|
-
* `getResult()`
|
96
|
-
* returns `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
|
97
|
-
|
98
|
-
* `getBrowser()`
|
99
|
-
* returns `{ name: '', version: '' }`
|
100
|
-
|
101
|
-
```sh
|
102
|
-
# Possible 'browser.name':
|
103
|
-
2345Explorer, 360 Browser, Amaya, Android Browser, Arora, Avant, Avast, AVG,
|
104
|
-
BIDUBrowser, Baidu, Basilisk, Blazer, Bolt, Brave, Bowser, Camino, Chimera,
|
105
|
-
[Mobile] Chrome [Headless/WebView], Chromium, Cobalt, Comodo Dragon, Dillo,
|
106
|
-
Dolphin, Doris, DuckDuckGo, Edge, Electron, Epiphany, Facebook, Falkon, Fennec,
|
107
|
-
Firebird, [Mobile] Firefox [Focus/Reality], Flock, Flow, GSA, GoBrowser, HeyTap,
|
108
|
-
Huawei Browser, ICE Browser, IE, IEMobile, IceApe, IceCat, IceDragon, Iceweasel,
|
109
|
-
Instagram, Iridium, Iron, Jasmine, Kakao[Story/Talk], K-Meleon, Kindle, Klar,
|
110
|
-
Konqueror, LBBROWSER, Line, LinkedIn, Links, Lunascape, Lynx, MIUI Browser,
|
111
|
-
Maemo Browser, Maemo, Maxthon, MetaSr Midori, Minimo, Mosaic, Mozilla, NetFront,
|
112
|
-
NetSurf, Netfront, Netscape, NokiaBrowser, Obigo, Oculus Browser, OmniWeb,
|
113
|
-
Opera Coast, Opera [Mini/Mobi/Tablet], PaleMoon, PhantomJS, Phoenix, Polaris,
|
114
|
-
Puffin, QQ, QQBrowser, QQBrowserLite, Quark, QupZilla, RockMelt, [Mobile] Safari,
|
115
|
-
Sailfish Browser, Samsung Browser, SeaMonkey, Silk, Skyfire, Sleipnir, Slim,
|
116
|
-
SlimBrowser, Swiftfox, Tesla, TikTok, Tizen Browser, UCBrowser, UP.Browser, Viera,
|
117
|
-
Vivaldi, Waterfox, WeChat, Weibo, Yandex, baidu, iCab, w3m, Whale Browser, ...
|
118
|
-
|
119
|
-
# 'browser.version' determined dynamically
|
120
|
-
```
|
121
|
-
|
122
|
-
* `getDevice()`
|
123
|
-
* returns `{ model: '', type: '', vendor: '' }`
|
124
|
-
|
125
|
-
```sh
|
126
|
-
# Possible 'device.type':
|
127
|
-
console, mobile, tablet, smarttv, wearable, embedded
|
128
|
-
|
129
|
-
##########
|
130
|
-
# NOTE: 'desktop' is not a possible device type.
|
131
|
-
# UAParser only reports info directly available from the UA string, which is not the case for 'desktop' device type.
|
132
|
-
# If you wish to detect desktop devices, you must handle the needed logic yourself.
|
133
|
-
# You can read more about it in this issue: https://github.com/faisalman/ua-parser-js/issues/182
|
134
|
-
##########
|
135
|
-
|
136
|
-
# Possible 'device.vendor':
|
137
|
-
Acer, Alcatel, Amazon, Apple, Archos, ASUS, AT&T, BenQ, BlackBerry, Dell,
|
138
|
-
Essential, Facebook, Fairphone, GeeksPhone, Google, HP, HTC, Huawei, Jolla, Kobo,
|
139
|
-
Lenovo, LG, Meizu, Microsoft, Motorola, Nexian, Nintendo, Nokia, Nvidia, OnePlus,
|
140
|
-
OPPO, Ouya, Palm, Panasonic, Pebble, Polytron, Realme, RIM, Roku, Samsung, Sharp,
|
141
|
-
Siemens, Sony[Ericsson], Sprint, Tesla, Vivo, Vodafone, Xbox, Xiaomi, Zebra, ZTE, ...
|
142
|
-
|
143
|
-
# 'device.model' determined dynamically
|
144
|
-
```
|
145
|
-
|
146
|
-
* `getEngine()`
|
147
|
-
* returns `{ name: '', version: '' }`
|
148
|
-
|
149
|
-
```sh
|
150
|
-
# Possible 'engine.name'
|
151
|
-
Amaya, Blink, EdgeHTML, Flow, Gecko, Goanna, iCab, KHTML, LibWeb, Links, Lynx,
|
152
|
-
NetFront, NetSurf, Presto, Tasman, Trident, w3m, WebKit
|
153
|
-
|
154
|
-
# 'engine.version' determined dynamically
|
155
|
-
```
|
156
|
-
|
157
|
-
* `getOS()`
|
158
|
-
* returns `{ name: '', version: '' }`
|
159
|
-
|
160
|
-
```sh
|
161
|
-
# Possible 'os.name'
|
162
|
-
AIX, Amiga OS, Android[-x86], Arch, Bada, BeOS, BlackBerry, CentOS, Chromium OS,
|
163
|
-
Contiki, Fedora, Firefox OS, FreeBSD, Debian, Deepin, DragonFly, elementary OS,
|
164
|
-
Fuchsia, Gentoo, GhostBSD, GNU, Haiku, HarmonyOS, HP-UX, Hurd, iOS, Joli, KaiOS,
|
165
|
-
Linpus, Linspire,Linux, Mac OS, Maemo, Mageia, Mandriva, Manjaro, MeeGo, Minix,
|
166
|
-
Mint, Morph OS, NetBSD, NetRange, NetTV, Nintendo, OpenBSD, OpenVMS, OS/2, Palm,
|
167
|
-
PC-BSD, PCLinuxOS, Plan9, PlayStation, QNX, Raspbian, RedHat, RIM Tablet OS,
|
168
|
-
RISC OS, Sabayon, Sailfish, SerenityOS, Series40, Slackware, Solaris, SUSE, Symbian,
|
169
|
-
Tizen, Ubuntu, Unix, VectorLinux, Viera, watchOS, WebOS, Windows [Phone/Mobile],
|
170
|
-
Zenwalk, ...
|
171
|
-
|
172
|
-
# 'os.version' determined dynamically
|
173
|
-
```
|
174
|
-
|
175
|
-
* `getCPU()`
|
176
|
-
* returns `{ architecture: '' }`
|
177
|
-
|
178
|
-
```sh
|
179
|
-
# Possible 'cpu.architecture'
|
180
|
-
68k, amd64, arm[64/hf], avr, ia[32/64], irix[64], mips[64], pa-risc, ppc, sparc[64]
|
181
|
-
```
|
182
|
-
|
183
|
-
* `getUA()`
|
184
|
-
* returns UA string of current instance
|
185
|
-
|
186
|
-
* `setUA(uastring)`
|
187
|
-
* set UA string to be parsed
|
188
|
-
* returns current instance
|
189
|
-
|
190
|
-
#### * `is():boolean` utility `since@2.0`
|
191
|
-
|
192
|
-
```js
|
193
|
-
// Is just a shorthand comparison to check whether the value of specified item equals one of its properties (in a case-insensitive way)
|
194
|
-
// so that instead of write it using `==` operator like this:
|
195
|
-
|
196
|
-
let ua = UAParser();
|
197
|
-
let device = ua.device;
|
198
|
-
let os = ua.os;
|
199
|
-
|
200
|
-
if (device.type == "mobile" && os.name != "iOS") {}
|
201
|
-
if (device.type == "smarttv" || device.vendor == "Samsung") {}
|
202
|
-
|
203
|
-
// we can also write the comparison above into as follow:
|
204
|
-
|
205
|
-
if (device.is("mobile") && !os.is("iOS")) {}
|
206
|
-
if (device.is("SmartTV") || device.is("SaMsUnG")) {}
|
207
|
-
|
208
|
-
/*
|
209
|
-
For device, properties will be checked in this particular order: type, model, vendor
|
210
|
-
*/
|
211
|
-
|
212
|
-
// Another examples:
|
213
|
-
|
214
|
-
let uap = new UAParser('Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 635) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537');
|
215
|
-
|
216
|
-
uap.getBrowser().name; // "IEMobile"
|
217
|
-
uap.getBrowser().is("IEMobile"); // true
|
218
|
-
uap.getCPU().is("ARM"); // true
|
219
|
-
|
220
|
-
uap.getOS().name; // "Windows Phone"
|
221
|
-
uap.getOS().is("Windows Phone"); // true
|
222
|
-
|
223
|
-
uap.getDevice(); // { vendor: "Nokia", model: "Lumia 635", type: "mobile" }
|
224
|
-
uap.getResult().device; // { vendor: "Nokia", model: "Lumia 635", type: "mobile" }
|
225
|
-
|
226
|
-
let device = uap.getDevice();
|
227
|
-
device.is("mobile"); // true
|
228
|
-
device.is("Lumia 635"); // true
|
229
|
-
device.is("Nokia"); // true
|
230
|
-
device.is("iPhone"); // false
|
231
|
-
uap.getResult().device.is("Nokia"); // true
|
232
|
-
uap.getResult().device.model; // "Lumia 635"
|
233
|
-
|
234
|
-
uap.setUA("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
|
235
|
-
|
236
|
-
let browser = uap.getBrowser();
|
237
|
-
browser.is("IEMobile"); // false
|
238
|
-
browser.is("Chrome"); // true
|
239
|
-
|
240
|
-
uap.getResult().browser.is("Edge"); // false
|
241
|
-
uap.getResult().os.name // "Mac OS"
|
242
|
-
uap.getResult().os.is("Mac OS"); // true
|
243
|
-
uap.getResult().os.version; // "10.6.8"
|
244
|
-
|
245
|
-
let engine = uap.getEngine();
|
246
|
-
engine.is("Blink"); // true
|
247
|
-
```
|
248
|
-
|
249
|
-
#### * `toString():string` utility `since@2.0`
|
250
|
-
|
251
|
-
```js
|
252
|
-
// Retrieve full-name values as a string
|
253
|
-
|
254
|
-
/*
|
255
|
-
Values will be concatenated following this pattern:
|
256
|
-
* browser : name + version
|
257
|
-
* cpu : architecture
|
258
|
-
* device : vendor + model
|
259
|
-
* engine : name + version
|
260
|
-
* os : name + version
|
261
|
-
*/
|
262
|
-
|
263
|
-
// Usage examples
|
264
|
-
|
265
|
-
let uap = new UAParser('Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 635) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537');
|
266
|
-
|
267
|
-
uap.getDevice(); // {
|
268
|
-
// vendor: "Nokia",
|
269
|
-
// model: "Lumia 635",
|
270
|
-
// type: "mobile"
|
271
|
-
// }
|
272
|
-
uap.getDevice().toString(); // "Nokia Lumia 635"
|
273
|
-
|
274
|
-
uap.getResult().os.name; // "Windows Phone"
|
275
|
-
uap.getResult().os.version; // "8.1"
|
276
|
-
uap.getResult().os.toString(); // "Windows Phone 8.1"
|
277
|
-
|
278
|
-
uap.setUA("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
|
279
|
-
uap.getBrowser().name; // "Chrome"
|
280
|
-
uap.getBrowser().version; // "28.0.1500.95"
|
281
|
-
uap.getBrowser().major; // "28"
|
282
|
-
uap.getBrowser().toString(); // "Chrome 28.0.1500.95"
|
283
|
-
|
284
|
-
let engine = uap.getEngine();
|
285
|
-
engine.name; // "Blink"
|
286
|
-
engine.version; // "28.0.1500.95"
|
287
|
-
engine.toString(); // "Blink 28.0.1500.95"
|
288
|
-
```
|
289
|
-
|
290
|
-
#### * `withClientHints():Promise<object>|Thenable<object>|object` `since@2.0`
|
291
|
-
|
292
|
-
Recently, Chrome limits the information exposed through user-agent and introduces a new experimental set of data called "client-hints". In browser-environment, obtaining the client-hints data via JavaScript must be done in an asynchronous way. In `UAParser` you can chain the result object from `get*` method with `withClientHints()` to also read the client-hints data from the browser and return the updated data as a `Promise`.
|
293
|
-
|
294
|
-
```js
|
295
|
-
// client-side example
|
296
|
-
(async function () {
|
297
|
-
let ua = new UAParser();
|
298
|
-
|
299
|
-
// get browser data from user-agent only :
|
300
|
-
let browser = ua.getBrowser();
|
301
|
-
console.log('Using User-Agent: ', browser);
|
302
|
-
|
303
|
-
// get browser data from client-hints (with user-agent as fallback) :
|
304
|
-
browser = await ua.getBrowser().withClientHints();
|
305
|
-
console.log('Using Client-Hints: ', browser);
|
306
|
-
|
307
|
-
// alternatively :
|
308
|
-
ua.getBrowser().withClientHints().then(function (browser) {
|
309
|
-
console.log('Using Client-Hints: ', browser);
|
310
|
-
});
|
311
|
-
})();
|
312
|
-
```
|
313
|
-
|
314
|
-
Along with `User-Agent` HTTP header, Chrome also sends this client-hints data by default under `Sec-CH-UA-*` HTTP headers in each request. In server-side development, you can capture this extra information by passing the `req.headers` to `UAParser()` (see examples below). When using `withClientHints()` in nodejs environment and browser without client-hints support (basically anything that's not Chromium-based), it will returns a new object with updated data.
|
315
|
-
|
316
|
-
```js
|
317
|
-
// server-side example
|
318
|
-
|
319
|
-
// Suppose we got a request having these HTTP headers:
|
320
|
-
const request = {
|
321
|
-
headers : {
|
322
|
-
'user-agent' : 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
|
323
|
-
|
324
|
-
'sec-ch-ua-mobile' : '?1',
|
325
|
-
'sec-ch-ua-model' : 'Galaxy S3 Marketing',
|
326
|
-
'sec-ch-ua-platform' : 'Android'
|
327
|
-
}
|
328
|
-
};
|
329
|
-
|
330
|
-
const result1 = UAParser(request.headers); // parse only "user-agent" header
|
331
|
-
const result2 = UAParser(request.headers).withClientHints(); // update with "sec-ch-ua" headers
|
332
|
-
|
333
|
-
console.log(result1.os.name); // "Linux"
|
334
|
-
console.log(result1.device.type); // undefined
|
335
|
-
console.log(result1.device.model); // undefined
|
336
|
-
|
337
|
-
console.log(result2.os.name); // "Android"
|
338
|
-
console.log(result2.device.type); // "mobile"
|
339
|
-
console.log(result2.device.model); // "Galaxy S3 Marketing"
|
340
|
-
|
341
|
-
new UAParser(request.headers)
|
342
|
-
.getBrowser()
|
343
|
-
.withClientHints()
|
344
|
-
.then((browser) => {
|
345
|
-
console.log(browser.toString()); // Chrome 110.0.0.0
|
346
|
-
});
|
347
|
-
```
|
348
|
-
|
349
|
-
## Extending Regex
|
350
|
-
|
351
|
-
If you want to detect something that's not currently provided by UAParser.js (eg: `bots`, specific apps, etc), you can pass a list of regexes to extend internal UAParser.js regexes with your own.
|
352
|
-
|
353
|
-
* `UAParser([uastring,] extensions [,headers:object(since@2.0)])`
|
354
|
-
|
355
|
-
```js
|
356
|
-
// Example:
|
357
|
-
const myOwnListOfBrowsers = [
|
358
|
-
[/(mybrowser)\/([\w\.]+)/i], [UAParser.BROWSER.NAME, UAParser.BROWSER.VERSION, ['type', 'bot']]
|
359
|
-
];
|
360
|
-
|
361
|
-
const myUA = 'Mozilla/5.0 MyBrowser/1.3';
|
362
|
-
|
363
|
-
let myParser = new UAParser({ browser: myOwnListOfBrowsers });
|
364
|
-
|
365
|
-
console.log(myParser.setUA(myUA).getBrowser()); // {name: "MyBrowser", version: "1.3", major: "1", type : "bot"}
|
366
|
-
console.log(myParser.getBrowser().is('bot')); // true
|
367
|
-
|
368
|
-
// Another example:
|
369
|
-
const myOwnListOfDevices = [
|
370
|
-
[/(mytab) ([\w ]+)/i], [UAParser.DEVICE.VENDOR, UAParser.DEVICE.MODEL, [UAParser.DEVICE.TYPE, UAParser.DEVICE.TABLET]],
|
371
|
-
[/(myphone)/i], [UAParser.DEVICE.VENDOR, [UAParser.DEVICE.TYPE, UAParser.DEVICE.MOBILE]]
|
372
|
-
];
|
373
|
-
|
374
|
-
const myUA2 = 'Mozilla/5.0 MyTab 14 Pro Max';
|
375
|
-
|
376
|
-
let myParser2 = new UAParser({
|
377
|
-
browser: myOwnListOfBrowsers,
|
378
|
-
device: myOwnListOfDevices
|
379
|
-
});
|
380
|
-
|
381
|
-
console.log(myParser2.setUA(myUA2).getDevice()); // {vendor: "MyTab", model: "14 Pro Max", type: "tablet"}
|
382
|
-
```
|
383
|
-
|
384
|
-
Some basic extensions (although not very complete at the moment) can also be found under `ua-parser-js/extensions` submodule.
|
385
|
-
|
386
|
-
```js
|
387
|
-
import { UAParser } from 'ua-parser-js';
|
388
|
-
import { Emails } from 'ua-parser-js/extensions';
|
389
|
-
|
390
|
-
const browser = new UAParser(Emails)
|
391
|
-
.setUA('Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0')
|
392
|
-
.getBrowser();
|
393
|
-
|
394
|
-
console.log(browser.name); // Thunderbird
|
395
|
-
```
|
396
|
-
|
397
|
-
|
398
|
-
# Usage
|
399
|
-
|
400
|
-
## Using HTML
|
401
|
-
|
402
|
-
```html
|
403
|
-
<!doctype html>
|
404
|
-
<html>
|
405
|
-
<head>
|
406
|
-
<script src="ua-parser.min.js"></script>
|
407
|
-
<script>
|
408
|
-
|
409
|
-
var uap = new UAParser();
|
410
|
-
console.log(uap.getResult());
|
411
|
-
/*
|
412
|
-
/// This will print an object structured like this:
|
413
|
-
{
|
414
|
-
ua: "",
|
415
|
-
browser: {
|
416
|
-
name: "",
|
417
|
-
version: "",
|
418
|
-
major: ""
|
419
|
-
},
|
420
|
-
engine: {
|
421
|
-
name: "",
|
422
|
-
version: ""
|
423
|
-
},
|
424
|
-
os: {
|
425
|
-
name: "",
|
426
|
-
version: ""
|
427
|
-
},
|
428
|
-
device: {
|
429
|
-
model: "",
|
430
|
-
type: "",
|
431
|
-
vendor: ""
|
432
|
-
},
|
433
|
-
cpu: {
|
434
|
-
architecture: ""
|
435
|
-
}
|
436
|
-
}
|
437
|
-
*/
|
438
|
-
// Default result depends on current window.navigator.userAgent value
|
439
|
-
|
440
|
-
// Now let's try a custom user-agent string as an example
|
441
|
-
var uastring1 = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.2 (KHTML, like Gecko) Ubuntu/11.10 Chromium/15.0.874.106 Chrome/15.0.874.106 Safari/535.2";
|
442
|
-
uap.setUA(uastring1);
|
443
|
-
var result = uap.getResult();
|
444
|
-
// You can also use UAParser constructor directly without having to create an instance:
|
445
|
-
// var ua = UAParser(uastring1);
|
446
|
-
|
447
|
-
console.log(result.browser); // {name: "Chromium", version: "15.0.874.106"}
|
448
|
-
console.log(result.device); // {model: undefined, type: undefined, vendor: undefined}
|
449
|
-
console.log(result.os); // {name: "Ubuntu", version: "11.10"}
|
450
|
-
console.log(result.os.version); // "11.10"
|
451
|
-
console.log(result.engine.name); // "WebKit"
|
452
|
-
console.log(result.cpu.architecture); // "amd64"
|
453
|
-
|
454
|
-
// Do some other tests
|
455
|
-
var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
|
456
|
-
console.log(uap.setUA(uastring2).getBrowser().name); // "Konqueror"
|
457
|
-
console.log(uap.getOS()); // {name: "OpenBSD", version: undefined}
|
458
|
-
console.log(uap.getEngine()); // {name: "KHTML", version: "4.1.4"}
|
459
|
-
|
460
|
-
var uastring3 = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Version/7.1.0.7 Safari/534.11';
|
461
|
-
console.log(uap.setUA(uastring3).getDevice().model); // "PlayBook"
|
462
|
-
console.log(uap.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
|
463
|
-
console.log(uap.getBrowser().name); // "Safari"
|
464
|
-
|
465
|
-
</script>
|
466
|
-
</head>
|
467
|
-
<body>
|
468
|
-
</body>
|
469
|
-
</html>
|
470
|
-
```
|
471
|
-
|
472
|
-
## Using node.js
|
473
|
-
|
474
|
-
Note: Device information is not available in the NodeJS environment.
|
475
|
-
|
476
|
-
```sh
|
477
|
-
$ npm install ua-parser-js
|
478
|
-
```
|
479
|
-
|
480
|
-
```js
|
481
|
-
var http = require('http');
|
482
|
-
var uap = require('ua-parser-js');
|
483
|
-
|
484
|
-
http.createServer(function (req, res) {
|
485
|
-
// get user-agent header
|
486
|
-
var ua = uap(req.headers['user-agent']);
|
487
|
-
|
488
|
-
/* // BEGIN since@2.0 - you can also pass client-hints data to UAParser
|
489
|
-
|
490
|
-
// note: only works in secure context (https:// or localhost or file://)
|
491
|
-
|
492
|
-
var getHighEntropyValues = 'Sec-CH-UA-Full-Version-List, Sec-CH-UA-Mobile, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version, Sec-CH-UA-Arch, Sec-CH-UA-Bitness';
|
493
|
-
res.setHeader('Accept-CH', getHighEntropyValues);
|
494
|
-
res.setHeader('Critical-CH', getHighEntropyValues);
|
495
|
-
|
496
|
-
var ua = uap(req.headers).withClientHints();
|
497
|
-
|
498
|
-
// END since@2.0 */
|
499
|
-
|
500
|
-
// write the result as response
|
501
|
-
res.end(JSON.stringify(ua, null, ' '));
|
502
|
-
})
|
503
|
-
.listen(1337, '127.0.0.1');
|
504
|
-
|
505
|
-
console.log('Server running at http://127.0.0.1:1337/');
|
506
|
-
```
|
507
|
-
|
508
|
-
## Using ES Modules
|
509
|
-
|
510
|
-
```js
|
511
|
-
import { UAParser } from 'ua-parser-js';
|
512
|
-
|
513
|
-
const { browser, cpu, device } = UAParser('Mozilla/5.0 (X11; U; Linux armv7l; en-GB; rv:1.9.2a1pre) Gecko/20090928 Firefox/3.5 Maemo Browser 1.4.1.22 RX-51 N900');
|
514
|
-
|
515
|
-
console.log(browser.name); // Maemo Browser
|
516
|
-
console.log(cpu.is('arm')); // true
|
517
|
-
console.log(device.is('mobile')); // true
|
518
|
-
console.log(device.model); // N900
|
519
|
-
```
|
520
|
-
|
521
|
-
## Using TypeScript
|
522
|
-
|
523
|
-
```sh
|
524
|
-
$ npm install --save @types/ua-parser-js
|
525
|
-
# Download TS type definition from DefinitelyTyped repository:
|
526
|
-
# https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/ua-parser-js
|
527
|
-
```
|
528
|
-
|
529
|
-
## Using jQuery/Zepto ($.ua)
|
530
|
-
|
531
|
-
Although written in vanilla js, this library will automatically detect if jQuery/Zepto is present and create `$.ua` object (with values based on its User-Agent) along with `window.UAParser` constructor. To get/set user-agent you can use: `$.ua.get()` / `$.ua.set(uastring)`.
|
532
|
-
|
533
|
-
```js
|
534
|
-
// Say we are in a browser with default user-agent: 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0':
|
535
|
-
|
536
|
-
// Get the details
|
537
|
-
console.log($.ua.device); // {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"}
|
538
|
-
console.log($.ua.os); // {name: "Android", version: "2.3.4"}
|
539
|
-
console.log($.ua.os.name); // "Android"
|
540
|
-
console.log($.ua.get()); // "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0"
|
541
|
-
|
542
|
-
// Now lets try to reset to another custom user-agent
|
543
|
-
$.ua.set('Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HWI69) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13');
|
544
|
-
|
545
|
-
// Test again
|
546
|
-
console.log($.ua.browser.name); // "Safari"
|
547
|
-
console.log($.ua.engine.name); // "Webkit"
|
548
|
-
console.log($.ua.device); // {vendor: "Motorola", model: "Xoom", type: "tablet"}
|
549
|
-
console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4
|
550
|
-
|
551
|
-
// Add class to <body> tag
|
552
|
-
// <body class="ua-browser-safari ua-devicetype-tablet">
|
553
|
-
$('body').addClass('ua-browser-' + $.ua.browser.name + ' ua-devicetype-' + $.ua.device.type);
|
554
|
-
```
|
555
|
-
|
556
|
-
# Development
|
557
|
-
|
558
|
-
## Backers & Sponsors
|
559
|
-
|
560
|
-
<a href="https://opencollective.com/ua-parser-js"><img src="https://opencollective.com/ua-parser-js/organizations.svg?avatarHeight=64"></a>
|
561
|
-
<a href="https://opencollective.com/ua-parser-js"><img src="https://opencollective.com/ua-parser-js/individuals.svg?avatarHeight=64"></a>
|
562
|
-
|
563
|
-
<a href="https://www.paypal.me/faisalman/"><img src="https://cdn.rawgit.com/twolfson/paypal-github-button/1.0.0/dist/button.svg" height="40"></a>
|
564
|
-
|
565
|
-
## Contributors
|
566
|
-
|
567
|
-
<a href="https://github.com/faisalman/ua-parser-js/graphs/contributors">
|
568
|
-
<img src="https://contrib.rocks/image?repo=faisalman/ua-parser-js" />
|
569
|
-
</a>
|
570
|
-
|
571
|
-
Made with [contributors-img](https://contrib.rocks).
|
572
|
-
|
573
|
-
## How To Contribute
|
574
|
-
|
575
|
-
* Fork and clone this repository
|
576
|
-
* Make some changes as required
|
577
|
-
* Write unit test to showcase its functionality
|
578
|
-
* Run the test suites to make sure it's not breaking anything `$ npm test`
|
579
|
-
* Submit a pull request under `develop` branch
|
580
|
-
|
581
|
-
# License
|
582
|
-
|
583
|
-
MIT License
|
584
|
-
|
585
|
-
Copyright (c) 2012-2023 Faisal Salman <<f@faisalman.com>>
|
586
|
-
|
587
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
588
|
-
of this software and associated documentation files (the "Software"), to deal
|
589
|
-
in the Software without restriction, including without limitation the rights
|
590
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
591
|
-
copies of the Software, and to permit persons to whom the Software is
|
592
|
-
furnished to do so, subject to the following conditions:
|
593
|
-
|
594
|
-
The above copyright notice and this permission notice shall be included in all
|
595
|
-
copies or substantial portions of the Software.
|
596
|
-
|
597
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
598
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
599
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
600
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
601
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
602
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
603
|
-
SOFTWARE.
|