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)os/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 (/coast/i.test(ua)) {
78 , version: versionIdentifier || getFirstMatch(/(?:coast)[\s\/](\d+(\.\d+)?)/i)
81 else if (/yabrowser/i.test(ua)) {
83 name: 'Yandex Browser'
85 , version: versionIdentifier || getFirstMatch(/(?:yabrowser)[\s\/](\d+(\.\d+)?)/i)
88 else if (/ucbrowser/i.test(ua)) {
92 , version: getFirstMatch(/(?:ucbrowser)[\s\/](\d+(?:\.\d+)+)/i)
95 else if (/mxios/i.test(ua)) {
99 , version: getFirstMatch(/(?:mxios)[\s\/](\d+(?:\.\d+)+)/i)
102 else if (/epiphany/i.test(ua)) {
106 , version: getFirstMatch(/(?:epiphany)[\s\/](\d+(?:\.\d+)+)/i)
109 else if (/puffin/i.test(ua)) {
113 , version: getFirstMatch(/(?:puffin)[\s\/](\d+(?:\.\d+)?)/i)
116 else if (/sleipnir/i.test(ua)) {
120 , version: getFirstMatch(/(?:sleipnir)[\s\/](\d+(?:\.\d+)+)/i)
123 else if (/k-meleon/i.test(ua)) {
127 , version: getFirstMatch(/(?:k-meleon)[\s\/](\d+(?:\.\d+)+)/i)
130 else if (windowsphone) {
132 name: 'Windows Phone'
133 , osname: 'Windows Phone'
138 result.version = edgeVersion
142 result.version = getFirstMatch(/iemobile\/(\d+(\.\d+)?)/i)
145 else if (/msie|trident/i.test(ua)) {
147 name: 'Internet Explorer'
149 , version: getFirstMatch(/(?:msie |rv:)(\d+(\.\d+)?)/i)
151 } else if (chromeos) {
154 , osname: 'Chrome OS'
158 , version: getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)
160 } else if (/edg([ea]|ios)/i.test(ua)) {
162 name: 'Microsoft Edge'
164 , version: edgeVersion
167 else if (/vivaldi/i.test(ua)) {
171 , version: getFirstMatch(/vivaldi\/(\d+(\.\d+)?)/i) || versionIdentifier
177 , osname: 'Sailfish OS'
179 , version: getFirstMatch(/sailfish\s?browser\/(\d+(\.\d+)?)/i)
182 else if (/seamonkey\//i.test(ua)) {
186 , version: getFirstMatch(/seamonkey\/(\d+(\.\d+)?)/i)
189 else if (/firefox|iceweasel|fxios/i.test(ua)) {
193 , version: getFirstMatch(/(?:firefox|iceweasel|fxios)[ \/](\d+(\.\d+)?)/i)
195 if (/\((mobile|tablet);[^\)]*rv:[\d\.]+\)/i.test(ua)) {
197 result.osname = 'Firefox OS'
204 , version : getFirstMatch(/silk\/(\d+(\.\d+)?)/i)
207 else if (/phantom/i.test(ua)) {
211 , version: getFirstMatch(/phantomjs\/(\d+(\.\d+)?)/i)
214 else if (/slimerjs/i.test(ua)) {
218 , version: getFirstMatch(/slimerjs\/(\d+(\.\d+)?)/i)
221 else if (/blackberry|\bbb\d+/i.test(ua) || /rim\stablet/i.test(ua)) {
224 , osname: 'BlackBerry OS'
226 , version: versionIdentifier || getFirstMatch(/blackberry[\d]+\/(\d+(\.\d+)?)/i)
234 , version: versionIdentifier || getFirstMatch(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i)
236 /touchpad\//i.test(ua) && (result.touchpad = t)
238 else if (/bada/i.test(ua)) {
243 , version: getFirstMatch(/dolfin\/(\d+(\.\d+)?)/i)
251 , version: getFirstMatch(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i) || versionIdentifier
254 else if (/qupzilla/i.test(ua)) {
258 , version: getFirstMatch(/(?:qupzilla)[\s\/](\d+(?:\.\d+)+)/i) || versionIdentifier
261 else if (/chromium/i.test(ua)) {
265 , version: getFirstMatch(/(?:chromium)[\s\/](\d+(?:\.\d+)?)/i) || versionIdentifier
268 else if (/chrome|crios|crmo/i.test(ua)) {
272 , version: getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)
278 , version: versionIdentifier
281 else if (/safari|applewebkit/i.test(ua)) {
286 if (versionIdentifier) {
287 result.version = versionIdentifier
290 else if (iosdevice) {
292 name : iosdevice == 'iphone' ? 'iPhone' : iosdevice == 'ipad' ? 'iPad' : 'iPod'
294 // WTF: version is not part of user agent in web apps
295 if (versionIdentifier) {
296 result.version = versionIdentifier
299 else if(/googlebot/i.test(ua)) {
303 , version: getFirstMatch(/googlebot\/(\d+(\.\d+))/i) || versionIdentifier
308 name: getFirstMatch(/^(.*)\/(.*) /),
309 version: getSecondMatch(/^(.*)\/(.*) /)
313 // set webkit or gecko flag for browsers based on these engines
314 if (!result.msedge && /(apple)?webkit/i.test(ua)) {
315 if (/(apple)?webkit\/537\.36/i.test(ua)) {
316 result.name = result.name || "Blink"
319 result.name = result.name || "Webkit"
322 if (!result.version && versionIdentifier) {
323 result.version = versionIdentifier
325 } else if (!result.opera && /gecko\//i.test(ua)) {
326 result.name = result.name || "Gecko"
328 result.version = result.version || getFirstMatch(/gecko\/(\d+(\.\d+)?)/i)
331 // set OS flags for platforms that have multiple browsers
332 if (!result.windowsphone && (android || result.silk)) {
334 result.osname = 'Android'
335 } else if (!result.windowsphone && iosdevice) {
336 result[iosdevice] = t
338 result.osname = 'iOS'
341 result.osname = 'macOS'
344 result.osname = 'Xbox'
345 } else if (windows) {
347 result.osname = 'Windows'
350 result.osname = 'Linux'
353 function getWindowsVersion (s) {
355 case 'NT': return 'NT'
356 case 'XP': return 'XP'
357 case 'NT 5.0': return '2000'
358 case 'NT 5.1': return 'XP'
359 case 'NT 5.2': return '2003'
360 case 'NT 6.0': return 'Vista'
361 case 'NT 6.1': return '7'
362 case 'NT 6.2': return '8'
363 case 'NT 6.3': return '8.1'
364 case 'NT 10.0': return '10'
365 default: return undefined
369 // OS version extraction
371 if (result.windows) {
372 osVersion = getWindowsVersion(getFirstMatch(/Windows ((NT|XP)( \d\d?.\d)?)/i))
373 } else if (result.windowsphone) {
374 osVersion = getFirstMatch(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i);
375 } else if (result.mac) {
376 osVersion = getFirstMatch(/Mac OS X (\d+([_\.\s]\d+)*)/i);
377 osVersion = osVersion.replace(/[_\s]/g, '.');
378 } else if (iosdevice) {
379 osVersion = getFirstMatch(/os (\d+([_\s]\d+)*) like mac os x/i);
380 osVersion = osVersion.replace(/[_\s]/g, '.');
381 } else if (android) {
382 osVersion = getFirstMatch(/android[ \/-](\d+(\.\d+)*)/i);
383 } else if (result.webos) {
384 osVersion = getFirstMatch(/(?:web|hpw)os\/(\d+(\.\d+)*)/i);
385 } else if (result.blackberry) {
386 osVersion = getFirstMatch(/rim\stablet\sos\s(\d+(\.\d+)*)/i);
387 } else if (result.bada) {
388 osVersion = getFirstMatch(/bada\/(\d+(\.\d+)*)/i);
389 } else if (result.tizen) {
390 osVersion = getFirstMatch(/tizen[\/\s](\d+(\.\d+)*)/i);
393 result.osversion = osVersion;
396 // device type extraction
397 var osMajorVersion = !result.windows && osVersion.split('.')[0];
401 || iosdevice == 'ipad'
402 || (android && (osMajorVersion == 3 || (osMajorVersion >= 4 && !mobile)))
408 || iosdevice == 'iphone'
409 || iosdevice == 'ipod'
419 // Graded Browser Support
420 // http://developer.yahoo.com/yui/articles/gbs
422 (result.msie && result.version >= 10) ||
423 (result.yandexbrowser && result.version >= 15) ||
424 (result.vivaldi && result.version >= 1.0) ||
425 (result.chrome && result.version >= 20) ||
426 (result.samsungBrowser && result.version >= 4) ||
427 (result.firefox && result.version >= 20.0) ||
428 (result.safari && result.version >= 6) ||
429 (result.opera && result.version >= 10.0) ||
430 (result.ios && result.osversion && result.osversion.split(".")[0] >= 6) ||
431 (result.blackberry && result.version >= 10.1)
432 || (result.chromium && result.version >= 20)
436 else if ((result.msie && result.version < 10) ||
437 (result.chrome && result.version < 20) ||
438 (result.firefox && result.version < 20.0) ||
439 (result.safari && result.version < 6) ||
440 (result.opera && result.version < 10.0) ||
441 (result.ios && result.osversion && result.osversion.split(".")[0] < 6)
442 || (result.chromium && result.version < 20)
450 var bowser = detect(typeof navigator !== 'undefined' ? navigator.userAgent || '' : '')
452 bowser.test = function (browserList) {
453 for (var i = 0; i < browserList.length; ++i) {
454 var browserItem = browserList[i];
455 if (typeof browserItem=== 'string') {
456 if (browserItem in bowser) {
465 * Get version precisions count
468 * getVersionPrecision("1.10.3") // 3
470 * @param {string} version
473 function getVersionPrecision(version) {
474 return version.split(".").length;
478 * Array::map polyfill
481 * @param {Function} iterator
484 function map(arr, iterator) {
486 if (Array.prototype.map) {
487 return Array.prototype.map.call(arr, iterator);
489 for (i = 0; i < arr.length; i++) {
490 result.push(iterator(arr[i]));
496 * Calculate browser version weight
499 * compareVersions(['1.10.2.1', '1.8.2.1.90']) // 1
500 * compareVersions(['1.010.2.1', '1.09.2.1.90']); // 1
501 * compareVersions(['1.10.2.1', '1.10.2.1']); // 0
502 * compareVersions(['1.10.2.1', '1.0800.2']); // -1
504 * @param {Array<String>} versions versions to compare
505 * @return {Number} comparison result
507 function compareVersions(versions) {
508 // 1) get common precision for both versions, for example for "10.0" and "9" it should be 2
509 var precision = Math.max(getVersionPrecision(versions[0]), getVersionPrecision(versions[1]));
510 var chunks = map(versions, function (version) {
511 var delta = precision - getVersionPrecision(version);
513 // 2) "9" -> "9.0" (for precision = 2)
514 version = version + new Array(delta + 1).join(".0");
516 // 3) "9.0" -> ["000000000"", "000000009"]
517 return map(version.split("."), function (chunk) {
518 return new Array(20 - chunk.length).join("0") + chunk;
522 // iterate in reverse order by reversed chunks array
523 while (--precision >= 0) {
524 // 4) compare: "000000009" > "000000010" = false (but "9" > "10" = true)
525 if (chunks[0][precision] > chunks[1][precision]) {
528 else if (chunks[0][precision] === chunks[1][precision]) {
529 if (precision === 0) {
530 // all version chunks are same
541 * Check if browser is unsupported
544 * bowser.isUnsupportedBrowser({
553 * @param {Object} minVersions map of minimal version to browser
554 * @param {Boolean} [strictMode = false] flag to return false if browser wasn't found in map
555 * @param {String} [ua] user agent string
558 function isUnsupportedBrowser(minVersions, strictMode, ua) {
559 var _bowser = bowser;
561 // make strictMode param optional with ua param usage
562 if (typeof strictMode === 'string') {
564 strictMode = void(0);
567 if (strictMode === void(0)) {
571 _bowser = detect(ua);
574 var version = "" + _bowser.version;
575 for (var browser in minVersions) {
576 if (minVersions.hasOwnProperty(browser)) {
577 if (_bowser[browser]) {
578 if (typeof minVersions[browser] !== 'string') {
579 throw new Error('Browser version in the minVersion map should be a string: ' + browser + ': ' + String(minVersions));
582 // browser version and min supported version.
583 return compareVersions([version, minVersions[browser]]) < 0;
588 return strictMode; // not found
592 * Check if browser is supported
594 * @param {Object} minVersions map of minimal version to browser
595 * @param {Boolean} [strictMode = false] flag to return false if browser wasn't found in map
596 * @param {String} [ua] user agent string
599 function check(minVersions, strictMode, ua) {
600 return !isUnsupportedBrowser(minVersions, strictMode, ua);
603 bowser.isUnsupportedBrowser = isUnsupportedBrowser;
604 bowser.compareVersions = compareVersions;
605 bowser.check = check;
608 * Set our detect method to the main bowser object so we can
609 * reuse it to test other user agents.
610 * This is needed to implement future tests.
612 bowser._detect = detect;
615 * Set our detect public method to the main bowser object
616 * This is needed to implement bowser in server side
618 bowser.detect = detect;