ua-parser-js 2.0.0-alpha.3 → 2.0.0-beta.1

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