ua-parser-js 1.0.35 → 2.0.0-alpha.2
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/dist/ua-parser.min.js +3 -3
- package/dist/ua-parser.pack.js +3 -3
- package/license.md +1 -1
- package/package.json +24 -4
- package/readme.md +277 -55
- package/src/enum/ua-parser-enum.js +101 -0
- package/src/enum/ua-parser-enum.mjs +105 -0
- package/src/extension/ua-parser-extension.js +120 -0
- package/src/extension/ua-parser-extension.mjs +124 -0
- package/src/ua-parser.js +430 -196
- package/src/ua-parser.mjs +1126 -0
package/readme.md
CHANGED
@@ -12,7 +12,7 @@
|
|
12
12
|
|
13
13
|
# UAParser.js
|
14
14
|
|
15
|
-
JavaScript library to detect Browser, Engine, OS, CPU, and Device type/model from User-Agent
|
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
16
|
|
17
17
|
* Author : Faisal Salman <<f@faisalman.com>>
|
18
18
|
* Demo : https://faisalman.github.io/ua-parser-js
|
@@ -26,50 +26,53 @@ JavaScript library to detect Browser, Engine, OS, CPU, and Device type/model fro
|
|
26
26
|
</thead>
|
27
27
|
<tbody>
|
28
28
|
<tr>
|
29
|
-
<td align="center" width="200px" rowspan="
|
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
30
|
<td align="left" width="400px"><a href="https://www.npmjs.com/package/@51degrees/ua-parser-js">↗ @51degrees/ua-parser-js</a></td>
|
31
31
|
</tr>
|
32
32
|
<tr>
|
33
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
34
|
</td>
|
35
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>
|
36
41
|
</tbody>
|
37
42
|
</table>
|
38
43
|
|
39
44
|
---
|
40
45
|
|
41
|
-
#
|
42
|
-
|
43
|
-
typeof `user-agent` "string".
|
46
|
+
# Version 2.0
|
47
|
+
What's new & breaking, please read [CHANGELOG](changelog.md) before upgrading.
|
44
48
|
|
45
|
-
|
49
|
+
# Documentation
|
50
|
+
### UAParser([user-agent:string][,extensions:object][,headers:object(since@2.0)])
|
46
51
|
|
47
|
-
In
|
48
|
-
Usually you can find the user agent in:
|
49
|
-
`request.headers["user-agent"]`.
|
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"]`.
|
50
53
|
|
51
54
|
|
52
55
|
## Constructor
|
53
|
-
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.
|
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.
|
54
57
|
Like so:
|
55
|
-
* `new UAParser([
|
58
|
+
* `new UAParser([user-agent:string][,extensions:object][,headers:object(since@2.0)])`
|
56
59
|
```js
|
57
|
-
let parser = new UAParser("user-agent"); // you need to pass the user-agent for nodejs
|
60
|
+
let parser = new UAParser("your user-agent here"); // you need to pass the user-agent for nodejs
|
58
61
|
console.log(parser); // {}
|
59
62
|
let parserResults = parser.getResult();
|
60
63
|
console.log(parserResults);
|
61
64
|
/** {
|
62
|
-
"ua": "",
|
63
|
-
"browser": {},
|
64
|
-
"engine": {},
|
65
|
-
"os": {},
|
66
|
-
"device": {},
|
67
|
-
"cpu": {}
|
65
|
+
"ua" : "",
|
66
|
+
"browser" : {},
|
67
|
+
"engine" : {},
|
68
|
+
"os" : {},
|
69
|
+
"device" : {},
|
70
|
+
"cpu" : {}
|
68
71
|
} */
|
69
72
|
```
|
70
73
|
|
71
74
|
When you call UAParser without the `new` keyword, it will automatically call `getResult()` function and return the parsed results.
|
72
|
-
* `UAParser([
|
75
|
+
* `UAParser([user-agent:string][,extensions:object][,headers:object(since@2.0)])`
|
73
76
|
* returns result object `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
|
74
77
|
|
75
78
|
## Methods
|
@@ -87,7 +90,6 @@ The methods are self explanatory, here's a small overview on all the available m
|
|
87
90
|
* `getUA()` - returns the user-agent string.
|
88
91
|
* `setUA(user-agent)` - set a custom user-agent to be parsed.
|
89
92
|
|
90
|
-
|
91
93
|
---
|
92
94
|
|
93
95
|
* `getResult()`
|
@@ -100,19 +102,19 @@ The methods are self explanatory, here's a small overview on all the available m
|
|
100
102
|
# Possible 'browser.name':
|
101
103
|
2345Explorer, 360 Browser, Amaya, Android Browser, Arora, Avant, Avast, AVG,
|
102
104
|
BIDUBrowser, Baidu, Basilisk, Blazer, Bolt, Brave, Bowser, Camino, Chimera,
|
103
|
-
Chrome Headless
|
105
|
+
[Mobile] Chrome [Headless/WebView], Chromium, Cobalt, Comodo Dragon, Dillo,
|
104
106
|
Dolphin, Doris, DuckDuckGo, Edge, Electron, Epiphany, Facebook, Falkon, Fennec,
|
105
|
-
Firebird, Firefox [Focus/Reality], Flock, Flow, GSA, GoBrowser,
|
107
|
+
Firebird, [Mobile] Firefox [Focus/Reality], Flock, Flow, GSA, GoBrowser, HeyTap,
|
106
108
|
Huawei Browser, ICE Browser, IE, IEMobile, IceApe, IceCat, IceDragon, Iceweasel,
|
107
109
|
Instagram, Iridium, Iron, Jasmine, Kakao[Story/Talk], K-Meleon, Kindle, Klar,
|
108
|
-
Konqueror, LBBROWSER, Line, LinkedIn, Links, Lunascape, Lynx, MIUI Browser,
|
109
|
-
Maemo, Maxthon, MetaSr Midori, Minimo,
|
110
|
+
Konqueror, LBBROWSER, Line, LinkedIn, Links, Lunascape, Lynx, MIUI Browser,
|
111
|
+
Maemo Browser, Maemo, Maxthon, MetaSr Midori, Minimo, Mosaic, Mozilla, NetFront,
|
110
112
|
NetSurf, Netfront, Netscape, NokiaBrowser, Obigo, Oculus Browser, OmniWeb,
|
111
113
|
Opera Coast, Opera [Mini/Mobi/Tablet], PaleMoon, PhantomJS, Phoenix, Polaris,
|
112
|
-
Puffin, QQ, QQBrowser, QQBrowserLite, Quark, QupZilla, RockMelt, Safari,
|
114
|
+
Puffin, QQ, QQBrowser, QQBrowserLite, Quark, QupZilla, RockMelt, [Mobile] Safari,
|
113
115
|
Sailfish Browser, Samsung Browser, SeaMonkey, Silk, Skyfire, Sleipnir, Slim,
|
114
116
|
SlimBrowser, Swiftfox, Tesla, TikTok, Tizen Browser, UCBrowser, UP.Browser, Viera,
|
115
|
-
Vivaldi, Waterfox, WeChat, Weibo, Yandex, baidu, iCab, w3m, Whale Browser...
|
117
|
+
Vivaldi, Waterfox, WeChat, Weibo, Yandex, baidu, iCab, w3m, Whale Browser, ...
|
116
118
|
|
117
119
|
# 'browser.version' determined dynamically
|
118
120
|
```
|
@@ -185,6 +187,214 @@ Zenwalk, ...
|
|
185
187
|
* set UA string to be parsed
|
186
188
|
* returns current instance
|
187
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
|
+
|
188
398
|
# Usage
|
189
399
|
|
190
400
|
## Using HTML
|
@@ -196,8 +406,8 @@ Zenwalk, ...
|
|
196
406
|
<script src="ua-parser.min.js"></script>
|
197
407
|
<script>
|
198
408
|
|
199
|
-
var
|
200
|
-
console.log(
|
409
|
+
var uap = new UAParser();
|
410
|
+
console.log(uap.getResult());
|
201
411
|
/*
|
202
412
|
/// This will print an object structured like this:
|
203
413
|
{
|
@@ -205,7 +415,7 @@ Zenwalk, ...
|
|
205
415
|
browser: {
|
206
416
|
name: "",
|
207
417
|
version: "",
|
208
|
-
major: ""
|
418
|
+
major: ""
|
209
419
|
},
|
210
420
|
engine: {
|
211
421
|
name: "",
|
@@ -229,10 +439,10 @@ Zenwalk, ...
|
|
229
439
|
|
230
440
|
// Now let's try a custom user-agent string as an example
|
231
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";
|
232
|
-
|
233
|
-
var result =
|
442
|
+
uap.setUA(uastring1);
|
443
|
+
var result = uap.getResult();
|
234
444
|
// You can also use UAParser constructor directly without having to create an instance:
|
235
|
-
// var
|
445
|
+
// var ua = UAParser(uastring1);
|
236
446
|
|
237
447
|
console.log(result.browser); // {name: "Chromium", version: "15.0.874.106"}
|
238
448
|
console.log(result.device); // {model: undefined, type: undefined, vendor: undefined}
|
@@ -243,14 +453,14 @@ Zenwalk, ...
|
|
243
453
|
|
244
454
|
// Do some other tests
|
245
455
|
var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
|
246
|
-
console.log(
|
247
|
-
console.log(
|
248
|
-
console.log(
|
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"}
|
249
459
|
|
250
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';
|
251
|
-
console.log(
|
252
|
-
console.log(
|
253
|
-
console.log(
|
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"
|
254
464
|
|
255
465
|
</script>
|
256
466
|
</head>
|
@@ -269,11 +479,24 @@ $ npm install ua-parser-js
|
|
269
479
|
|
270
480
|
```js
|
271
481
|
var http = require('http');
|
272
|
-
var
|
482
|
+
var uap = require('ua-parser-js');
|
273
483
|
|
274
484
|
http.createServer(function (req, res) {
|
275
485
|
// get user-agent header
|
276
|
-
var ua =
|
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
|
+
|
277
500
|
// write the result as response
|
278
501
|
res.end(JSON.stringify(ua, null, ' '));
|
279
502
|
})
|
@@ -282,6 +505,19 @@ http.createServer(function (req, res) {
|
|
282
505
|
console.log('Server running at http://127.0.0.1:1337/');
|
283
506
|
```
|
284
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
|
+
|
285
521
|
## Using TypeScript
|
286
522
|
|
287
523
|
```sh
|
@@ -317,20 +553,6 @@ console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4
|
|
317
553
|
$('body').addClass('ua-browser-' + $.ua.browser.name + ' ua-devicetype-' + $.ua.device.type);
|
318
554
|
```
|
319
555
|
|
320
|
-
## Using Extension
|
321
|
-
|
322
|
-
* `UAParser([uastring,] extensions)`
|
323
|
-
|
324
|
-
```js
|
325
|
-
// Example:
|
326
|
-
var myOwnListOfBrowsers = [
|
327
|
-
[/(mybrowser)\/([\w\.]+)/i], [UAParser.BROWSER.NAME, UAParser.BROWSER.VERSION]
|
328
|
-
];
|
329
|
-
var myParser = new UAParser({ browser: myOwnListOfBrowsers });
|
330
|
-
var myUA = 'Mozilla/5.0 MyBrowser/1.3';
|
331
|
-
console.log(myParser.setUA(myUA).getBrowser()); // {name: "MyBrowser", version: "1.3"}
|
332
|
-
```
|
333
|
-
|
334
556
|
# Development
|
335
557
|
|
336
558
|
## Backers & Sponsors
|
@@ -360,7 +582,7 @@ Made with [contributors-img](https://contrib.rocks).
|
|
360
582
|
|
361
583
|
MIT License
|
362
584
|
|
363
|
-
Copyright (c) 2012-
|
585
|
+
Copyright (c) 2012-2023 Faisal Salman <<f@faisalman.com>>
|
364
586
|
|
365
587
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
366
588
|
of this software and associated documentation files (the "Software"), to deal
|
@@ -0,0 +1,101 @@
|
|
1
|
+
///////////////////////////////////////////////
|
2
|
+
/* Enums for UAParser.js v2.0.0-alpha.2
|
3
|
+
https://github.com/faisalman/ua-parser-js
|
4
|
+
Author: Faisal Salman <f@faisalman.com>
|
5
|
+
MIT License */
|
6
|
+
//////////////////////////////////////////////
|
7
|
+
|
8
|
+
const BrowserName = Object.freeze({
|
9
|
+
CHROME : 'Chrome',
|
10
|
+
EDGE : 'Edge',
|
11
|
+
SAFARI : 'Safari',
|
12
|
+
FIREFOX : 'Firefox',
|
13
|
+
OPERA : 'Opera',
|
14
|
+
MOBILE_CHROME : 'Mobile Chrome',
|
15
|
+
MOBILE_SAFARI : 'Mobile Safari',
|
16
|
+
MOBILE_FIREFOX : 'Mobile Firefox',
|
17
|
+
ANDROID_BROWSER : 'Android Browser'
|
18
|
+
|
19
|
+
// TODO : test!
|
20
|
+
});
|
21
|
+
|
22
|
+
const CPUArch = Object.freeze({
|
23
|
+
IA32 : 'ia32',
|
24
|
+
AMD64 : 'amd64',
|
25
|
+
IA64 : 'ia64',
|
26
|
+
ARM : 'arm',
|
27
|
+
ARM64 : 'arm64',
|
28
|
+
ARMHF : 'armhf',
|
29
|
+
_68K : '68k',
|
30
|
+
AVR : 'avr',
|
31
|
+
IRIX : 'irix',
|
32
|
+
IRIX64 : 'irix64',
|
33
|
+
MIPS : 'mips',
|
34
|
+
MIPS64 : 'mips64',
|
35
|
+
PPC : 'ppc',
|
36
|
+
SPARC : 'sparc',
|
37
|
+
SPARC64 : 'sparc64'
|
38
|
+
});
|
39
|
+
|
40
|
+
const DeviceType = Object.freeze({
|
41
|
+
MOBILE : 'mobile',
|
42
|
+
TABLET : 'tablet',
|
43
|
+
SMARTTV : 'smarttv',
|
44
|
+
CONSOLE : 'console',
|
45
|
+
WEARABLE: 'wearable',
|
46
|
+
EMBEDDED: 'embedded'
|
47
|
+
});
|
48
|
+
|
49
|
+
const DeviceVendor = Object.freeze({
|
50
|
+
APPLE : 'Apple',
|
51
|
+
SAMSUNG : 'Samsung',
|
52
|
+
HUAWEI : 'Huawei',
|
53
|
+
XIAOMI : 'Xiaomi',
|
54
|
+
OPPO : 'OPPO',
|
55
|
+
VIVO : 'Vivo',
|
56
|
+
REALME : 'Realme',
|
57
|
+
LENOVO : 'Lenovo',
|
58
|
+
LG : 'LG'
|
59
|
+
|
60
|
+
// TODO : test!
|
61
|
+
});
|
62
|
+
|
63
|
+
const EngineName = Object.freeze({
|
64
|
+
AMAYA : 'Amaya',
|
65
|
+
BLINK : 'Blink',
|
66
|
+
EDGEHTML: 'EdgeHTML',
|
67
|
+
FLOW : 'Flow',
|
68
|
+
GECKO : 'Gecko',
|
69
|
+
GOANNA : 'Goanna',
|
70
|
+
ICAB : 'iCab',
|
71
|
+
LIBWEB : 'LibWeb',
|
72
|
+
KHTML : 'KHTML',
|
73
|
+
LINKS : 'Links',
|
74
|
+
LYNX : 'Lynx',
|
75
|
+
NETFRONT: 'NetFront',
|
76
|
+
NETSURF : 'NetSurf',
|
77
|
+
PRESTO : 'Presto',
|
78
|
+
TASMAN : 'Tasman',
|
79
|
+
TRIDENT : 'Trident',
|
80
|
+
W3M : 'w3m',
|
81
|
+
WEBKIT : 'WebKit'
|
82
|
+
});
|
83
|
+
|
84
|
+
const OSName = Object.freeze({
|
85
|
+
WINDOWS : 'Windows',
|
86
|
+
LINUX : 'Linux',
|
87
|
+
MACOS : 'macOS',
|
88
|
+
IOS : 'iOS',
|
89
|
+
ANDROID : 'Android'
|
90
|
+
|
91
|
+
// TODO : test!
|
92
|
+
});
|
93
|
+
|
94
|
+
module.exports = {
|
95
|
+
BrowserName,
|
96
|
+
CPUArch,
|
97
|
+
DeviceType,
|
98
|
+
DeviceVendor,
|
99
|
+
EngineName,
|
100
|
+
OSName
|
101
|
+
}
|
@@ -0,0 +1,105 @@
|
|
1
|
+
// Generated ESM version of UAParser.js enums
|
2
|
+
// DO NOT EDIT THIS FILE!
|
3
|
+
// Source: /src/enum/ua-parser-enum.js
|
4
|
+
|
5
|
+
///////////////////////////////////////////////
|
6
|
+
/* Enums for UAParser.js v2.0.0-alpha.2
|
7
|
+
https://github.com/faisalman/ua-parser-js
|
8
|
+
Author: Faisal Salman <f@faisalman.com>
|
9
|
+
MIT License */
|
10
|
+
//////////////////////////////////////////////
|
11
|
+
|
12
|
+
const BrowserName = Object.freeze({
|
13
|
+
CHROME : 'Chrome',
|
14
|
+
EDGE : 'Edge',
|
15
|
+
SAFARI : 'Safari',
|
16
|
+
FIREFOX : 'Firefox',
|
17
|
+
OPERA : 'Opera',
|
18
|
+
MOBILE_CHROME : 'Mobile Chrome',
|
19
|
+
MOBILE_SAFARI : 'Mobile Safari',
|
20
|
+
MOBILE_FIREFOX : 'Mobile Firefox',
|
21
|
+
ANDROID_BROWSER : 'Android Browser'
|
22
|
+
|
23
|
+
// TODO : test!
|
24
|
+
});
|
25
|
+
|
26
|
+
const CPUArch = Object.freeze({
|
27
|
+
IA32 : 'ia32',
|
28
|
+
AMD64 : 'amd64',
|
29
|
+
IA64 : 'ia64',
|
30
|
+
ARM : 'arm',
|
31
|
+
ARM64 : 'arm64',
|
32
|
+
ARMHF : 'armhf',
|
33
|
+
_68K : '68k',
|
34
|
+
AVR : 'avr',
|
35
|
+
IRIX : 'irix',
|
36
|
+
IRIX64 : 'irix64',
|
37
|
+
MIPS : 'mips',
|
38
|
+
MIPS64 : 'mips64',
|
39
|
+
PPC : 'ppc',
|
40
|
+
SPARC : 'sparc',
|
41
|
+
SPARC64 : 'sparc64'
|
42
|
+
});
|
43
|
+
|
44
|
+
const DeviceType = Object.freeze({
|
45
|
+
MOBILE : 'mobile',
|
46
|
+
TABLET : 'tablet',
|
47
|
+
SMARTTV : 'smarttv',
|
48
|
+
CONSOLE : 'console',
|
49
|
+
WEARABLE: 'wearable',
|
50
|
+
EMBEDDED: 'embedded'
|
51
|
+
});
|
52
|
+
|
53
|
+
const DeviceVendor = Object.freeze({
|
54
|
+
APPLE : 'Apple',
|
55
|
+
SAMSUNG : 'Samsung',
|
56
|
+
HUAWEI : 'Huawei',
|
57
|
+
XIAOMI : 'Xiaomi',
|
58
|
+
OPPO : 'OPPO',
|
59
|
+
VIVO : 'Vivo',
|
60
|
+
REALME : 'Realme',
|
61
|
+
LENOVO : 'Lenovo',
|
62
|
+
LG : 'LG'
|
63
|
+
|
64
|
+
// TODO : test!
|
65
|
+
});
|
66
|
+
|
67
|
+
const EngineName = Object.freeze({
|
68
|
+
AMAYA : 'Amaya',
|
69
|
+
BLINK : 'Blink',
|
70
|
+
EDGEHTML: 'EdgeHTML',
|
71
|
+
FLOW : 'Flow',
|
72
|
+
GECKO : 'Gecko',
|
73
|
+
GOANNA : 'Goanna',
|
74
|
+
ICAB : 'iCab',
|
75
|
+
LIBWEB : 'LibWeb',
|
76
|
+
KHTML : 'KHTML',
|
77
|
+
LINKS : 'Links',
|
78
|
+
LYNX : 'Lynx',
|
79
|
+
NETFRONT: 'NetFront',
|
80
|
+
NETSURF : 'NetSurf',
|
81
|
+
PRESTO : 'Presto',
|
82
|
+
TASMAN : 'Tasman',
|
83
|
+
TRIDENT : 'Trident',
|
84
|
+
W3M : 'w3m',
|
85
|
+
WEBKIT : 'WebKit'
|
86
|
+
});
|
87
|
+
|
88
|
+
const OSName = Object.freeze({
|
89
|
+
WINDOWS : 'Windows',
|
90
|
+
LINUX : 'Linux',
|
91
|
+
MACOS : 'macOS',
|
92
|
+
IOS : 'iOS',
|
93
|
+
ANDROID : 'Android'
|
94
|
+
|
95
|
+
// TODO : test!
|
96
|
+
});
|
97
|
+
|
98
|
+
export {
|
99
|
+
BrowserName,
|
100
|
+
CPUArch,
|
101
|
+
DeviceType,
|
102
|
+
DeviceVendor,
|
103
|
+
EngineName,
|
104
|
+
OSName
|
105
|
+
}
|