選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. # UAParser.js
  2. <img align="right" src="https://raw.githubusercontent.com/faisalman/ua-parser-js/gh-pages/images/logo.png"> A JavaScript-based User-Agent string parser. Can be used either in browser (client-side) or in node.js (server-side) environment. Also available as jQuery/Zepto plugin, Bower/Meteor package, & RequireJS/AMD module. This library aims to identify detailed type of web browser, layout engine, operating system, cpu architecture, and device type/model, entirely from user-agent string with a relatively small footprint (~17KB when minified / ~6KB gzipped). Written in vanilla JavaScript, which means it doesn't require any other library and can be used independently. However, it's not recommended to use this library as browser detection since the result may not be more accurate than using feature detection.
  3. [![Build Status](https://travis-ci.org/faisalman/ua-parser-js.svg?branch=master)](https://travis-ci.org/faisalman/ua-parser-js)
  4. [![NPM downloads](https://img.shields.io/npm/dw/ua-parser-js.svg)](https://www.npmjs.com/package/ua-parser-js)
  5. [![NPM](https://img.shields.io/npm/v/ua-parser-js.svg)](https://www.npmjs.com/package/ua-parser-js)
  6. [![Bower](https://img.shields.io/bower/v/ua-parser-js.svg)](https://bower.io/)
  7. [![CDNJS](https://img.shields.io/cdnjs/v/UAParser.js.svg)](https://cdnjs.com/libraries/UAParser.js)
  8. * Author : Faisal Salman <<f@faisalman.com>>
  9. * Demo : http://faisalman.github.io/ua-parser-js
  10. * Source : https://github.com/faisalman/ua-parser-js
  11. # Constructor
  12. * `new UAParser([uastring][,extensions])`
  13. * returns new instance
  14. * `UAParser([uastring][,extensions])`
  15. * returns result object `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
  16. # Methods
  17. * `getBrowser()`
  18. * returns `{ name: '', version: '' }`
  19. ```sh
  20. # Possible 'browser.name':
  21. 2345Explorer, 360 Browser, Amaya, Android Browser, Arora, Avant, Avast, AVG,
  22. BIDUBrowser, Baidu, Basilisk, Blazer, Bolt, Brave, Bowser, Camino, Chimera,
  23. Chrome Headless, Chrome WebView, Chrome, Chromium, Comodo Dragon, Dillo,
  24. Dolphin, Doris, Edge, Epiphany, Facebook, Falkon, Fennec, Firebird, Firefox,
  25. Flock, GSA, GoBrowser, ICE Browser, IE, IEMobile, IceApe, IceCat, IceDragon,
  26. Iceape, Iceweasel, Iridium, Iron, Jasmine, K-Meleon, Kindle, Konqueror,
  27. LBBROWSER Line, Links, Lunascape, Lynx, MIUI Browser, Maemo Browser, Maemo,
  28. Maxthon, MetaSr Midori, Minimo, Mobile Safari, Mosaic, Mozilla, NetFront,
  29. NetSurf, Netfront, Netscape, NokiaBrowser, Oculus Browser, OmniWeb,
  30. Opera Coast, Opera Mini, Opera Mobi, Opera Tablet, Opera, PaleMoon, PhantomJS,
  31. Phoenix, Polaris, Puffin, QQ, QQBrowser, QQBrowserLite, Quark, QupZilla,
  32. RockMelt, Safari, Sailfish Browser, Samsung Browser, SeaMonkey, Silk, Skyfire,
  33. Sleipnir, Slim, SlimBrowser, Swiftfox, Tizen Browser, UCBrowser, Vivaldi,
  34. Waterfox, WeChat, Yandex, baidu, iCab, w3m, ...
  35. # 'browser.version' determined dynamically
  36. ```
  37. * `getDevice()`
  38. * returns `{ model: '', type: '', vendor: '' }`
  39. ```sh
  40. # Possible 'device.type':
  41. console, mobile, tablet, smarttv, wearable, embedded
  42. # Possible 'device.vendor':
  43. Acer, Alcatel, Amazon, Apple, Archos, Asus, BenQ, BlackBerry, Dell, Essential,
  44. GeeksPhone, Google, HP, HTC, Huawei, Jolla, Lenovo, LG, Meizu, Microsoft, Motorola,
  45. Nexian, Nintendo, Nokia, Nvidia, OnePlus, Ouya, Palm, Panasonic, Pebble, Polytron,
  46. RIM, Samsung, Sharp, Siemens, Sony[Ericsson], Sprint, Xbox, Xiaomi, ZTE, ...
  47. # 'device.model' determined dynamically
  48. ```
  49. * `getEngine()`
  50. * returns `{ name: '', version: '' }`
  51. ```sh
  52. # Possible 'engine.name'
  53. Amaya, Blink, EdgeHTML, Gecko, Goanna, iCab, KHTML, Links, Lynx, NetFront,
  54. NetSurf, Presto, Tasman, Trident, w3m, WebKit
  55. # 'engine.version' determined dynamically
  56. ```
  57. * `getOS()`
  58. * returns `{ name: '', version: '' }`
  59. ```sh
  60. # Possible 'os.name'
  61. AIX, Amiga OS, Android, Arch, Bada, BeOS, BlackBerry, CentOS, Chromium OS,
  62. Contiki, Fedora, Firefox OS, FreeBSD, Debian, DragonFly, Fuchsia, Gentoo, GNU,
  63. Haiku, Hurd, iOS, Joli, KaiOS, Linpus, Linux, Mac OS, Mageia, Mandriva, MeeGo,
  64. Minix, Mint, Morph OS, NetBSD, Nintendo, OpenBSD, OpenVMS, OS/2, Palm, PC-BSD,
  65. PCLinuxOS, Plan9, Playstation, QNX, RedHat, RIM Tablet OS, RISC OS, Sailfish,
  66. Series40, Slackware, Solaris, SUSE, Symbian, Tizen, Ubuntu, Unix, VectorLinux,
  67. WebOS, Windows [Phone/Mobile], Zenwalk, ...
  68. # 'os.version' determined dynamically
  69. ```
  70. * `getCPU()`
  71. * returns `{ architecture: '' }`
  72. ```sh
  73. # Possible 'cpu.architecture'
  74. 68k, amd64, arm[64], avr, ia[32/64], irix[64], mips[64], pa-risc, ppc, sparc[64]
  75. ```
  76. * `getResult()`
  77. * returns `{ ua: '', browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
  78. * `getUA()`
  79. * returns UA string of current instance
  80. * `setUA(uastring)`
  81. * set UA string to parse
  82. * returns current instance
  83. # Example
  84. ```html
  85. <!doctype html>
  86. <html>
  87. <head>
  88. <script type="text/javascript" src="ua-parser.min.js"></script>
  89. <script type="text/javascript">
  90. var parser = new UAParser();
  91. // by default it takes ua string from current browser's window.navigator.userAgent
  92. console.log(parser.getResult());
  93. /*
  94. /// this will print an object structured like this:
  95. {
  96. ua: "",
  97. browser: {
  98. name: "",
  99. version: ""
  100. },
  101. engine: {
  102. name: "",
  103. version: ""
  104. },
  105. os: {
  106. name: "",
  107. version: ""
  108. },
  109. device: {
  110. model: "",
  111. type: "",
  112. vendor: ""
  113. },
  114. cpu: {
  115. architecture: ""
  116. }
  117. }
  118. */
  119. // let's test a custom user-agent string as an example
  120. var uastring = "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";
  121. parser.setUA(uastring);
  122. var result = parser.getResult();
  123. // this will also produce the same result (without instantiation):
  124. // var result = UAParser(uastring);
  125. console.log(result.browser); // {name: "Chromium", version: "15.0.874.106"}
  126. console.log(result.device); // {model: undefined, type: undefined, vendor: undefined}
  127. console.log(result.os); // {name: "Ubuntu", version: "11.10"}
  128. console.log(result.os.version); // "11.10"
  129. console.log(result.engine.name); // "WebKit"
  130. console.log(result.cpu.architecture); // "amd64"
  131. // do some other tests
  132. var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
  133. console.log(parser.setUA(uastring2).getBrowser().name); // "Konqueror"
  134. console.log(parser.getOS()); // {name: "OpenBSD", version: undefined}
  135. console.log(parser.getEngine()); // {name: "KHTML", version: "4.1.4"}
  136. 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';
  137. console.log(parser.setUA(uastring3).getDevice().model); // "PlayBook"
  138. console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"}
  139. console.log(parser.getBrowser().name); // "Safari"
  140. </script>
  141. </head>
  142. <body>
  143. </body>
  144. </html>
  145. ```
  146. ## Using node.js
  147. ```sh
  148. $ npm install ua-parser-js
  149. ```
  150. ```js
  151. var http = require('http');
  152. var parser = require('ua-parser-js');
  153. http.createServer(function (req, res) {
  154. // get user-agent header
  155. var ua = parser(req.headers['user-agent']);
  156. // write the result as response
  157. res.end(JSON.stringify(ua, null, ' '));
  158. })
  159. .listen(1337, '127.0.0.1');
  160. console.log('Server running at http://127.0.0.1:1337/');
  161. ```
  162. ## Using requirejs
  163. ```js
  164. requirejs.config({
  165. baseUrl : 'js/lib', // path to your script directory
  166. paths : {
  167. 'ua-parser-js' : 'ua-parser.min'
  168. }
  169. });
  170. requirejs(['ua-parser-js'], function(UAParser) {
  171. var parser = new UAParser();
  172. console.log(parser.getResult());
  173. });
  174. ```
  175. ## Using CDN
  176. ```html
  177. <script src="https://cdn.jsdelivr.net/npm/ua-parser-js@0/dist/ua-parser.min.js"></script>
  178. ```
  179. ## Using bower
  180. ```sh
  181. $ bower install ua-parser-js
  182. ```
  183. ## Using meteor
  184. ```sh
  185. $ meteor add faisalman:ua-parser-js
  186. ```
  187. ## Using TypeScript
  188. ```sh
  189. $ npm install --save @types/ua-parser-js
  190. # Download TS type definition from DefinitelyTyped repository:
  191. # https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/ua-parser-js
  192. ```
  193. ## Using jQuery/Zepto ($.ua)
  194. Although written in vanilla js (which means it doesn't depends on jQuery), this library will automatically detect if jQuery/Zepto is present and create `$.ua` object based on browser's user-agent (although in case you need, `window.UAParser` constructor is still present). To get/set user-agent you can use: `$.ua.get()` / `$.ua.set(uastring)`.
  195. ```js
  196. // In 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':
  197. // Do some tests
  198. console.log($.ua.device); // {vendor: "HTC", model: "Evo Shift 4G", type: "mobile"}
  199. console.log($.ua.os); // {name: "Android", version: "2.3.4"}
  200. console.log($.ua.os.name); // "Android"
  201. 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"
  202. // reset to custom user-agent
  203. $.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');
  204. // Test again
  205. console.log($.ua.browser.name); // "Safari"
  206. console.log($.ua.engine.name); // "Webkit"
  207. console.log($.ua.device); // {vendor: "Motorola", model: "Xoom", type: "tablet"}
  208. console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4
  209. // Add class to <body> tag
  210. // <body class="ua-browser-safari ua-devicetype-tablet">
  211. $('body').addClass('ua-browser-' + $.ua.browser.name + ' ua-devicetype-' + $.ua.device.type);
  212. ```
  213. ## Extending regex patterns
  214. * `UAParser([uastring,] extensions)`
  215. Pass your own regexes to extend the limited matching rules.
  216. ```js
  217. // Example:
  218. var myOwnRegex = [[/(myownbrowser)\/([\w\.]+)/i], [UAParser.BROWSER.NAME, UAParser.BROWSER.VERSION]];
  219. var myParser = new UAParser({ browser: myOwnRegex });
  220. var uaString = 'Mozilla/5.0 MyOwnBrowser/1.3';
  221. console.log(myParser.setUA(uaString).getBrowser()); // {name: "MyOwnBrowser", version: "1.3"}
  222. ```
  223. # Development
  224. ## Contribute
  225. * Fork and clone this repository
  226. * Make some changes as required
  227. * Write a unit test to showcase your feature
  228. * Run the test suites to make sure the changes you made didn't break anything `$ npm run test`
  229. * Commit and push to your own repository
  230. * Submit a pull request to this repository under `develop` branch
  231. * Profit? $$$
  232. ## Build
  233. Build a minified & packed script
  234. ```sh
  235. $ npm run build
  236. ```
  237. # Donate
  238. Do you use & like UAParser.js but you don’t find a way to show some love? If yes, please consider donating to support this project. Otherwise, no worries, regardless of whether there is support or not, I will keep maintaining this project. Still, if you buy me a cup of coffee I would be more than happy though :)
  239. [![Support via PayPal](https://cdn.rawgit.com/twolfson/paypal-github-button/1.0.0/dist/button.svg)](https://www.paypal.me/faisalman/)
  240. # License
  241. MIT License
  242. Copyright (c) 2012-2019 Faisal Salman <<f@faisalman.com>>
  243. Permission is hereby granted, free of charge, to any person obtaining a copy
  244. of this software and associated documentation files (the "Software"), to deal
  245. in the Software without restriction, including without limitation the rights
  246. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  247. copies of the Software, and to permit persons to whom the Software is
  248. furnished to do so, subject to the following conditions:
  249. The above copyright notice and this permission notice shall be included in all
  250. copies or substantial portions of the Software.
  251. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  252. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  253. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  254. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  255. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  256. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  257. SOFTWARE.