2 * Bowser - a browser detector
3 * https://github.com/ded/bowser
4 * MIT License | (c) Dustin Diaz 2015
7 !function (root, name, definition) {
8 if (typeof module != 'undefined' && module.exports) module.exports = definition()
9 else if (typeof define == 'function' && define.amd) define(name, definition)
10 else root[name] = definition()
11 }(this, 'bowser', function () {
13 * See useragents.js for examples of navigator.userAgent
20 function getFirstMatch(regex) {
21 var match = ua.match(regex);
22 return (match && match.length > 1 && match[1]) || '';
25 function getSecondMatch(regex) {
26 var match = ua.match(regex);
27 return (match && match.length > 1 && match[2]) || '';
30 var iosdevice = getFirstMatch(/(ipod|iphone|ipad)/i).toLowerCase()
31 , likeAndroid = /like android/i.test(ua)
32 , android = !likeAndroid && /android/i.test(ua)
33 , nexusMobile = /nexus\s*[0-6]\s*/i.test(ua)
34 , nexusTablet = !nexusMobile && /nexus\s*[0-9]+/i.test(ua)
35 , chromeos = /CrOS/.test(ua)
36 , silk = /silk/i.test(ua)
37 , sailfish = /sailfish/i.test(ua)
38 , tizen = /tizen/i.test(ua)
39 , webos = /(web|hpw)(o|0)s/i.test(ua)
40 , windowsphone = /windows phone/i.test(ua)
41 , samsungBrowser = /SamsungBrowser/i.test(ua)
42 , windows = !windowsphone && /windows/i.test(ua)
43 , mac = !iosdevice && !silk && /macintosh/i.test(ua)
44 , linux = !android && !sailfish && !tizen && !webos && /linux/i.test(ua)
45 , edgeVersion = getSecondMatch(/edg([ea]|ios)\/(\d+(\.\d+)?)/i)
46 , versionIdentifier = getFirstMatch(/version\/(\d+(\.\d+)?)/i)
47 , tablet = /tablet/i.test(ua) && !/tablet pc/i.test(ua)
48 , mobile = !tablet && /[^-]mobi/i.test(ua)
49 , xbox = /xbox/i.test(ua)
52 if (/opera/i.test(ua)) {
57 , version: versionIdentifier || getFirstMatch(/(?:opera|opr|opios)[\s\/](\d+(\.\d+)?)/i)
59 } else if (/opr\/|opios/i.test(ua)) {
64 , version: getFirstMatch(/(?:opr|opios)[\s\/](\d+(\.\d+)?)/i) || versionIdentifier
67 else if (/SamsungBrowser/i.test(ua)) {
69 name: 'Samsung Internet for Android'
71 , version: versionIdentifier || getFirstMatch(/(?:SamsungBrowser)[\s\/](\d+(\.\d+)?)/i)
74 else if (/Whale/i.test(ua)) {
76 name: 'NAVER Whale browser'
78 , version: getFirstMatch(/(?:whale)[\s\/](\d+(?:\.\d+)+)/i)
81 else if (/MZBrowser/i.test(ua)) {
85 , version: getFirstMatch(/(?:MZBrowser)[\s\/](\d+(?:\.\d+)+)/i)
88 else if (/coast/i.test(ua)) {
92 , version: versionIdentifier || getFirstMatch(/(?:coast)[\s\/](\d+(\.\d+)?)/i)
95 else if (/focus/i.test(ua)) {
99 , version: getFirstMatch(/(?:focus)[\s\/](\d+(?:\.\d+)+)/i)
102 else if (/yabrowser/i.test(ua)) {
104 name: 'Yandex Browser'
106 , version: versionIdentifier || getFirstMatch(/(?:yabrowser)[\s\/](\d+(\.\d+)?)/i)
109 else if (/ucbrowser/i.test(ua)) {
113 , version: getFirstMatch(/(?:ucbrowser)[\s\/](\d+(?:\.\d+)+)/i)
116 else if (/mxios/i.test(ua)) {
120 , version: getFirstMatch(/(?:mxios)[\s\/](\d+(?:\.\d+)+)/i)
123 else if (/epiphany/i.test(ua)) {
127 , version: getFirstMatch(/(?:epiphany)[\s\/](\d+(?:\.\d+)+)/i)
130 else if (/puffin/i.test(ua)) {
134 , version: getFirstMatch(/(?:puffin)[\s\/](\d+(?:\.\d+)?)/i)
137 else if (/sleipnir/i.test(ua)) {
141 , version: getFirstMatch(/(?:sleipnir)[\s\/](\d+(?:\.\d+)+)/i)
144 else if (/k-meleon/i.test(ua)) {
148 , version: getFirstMatch(/(?:k-meleon)[\s\/](\d+(?:\.\d+)+)/i)
151 else if (windowsphone) {
153 name: 'Windows Phone'
154 , osname: 'Windows Phone'
159 result.version = edgeVersion
163 result.version = getFirstMatch(/iemobile\/(\d+(\.\d+)?)/i)
166 else if (/msie|trident/i.test(ua)) {
168 name: 'Internet Explorer'
170 , version: getFirstMatch(/(?:msie |rv:)(\d+(\.\d+)?)/i)
172 } else if (chromeos) {
175 , osname: 'Chrome OS'
179 , version: getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)
181 } else if (/edg([ea]|ios)/i.test(ua)) {
183 name: 'Microsoft Edge'
185 , version: edgeVersion
188 else if (/vivaldi/i.test(ua)) {
192 , version: getFirstMatch(/vivaldi\/(\d+(\.\d+)?)/i) || versionIdentifier
198 , osname: 'Sailfish OS'
200 , version: getFirstMatch(/sailfish\s?browser\/(\d+(\.\d+)?)/i)
203 else if (/seamonkey\//i.test(ua)) {
207 , version: getFirstMatch(/seamonkey\/(\d+(\.\d+)?)/i)
210 else if (/firefox|iceweasel|fxios/i.test(ua)) {
214 , version: getFirstMatch(/(?:firefox|iceweasel|fxios)[ \/](\d+(\.\d+)?)/i)
216 if (/\((mobile|tablet);[^\)]*rv:[\d\.]+\)/i.test(ua)) {
218 result.osname = 'Firefox OS'
225 , version : getFirstMatch(/silk\/(\d+(\.\d+)?)/i)
228 else if (/phantom/i.test(ua)) {
232 , version: getFirstMatch(/phantomjs\/(\d+(\.\d+)?)/i)
235 else if (/slimerjs/i.test(ua)) {
239 , version: getFirstMatch(/slimerjs\/(\d+(\.\d+)?)/i)
242 else if (/blackberry|\bbb\d+/i.test(ua) || /rim\stablet/i.test(ua)) {
245 , osname: 'BlackBerry OS'
247 , version: versionIdentifier || getFirstMatch(/blackberry[\d]+\/(\d+(\.\d+)?)/i)
255 , version: versionIdentifier || getFirstMatch(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i)
257 /touchpad\//i.test(ua) && (result.touchpad = t)
259 else if (/bada/i.test(ua)) {
264 , version: getFirstMatch(/dolfin\/(\d+(\.\d+)?)/i)
272 , version: getFirstMatch(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i) || versionIdentifier
275 else if (/qupzilla/i.test(ua)) {
279 , version: getFirstMatch(/(?:qupzilla)[\s\/](\d+(?:\.\d+)+)/i) || versionIdentifier
282 else if (/chromium/i.test(ua)) {
286 , version: getFirstMatch(/(?:chromium)[\s\/](\d+(?:\.\d+)?)/i) || versionIdentifier
289 else if (/chrome|crios|crmo/i.test(ua)) {
293 , version: getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)
299 , version: versionIdentifier
302 else if (/safari|applewebkit/i.test(ua)) {
307 if (versionIdentifier) {
308 result.version = versionIdentifier
311 else if (iosdevice) {
313 name : iosdevice == 'iphone' ? 'iPhone' : iosdevice == 'ipad' ? 'iPad' : 'iPod'
315 // WTF: version is not part of user agent in web apps
316 if (versionIdentifier) {
317 result.version = versionIdentifier
320 else if(/googlebot/i.test(ua)) {
324 , version: getFirstMatch(/googlebot\/(\d+(\.\d+))/i) || versionIdentifier
329 name: getFirstMatch(/^(.*)\/(.*) /),
330 version: getSecondMatch(/^(.*)\/(.*) /)
334 // set webkit or gecko flag for browsers based on these engines
335 if (!result.msedge && /(apple)?webkit/i.test(ua)) {
336 if (/(apple)?webkit\/537\.36/i.test(ua)) {
337 result.name = result.name || "Blink"
340 result.name = result.name || "Webkit"
343 if (!result.version && versionIdentifier) {
344 result.version = versionIdentifier
346 } else if (!result.opera && /gecko\//i.test(ua)) {
347 result.name = result.name || "Gecko"
349 result.version = result.version || getFirstMatch(/gecko\/(\d+(\.\d+)?)/i)
352 // set OS flags for platforms that have multiple browsers
353 if (!result.windowsphone && (android || result.silk)) {
355 result.osname = 'Android'
356 } else if (!result.windowsphone && iosdevice) {
357 result[iosdevice] = t
359 result.osname = 'iOS'
362 result.osname = 'macOS'
365 result.osname = 'Xbox'
366 } else if (windows) {
368 result.osname = 'Windows'
371 result.osname = 'Linux'
374 function getWindowsVersion (s) {
376 case 'NT': return 'NT'
377 case 'XP': return 'XP'
378 case 'NT 5.0': return '2000'
379 case 'NT 5.1': return 'XP'
380 case 'NT 5.2': return '2003'
381 case 'NT 6.0': return 'Vista'
382 case 'NT 6.1': return '7'
383 case 'NT 6.2': return '8'
384 case 'NT 6.3': return '8.1'
385 case 'NT 10.0': return '10'
386 default: return undefined
390 // OS version extraction
392 if (result.windows) {
393 osVersion = getWindowsVersion(getFirstMatch(/Windows ((NT|XP)( \d\d?.\d)?)/i))
394 } else if (result.windowsphone) {
395 osVersion = getFirstMatch(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i);
396 } else if (result.mac) {
397 osVersion = getFirstMatch(/Mac OS X (\d+([_\.\s]\d+)*)/i);
398 osVersion = osVersion.replace(/[_\s]/g, '.');
399 } else if (iosdevice) {
400 osVersion = getFirstMatch(/os (\d+([_\s]\d+)*) like mac os x/i);
401 osVersion = osVersion.replace(/[_\s]/g, '.');
402 } else if (android) {
403 osVersion = getFirstMatch(/android[ \/-](\d+(\.\d+)*)/i);
404 } else if (result.webos) {
405 osVersion = getFirstMatch(/(?:web|hpw)os\/(\d+(\.\d+)*)/i);
406 } else if (result.blackberry) {
407 osVersion = getFirstMatch(/rim\stablet\sos\s(\d+(\.\d+)*)/i);
408 } else if (result.bada) {
409 osVersion = getFirstMatch(/bada\/(\d+(\.\d+)*)/i);
410 } else if (result.tizen) {
411 osVersion = getFirstMatch(/tizen[\/\s](\d+(\.\d+)*)/i);
414 result.osversion = osVersion;
417 // device type extraction
418 var osMajorVersion = !result.windows && osVersion.split('.')[0];
422 || iosdevice == 'ipad'
423 || (android && (osMajorVersion == 3 || (osMajorVersion >= 4 && !mobile)))
429 || iosdevice == 'iphone'
430 || iosdevice == 'ipod'
440 // Graded Browser Support
441 // http://developer.yahoo.com/yui/articles/gbs
443 (result.msie && result.version >= 10) ||
444 (result.yandexbrowser && result.version >= 15) ||
445 (result.vivaldi && result.version >= 1.0) ||
446 (result.chrome && result.version >= 20) ||
447 (result.samsungBrowser && result.version >= 4) ||
448 (result.whale && compareVersions([result.version, '1.0']) === 1) ||
449 (result.mzbrowser && compareVersions([result.version, '6.0']) === 1) ||
450 (result.focus && compareVersions([result.version, '1.0']) === 1) ||
451 (result.firefox && result.version >= 20.0) ||
452 (result.safari && result.version >= 6) ||
453 (result.opera && result.version >= 10.0) ||
454 (result.ios && result.osversion && result.osversion.split(".")[0] >= 6) ||
455 (result.blackberry && result.version >= 10.1)
456 || (result.chromium && result.version >= 20)
460 else if ((result.msie && result.version < 10) ||
461 (result.chrome && result.version < 20) ||
462 (result.firefox && result.version < 20.0) ||
463 (result.safari && result.version < 6) ||
464 (result.opera && result.version < 10.0) ||
465 (result.ios && result.osversion && result.osversion.split(".")[0] < 6)
466 || (result.chromium && result.version < 20)
474 var bowser = detect(typeof navigator !== 'undefined' ? navigator.userAgent || '' : '')
476 bowser.test = function (browserList) {
477 for (var i = 0; i < browserList.length; ++i) {
478 var browserItem = browserList[i];
479 if (typeof browserItem=== 'string') {
480 if (browserItem in bowser) {
489 * Get version precisions count
492 * getVersionPrecision("1.10.3") // 3
494 * @param {string} version
497 function getVersionPrecision(version) {
498 return version.split(".").length;
502 * Array::map polyfill
505 * @param {Function} iterator
508 function map(arr, iterator) {
510 if (Array.prototype.map) {
511 return Array.prototype.map.call(arr, iterator);
513 for (i = 0; i < arr.length; i++) {
514 result.push(iterator(arr[i]));
520 * Calculate browser version weight
523 * compareVersions(['1.10.2.1', '1.8.2.1.90']) // 1
524 * compareVersions(['1.010.2.1', '1.09.2.1.90']); // 1
525 * compareVersions(['1.10.2.1', '1.10.2.1']); // 0
526 * compareVersions(['1.10.2.1', '1.0800.2']); // -1
528 * @param {Array<String>} versions versions to compare
529 * @return {Number} comparison result
531 function compareVersions(versions) {
532 // 1) get common precision for both versions, for example for "10.0" and "9" it should be 2
533 var precision = Math.max(getVersionPrecision(versions[0]), getVersionPrecision(versions[1]));
534 var chunks = map(versions, function (version) {
535 var delta = precision - getVersionPrecision(version);
537 // 2) "9" -> "9.0" (for precision = 2)
538 version = version + new Array(delta + 1).join(".0");
540 // 3) "9.0" -> ["000000000"", "000000009"]
541 return map(version.split("."), function (chunk) {
542 return new Array(20 - chunk.length).join("0") + chunk;
546 // iterate in reverse order by reversed chunks array
547 while (--precision >= 0) {
548 // 4) compare: "000000009" > "000000010" = false (but "9" > "10" = true)
549 if (chunks[0][precision] > chunks[1][precision]) {
552 else if (chunks[0][precision] === chunks[1][precision]) {
553 if (precision === 0) {
554 // all version chunks are same
565 * Check if browser is unsupported
568 * bowser.isUnsupportedBrowser({
577 * @param {Object} minVersions map of minimal version to browser
578 * @param {Boolean} [strictMode = false] flag to return false if browser wasn't found in map
579 * @param {String} [ua] user agent string
582 function isUnsupportedBrowser(minVersions, strictMode, ua) {
583 var _bowser = bowser;
585 // make strictMode param optional with ua param usage
586 if (typeof strictMode === 'string') {
588 strictMode = void(0);
591 if (strictMode === void(0)) {
595 _bowser = detect(ua);
598 var version = "" + _bowser.version;
599 for (var browser in minVersions) {
600 if (minVersions.hasOwnProperty(browser)) {
601 if (_bowser[browser]) {
602 if (typeof minVersions[browser] !== 'string') {
603 throw new Error('Browser version in the minVersion map should be a string: ' + browser + ': ' + String(minVersions));
606 // browser version and min supported version.
607 return compareVersions([version, minVersions[browser]]) < 0;
612 return strictMode; // not found
616 * Check if browser is supported
618 * @param {Object} minVersions map of minimal version to browser
619 * @param {Boolean} [strictMode = false] flag to return false if browser wasn't found in map
620 * @param {String} [ua] user agent string
623 function check(minVersions, strictMode, ua) {
624 return !isUnsupportedBrowser(minVersions, strictMode, ua);
627 bowser.isUnsupportedBrowser = isUnsupportedBrowser;
628 bowser.compareVersions = compareVersions;
629 bowser.check = check;
632 * Set our detect method to the main bowser object so we can
633 * reuse it to test other user agents.
634 * This is needed to implement future tests.
636 bowser._detect = detect;
639 * Set our detect public method to the main bowser object
640 * This is needed to implement bowser in server side
642 bowser.detect = detect;