[6a3a178] | 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 data with relatively small footprint (~17KB minified, ~6KB gzipped) that can be used either in browser (client-side) or node.js (server-side).
|
---|
| 16 |
|
---|
| 17 | * Author : Faisal Salman <<f@faisalman.com>>
|
---|
[e29cc2e] | 18 | * Demo : https://faisalman.github.io/ua-parser-js
|
---|
[6a3a178] | 19 | * Source : https://github.com/faisalman/ua-parser-js
|
---|
| 20 |
|
---|
| 21 | # Documentation
|
---|
| 22 |
|
---|
| 23 | ## Constructor
|
---|
| 24 |
|
---|
| 25 | * `new UAParser([uastring][,extensions])`
|
---|
| 26 | * returns new instance
|
---|
| 27 |
|
---|
| 28 | * `UAParser([uastring][,extensions])`
|
---|
| 29 | * returns result object `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
|
---|
| 30 |
|
---|
| 31 | ## Methods
|
---|
| 32 |
|
---|
| 33 | * `getBrowser()`
|
---|
| 34 | * returns `{ name: '', version: '' }`
|
---|
| 35 |
|
---|
| 36 | ```sh
|
---|
| 37 | # Possible 'browser.name':
|
---|
| 38 | 2345Explorer, 360 Browser, Amaya, Android Browser, Arora, Avant, Avast, AVG,
|
---|
| 39 | BIDUBrowser, Baidu, Basilisk, Blazer, Bolt, Brave, Bowser, Camino, Chimera,
|
---|
| 40 | Chrome Headless, Chrome WebView, Chrome, Chromium, Comodo Dragon, Dillo,
|
---|
| 41 | Dolphin, Doris, Edge, Electron, Epiphany, Facebook, Falkon, Fennec, Firebird,
|
---|
| 42 | Firefox [Reality], Flock, Flow, GSA, GoBrowser, ICE Browser, IE, IEMobile, IceApe,
|
---|
| 43 | IceCat, IceDragon, Iceweasel, Instagram, Iridium, Iron, Jasmine, K-Meleon,
|
---|
[e29cc2e] | 44 | Kindle, Klar, Konqueror, LBBROWSER, Line, Links, Lunascape, Lynx, MIUI Browser,
|
---|
[6a3a178] | 45 | Maemo Browser, Maemo, Maxthon, MetaSr Midori, Minimo, Mobile Safari, Mosaic,
|
---|
[e29cc2e] | 46 | Mozilla, NetFront, NetSurf, Netfront, Netscape, NokiaBrowser, Obigo, Oculus Browser,
|
---|
[6a3a178] | 47 | OmniWeb, Opera Coast, Opera [Mini/Mobi/Tablet], PaleMoon, PhantomJS, Phoenix,
|
---|
| 48 | Polaris, Puffin, QQ, QQBrowser, QQBrowserLite, Quark, QupZilla, RockMelt, Safari,
|
---|
| 49 | Sailfish Browser, Samsung Browser, SeaMonkey, Silk, Skyfire, Sleipnir, Slim,
|
---|
[e29cc2e] | 50 | SlimBrowser, Swiftfox, Tesla, Tizen Browser, UCBrowser, UP.Browser, Vivaldi,
|
---|
| 51 | Waterfox, WeChat, Weibo, Yandex, baidu, iCab, w3m, Whale Browser...
|
---|
[6a3a178] | 52 |
|
---|
| 53 | # 'browser.version' determined dynamically
|
---|
| 54 | ```
|
---|
| 55 |
|
---|
| 56 | * `getDevice()`
|
---|
| 57 | * returns `{ model: '', type: '', vendor: '' }`
|
---|
| 58 |
|
---|
| 59 | ```sh
|
---|
| 60 | # Possible 'device.type':
|
---|
| 61 | console, mobile, tablet, smarttv, wearable, embedded
|
---|
| 62 |
|
---|
| 63 | # Possible 'device.vendor':
|
---|
| 64 | Acer, Alcatel, Amazon, Apple, Archos, ASUS, AT&T, BenQ, BlackBerry, Dell,
|
---|
| 65 | Essential, Fairphone, GeeksPhone, Google, HP, HTC, Huawei, Jolla, Lenovo, LG,
|
---|
| 66 | Meizu, Microsoft, Motorola, Nexian, Nintendo, Nokia, Nvidia, OnePlus, OPPO, Ouya,
|
---|
[e29cc2e] | 67 | Palm, Panasonic, Pebble, Polytron, Realme, RIM, Roku, Samsung, Sharp, Siemens,
|
---|
[6a3a178] | 68 | Sony[Ericsson], Sprint, Tesla, Vivo, Vodafone, Xbox, Xiaomi, Zebra, ZTE, ...
|
---|
| 69 |
|
---|
| 70 | # 'device.model' determined dynamically
|
---|
| 71 | ```
|
---|
| 72 |
|
---|
| 73 | * `getEngine()`
|
---|
| 74 | * returns `{ name: '', version: '' }`
|
---|
| 75 |
|
---|
| 76 | ```sh
|
---|
| 77 | # Possible 'engine.name'
|
---|
| 78 | Amaya, Blink, EdgeHTML, Flow, Gecko, Goanna, iCab, KHTML, Links, Lynx, NetFront,
|
---|
| 79 | NetSurf, Presto, Tasman, Trident, w3m, WebKit
|
---|
| 80 |
|
---|
| 81 | # 'engine.version' determined dynamically
|
---|
| 82 | ```
|
---|
| 83 |
|
---|
| 84 | * `getOS()`
|
---|
| 85 | * returns `{ name: '', version: '' }`
|
---|
| 86 |
|
---|
| 87 | ```sh
|
---|
| 88 | # Possible 'os.name'
|
---|
[e29cc2e] | 89 | AIX, Amiga OS, Android[-x86], Arch, Bada, BeOS, BlackBerry, CentOS, Chromium OS,
|
---|
| 90 | Contiki, Fedora, Firefox OS, FreeBSD, Debian, Deepin, DragonFly, elementary OS,
|
---|
| 91 | Fuchsia, Gentoo, GhostBSD, GNU, Haiku, HP-UX, Hurd, iOS, Joli, KaiOS, Linpus, Linspire,
|
---|
| 92 | Linux, Mac OS, Maemo, Mageia, Mandriva, Manjaro, MeeGo, Minix, Mint, Morph OS, NetBSD,
|
---|
| 93 | Nintendo, OpenBSD, OpenVMS, OS/2, Palm, PC-BSD, PCLinuxOS, Plan9, PlayStation, QNX,
|
---|
| 94 | Raspbian, RedHat, RIM Tablet OS, RISC OS, Sabayon, Sailfish, Series40, Slackware, Solaris,
|
---|
| 95 | SUSE, Symbian, Tizen, Ubuntu, Unix, VectorLinux, WebOS, Windows [Phone/Mobile], Zenwalk, ...
|
---|
[6a3a178] | 96 |
|
---|
| 97 | # 'os.version' determined dynamically
|
---|
| 98 | ```
|
---|
| 99 |
|
---|
| 100 | * `getCPU()`
|
---|
| 101 | * returns `{ architecture: '' }`
|
---|
| 102 |
|
---|
| 103 | ```sh
|
---|
| 104 | # Possible 'cpu.architecture'
|
---|
| 105 | 68k, amd64, arm[64/hf], avr, ia[32/64], irix[64], mips[64], pa-risc, ppc, sparc[64]
|
---|
| 106 | ```
|
---|
| 107 |
|
---|
| 108 | * `getResult()`
|
---|
| 109 | * returns `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
|
---|
| 110 |
|
---|
| 111 | * `getUA()`
|
---|
| 112 | * returns UA string of current instance
|
---|
| 113 |
|
---|
| 114 | * `setUA(uastring)`
|
---|
| 115 | * set UA string to be parsed
|
---|
| 116 | * returns current instance
|
---|
| 117 |
|
---|
| 118 | # Usage
|
---|
| 119 |
|
---|
| 120 | ## Using HTML
|
---|
| 121 |
|
---|
| 122 | ```html
|
---|
| 123 | <!doctype html>
|
---|
| 124 | <html>
|
---|
| 125 | <head>
|
---|
| 126 | <script src="ua-parser.min.js"></script>
|
---|
| 127 | <script>
|
---|
| 128 |
|
---|
| 129 | var parser = new UAParser();
|
---|
| 130 | console.log(parser.getResult());
|
---|
| 131 | /*
|
---|
| 132 | /// This will print an object structured like this:
|
---|
| 133 | {
|
---|
| 134 | ua: "",
|
---|
| 135 | browser: {
|
---|
| 136 | name: "",
|
---|
| 137 | version: "",
|
---|
| 138 | major: "" //@deprecated
|
---|
| 139 | },
|
---|
| 140 | engine: {
|
---|
| 141 | name: "",
|
---|
| 142 | version: ""
|
---|
| 143 | },
|
---|
| 144 | os: {
|
---|
| 145 | name: "",
|
---|
| 146 | version: ""
|
---|
| 147 | },
|
---|
| 148 | device: {
|
---|
| 149 | model: "",
|
---|
| 150 | type: "",
|
---|
| 151 | vendor: ""
|
---|
| 152 | },
|
---|
| 153 | cpu: {
|
---|
| 154 | architecture: ""
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | */
|
---|
| 158 | // Default result depends on current window.navigator.userAgent value
|
---|
| 159 |
|
---|
| 160 | // Now let's try a custom user-agent string as an example
|
---|
| 161 | 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";
|
---|
| 162 | parser.setUA(uastring1);
|
---|
| 163 | var result = parser.getResult();
|
---|
| 164 | // You can also use UAParser constructor directly without having to create an instance:
|
---|
| 165 | // var result = UAParser(uastring1);
|
---|
| 166 |
|
---|
| 167 | console.log(result.browser); // {name: "Chromium", version: "15.0.874.106"}
|
---|
| 168 | console.log(result.device); // {model: undefined, type: undefined, vendor: undefined}
|
---|
| 169 | console.log(result.os); // {name: "Ubuntu", version: "11.10"}
|
---|
| 170 | console.log(result.os.version); // "11.10"
|
---|
| 171 | console.log(result.engine.name); // "WebKit"
|
---|
| 172 | console.log(result.cpu.architecture); // "amd64"
|
---|
| 173 |
|
---|
| 174 | // Do some other tests
|
---|
| 175 | var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
|
---|
| 176 | console.log(parser.setUA(uastring2).getBrowser().name); // "Konqueror"
|
---|
| 177 | console.log(parser.getOS()); // {name: "OpenBSD", version: undefined}
|
---|
| 178 | console.log(parser.getEngine()); // {name: "KHTML", version: "4.1.4"}
|
---|
| 179 |
|
---|
| 180 | 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';
|
---|
| 181 | console.log(parser.setUA(uastring3).getDevice().model); // "PlayBook"
|
---|
| 182 | console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
|
---|
| 183 | console.log(parser.getBrowser().name); // "Safari"
|
---|
| 184 |
|
---|
| 185 | </script>
|
---|
| 186 | </head>
|
---|
| 187 | <body>
|
---|
| 188 | </body>
|
---|
| 189 | </html>
|
---|
| 190 | ```
|
---|
| 191 |
|
---|
| 192 | ## Using node.js
|
---|
| 193 |
|
---|
| 194 | ```sh
|
---|
| 195 | $ npm install ua-parser-js
|
---|
| 196 | ```
|
---|
| 197 |
|
---|
| 198 | ```js
|
---|
| 199 | var http = require('http');
|
---|
| 200 | var parser = require('ua-parser-js');
|
---|
| 201 |
|
---|
| 202 | http.createServer(function (req, res) {
|
---|
| 203 | // get user-agent header
|
---|
| 204 | var ua = parser(req.headers['user-agent']);
|
---|
| 205 | // write the result as response
|
---|
| 206 | res.end(JSON.stringify(ua, null, ' '));
|
---|
| 207 | })
|
---|
| 208 | .listen(1337, '127.0.0.1');
|
---|
| 209 |
|
---|
| 210 | console.log('Server running at http://127.0.0.1:1337/');
|
---|
| 211 | ```
|
---|
| 212 |
|
---|
| 213 | ## Using TypeScript
|
---|
| 214 |
|
---|
| 215 | ```sh
|
---|
| 216 | $ npm install --save @types/ua-parser-js
|
---|
| 217 | # Download TS type definition from DefinitelyTyped repository:
|
---|
| 218 | # https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/ua-parser-js
|
---|
| 219 | ```
|
---|
| 220 |
|
---|
| 221 | ## Using jQuery/Zepto ($.ua)
|
---|
| 222 |
|
---|
| 223 | 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)`.
|
---|
| 224 |
|
---|
| 225 | ```js
|
---|
| 226 | // 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':
|
---|
| 227 |
|
---|
| 228 | // Get the details
|
---|
| 229 | console.log($.ua.device); // {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"}
|
---|
| 230 | console.log($.ua.os); // {name: "Android", version: "2.3.4"}
|
---|
| 231 | console.log($.ua.os.name); // "Android"
|
---|
| 232 | 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"
|
---|
| 233 |
|
---|
| 234 | // Now lets try to reset to another custom user-agent
|
---|
| 235 | $.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');
|
---|
| 236 |
|
---|
| 237 | // Test again
|
---|
| 238 | console.log($.ua.browser.name); // "Safari"
|
---|
| 239 | console.log($.ua.engine.name); // "Webkit"
|
---|
| 240 | console.log($.ua.device); // {vendor: "Motorola", model: "Xoom", type: "tablet"}
|
---|
| 241 | console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4
|
---|
| 242 |
|
---|
| 243 | // Add class to <body> tag
|
---|
| 244 | // <body class="ua-browser-safari ua-devicetype-tablet">
|
---|
| 245 | $('body').addClass('ua-browser-' + $.ua.browser.name + ' ua-devicetype-' + $.ua.device.type);
|
---|
| 246 | ```
|
---|
| 247 |
|
---|
| 248 | ## Using Extension
|
---|
| 249 |
|
---|
| 250 | * `UAParser([uastring,] extensions)`
|
---|
| 251 |
|
---|
| 252 | ```js
|
---|
| 253 | // Example:
|
---|
| 254 | var myOwnListOfBrowsers = [
|
---|
| 255 | [/(mybrowser)\/([\w\.]+)/i], [UAParser.BROWSER.NAME, UAParser.BROWSER.VERSION]
|
---|
| 256 | ];
|
---|
| 257 | var myParser = new UAParser({ browser: myOwnListOfBrowsers });
|
---|
| 258 | var myUA = 'Mozilla/5.0 MyBrowser/1.3';
|
---|
| 259 | console.log(myParser.setUA(myUA).getBrowser()); // {name: "MyBrowser", version: "1.3"}
|
---|
| 260 | ```
|
---|
| 261 |
|
---|
| 262 | # Development
|
---|
| 263 |
|
---|
| 264 | ## Sponsors
|
---|
| 265 |
|
---|
| 266 | <a href="https://opencollective.com/ua-parser-js"><img src="https://opencollective.com/ua-parser-js/tiers/backers.svg?avatarHeight=64" height="80"/></a> <a href="https://opencollective.com/ua-parser-js"><img src="https://opencollective.com/ua-parser-js/tiers/sponsors.svg?avatarHeight=64" height="80"/></a>
|
---|
| 267 |
|
---|
| 268 | <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>
|
---|
| 269 |
|
---|
| 270 | ## Contributors
|
---|
| 271 |
|
---|
| 272 | <a href="https://github.com/faisalman/ua-parser-js/graphs/contributors">
|
---|
| 273 | <img src="https://contrib.rocks/image?repo=faisalman/ua-parser-js" />
|
---|
| 274 | </a>
|
---|
| 275 |
|
---|
| 276 | Made with [contributors-img](https://contrib.rocks).
|
---|
| 277 |
|
---|
| 278 | ## How To Contribute
|
---|
| 279 |
|
---|
| 280 | * Fork and clone this repository
|
---|
| 281 | * Make some changes as required
|
---|
| 282 | * Write unit test to showcase its functionality
|
---|
| 283 | * Run the test suites to make sure it's not breaking anything `$ npm test`
|
---|
| 284 | * Submit a pull request under `develop` branch
|
---|
| 285 |
|
---|
| 286 | # License
|
---|
| 287 |
|
---|
| 288 | MIT License
|
---|
| 289 |
|
---|
| 290 | Copyright (c) 2012-2021 Faisal Salman <<f@faisalman.com>>
|
---|
| 291 |
|
---|
| 292 | Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
| 293 | of this software and associated documentation files (the "Software"), to deal
|
---|
| 294 | in the Software without restriction, including without limitation the rights
|
---|
| 295 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 296 | copies of the Software, and to permit persons to whom the Software is
|
---|
| 297 | furnished to do so, subject to the following conditions:
|
---|
| 298 |
|
---|
| 299 | The above copyright notice and this permission notice shall be included in all
|
---|
| 300 | copies or substantial portions of the Software.
|
---|
| 301 |
|
---|
| 302 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
| 303 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
| 304 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
| 305 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
| 306 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
---|
| 307 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
---|
| 308 | SOFTWARE.
|
---|