1
0
mirror of https://github.com/lancedikson/bowser synced 2024-10-27 20:34:22 +00:00

Make the detect function reusable.

This commit is contained in:
Hannes Diercks 2013-09-13 14:08:52 +02:00
parent 90ad912e8d
commit 3fe73237e2
3 changed files with 115 additions and 103 deletions

108
bowser.js
View File

@ -28,44 +28,42 @@
* PhantomJS: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34" * PhantomJS: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34"
*/ */
var ua = navigator.userAgent function detect(ua) {
, t = true var t = true
, ie = /(msie|trident)/i.test(ua) , ie = /(msie|trident)/i.test(ua)
, chrome = /chrome/i.test(ua) , chrome = /chrome/i.test(ua)
, phantom = /phantom/i.test(ua) , phantom = /phantom/i.test(ua)
, safari = /safari/i.test(ua) && !chrome && !phantom , safari = /safari/i.test(ua) && !chrome && !phantom
, iphone = /iphone/i.test(ua) , iphone = /iphone/i.test(ua)
, ipad = /ipad/i.test(ua) , ipad = /ipad/i.test(ua)
, touchpad = /touchpad/i.test(ua) , touchpad = /touchpad/i.test(ua)
, android = /android/i.test(ua) , android = /android/i.test(ua)
, opera = /opera/i.test(ua) || /opr/i.test(ua) , opera = /opera/i.test(ua) || /opr/i.test(ua)
, firefox = /firefox/i.test(ua) , firefox = /firefox/i.test(ua)
, gecko = /gecko\//i.test(ua) , gecko = /gecko\//i.test(ua)
, seamonkey = /seamonkey\//i.test(ua) , seamonkey = /seamonkey\//i.test(ua)
, webkitVersion = /version\/(\d+(\.\d+)?)/i , webkitVersion = /version\/(\d+(\.\d+)?)/i
, firefoxVersion = /firefox\/(\d+(\.\d+)?)/i , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i
, o; , o;
function detect() {
if (ie) { if (ie) {
return { o = {
msie: t msie: t
, version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2] , version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]
}; };
} else if (opera) { } else if (opera) {
return { o = {
opera: t opera: t
, version: ua.match(webkitVersion) ? ua.match(webkitVersion)[1] : ua.match(/opr\/(\d+(\.\d+)?)/i) , version: ua.match(webkitVersion) ? ua.match(webkitVersion)[1] : ua.match(/opr\/(\d+(\.\d+)?)/i)
}; };
} else if (chrome) { } else if (chrome) {
return { o = {
webkit: t webkit: t
, chrome: t , chrome: t
, version: ua.match(/chrome\/(\d+(\.\d+)?)/i)[1] , version: ua.match(/chrome\/(\d+(\.\d+)?)/i)[1]
}; };
} else if (phantom) { } else if (phantom) {
return { o = {
webkit: t webkit: t
, phantom: t , phantom: t
, version: ua.match(/phantomjs\/(\d+(\.\d+)+)/i)[1] , version: ua.match(/phantomjs\/(\d+(\.\d+)+)/i)[1]
@ -88,16 +86,15 @@
if (webkitVersion.test(ua)) { if (webkitVersion.test(ua)) {
o.version = ua.match(webkitVersion)[1]; o.version = ua.match(webkitVersion)[1];
} }
return o;
} else if (android) { } else if (android) {
return { o = {
webkit: t webkit: t
, android: t , android: t
, mobile: t , mobile: t
, version: (ua.match(webkitVersion) || ua.match(firefoxVersion))[1] , version: (ua.match(webkitVersion) || ua.match(firefoxVersion))[1]
}; };
} else if (safari) { } else if (safari) {
return { o = {
webkit: t webkit: t
, safari: t , safari: t
, version: ua.match(webkitVersion)[1] , version: ua.match(webkitVersion)[1]
@ -109,38 +106,47 @@
, version: ua.match(firefoxVersion)[1] , version: ua.match(firefoxVersion)[1]
}; };
if (firefox) o.firefox = t; if (firefox) o.firefox = t;
return o;
} else if (seamonkey) { } else if (seamonkey) {
return { o = {
seamonkey: t seamonkey: t
, version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1] , version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]
}; };
} else { } else {
return {}; o = {};
} }
// Graded Browser Support
// http://developer.yahoo.com/yui/articles/gbs
if ((o.msie && o.version >= 8) ||
(o.chrome && o.version >= 10) ||
(o.firefox && o.version >= 4.0) ||
(o.safari && o.version >= 5) ||
(o.opera && o.version >= 10.0)) {
o.a = t;
}
else if ((o.msie && o.version < 8) ||
(o.chrome && o.version < 10) ||
(o.firefox && o.version < 4.0) ||
(o.safari && o.version < 5) ||
(o.opera && o.version < 10.0)) {
o.c = t;
} else {
o.x = t;
}
return o;
} }
var bowser = detect() /* Get our main bowser object from navigators user agent if present. */
var bowser = detect(typeof navigator !== 'undefined' ? navigator.userAgent : '');
// Graded Browser Support
// http://developer.yahoo.com/yui/articles/gbs /*
if ((bowser.msie && bowser.version >= 8) || * Set our detect method to the main bowser object so we can
(bowser.chrome && bowser.version >= 10) || * reuse it to test other user agents.
(bowser.firefox && bowser.version >= 4.0) || * This is needed to implement future tests.
(bowser.safari && bowser.version >= 5) || */
(bowser.opera && bowser.version >= 10.0)) { bowser._detect = detect;
bowser.a = t;
}
else if ((bowser.msie && bowser.version < 8) ||
(bowser.chrome && bowser.version < 10) ||
(bowser.firefox && bowser.version < 4.0) ||
(bowser.safari && bowser.version < 5) ||
(bowser.opera && bowser.version < 10.0)) {
bowser.c = t;
} else {
bowser.x = t;
}
return bowser; return bowser;
}) });

2
bowser.min.js vendored
View File

@ -3,4 +3,4 @@
* https://github.com/ded/bowser * https://github.com/ded/bowser
* MIT License | (c) Dustin Diaz 2013 * MIT License | (c) Dustin Diaz 2013
*/ */
!function(e,t){typeof define=="function"?define(t):typeof module!="undefined"&&module.exports?module.exports[e]=t():this[e]=t()}("bowser",function(){function g(){return n?{msie:t,version:e.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]}:l?{opera:t,version:e.match(d)?e.match(d)[1]:e.match(/opr\/(\d+(\.\d+)?)/i)}:r?{webkit:t,chrome:t,version:e.match(/chrome\/(\d+(\.\d+)?)/i)[1]}:i?{webkit:t,phantom:t,version:e.match(/phantomjs\/(\d+(\.\d+)+)/i)[1]}:a?{webkit:t,touchpad:t,version:e.match(/touchpad\/(\d+(\.\d+)?)/i)[1]}:o||u?(m={webkit:t,mobile:t,ios:t,iphone:o,ipad:u},d.test(e)&&(m.version=e.match(d)[1]),m):f?{webkit:t,android:t,mobile:t,version:(e.match(d)||e.match(v))[1]}:s?{webkit:t,safari:t,version:e.match(d)[1]}:h?(m={gecko:t,mozilla:t,version:e.match(v)[1]},c&&(m.firefox=t),m):p?{seamonkey:t,version:e.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]}:{}}var e=navigator.userAgent,t=!0,n=/(msie|trident)/i.test(e),r=/chrome/i.test(e),i=/phantom/i.test(e),s=/safari/i.test(e)&&!r&&!i,o=/iphone/i.test(e),u=/ipad/i.test(e),a=/touchpad/i.test(e),f=/android/i.test(e),l=/opera/i.test(e)||/opr/i.test(e),c=/firefox/i.test(e),h=/gecko\//i.test(e),p=/seamonkey\//i.test(e),d=/version\/(\d+(\.\d+)?)/i,v=/firefox\/(\d+(\.\d+)?)/i,m,y=g();return y.msie&&y.version>=8||y.chrome&&y.version>=10||y.firefox&&y.version>=4||y.safari&&y.version>=5||y.opera&&y.version>=10?y.a=t:y.msie&&y.version<8||y.chrome&&y.version<10||y.firefox&&y.version<4||y.safari&&y.version<5||y.opera&&y.version<10?y.c=t:y.x=t,y}) !function(e,t){typeof define=="function"?define(t):typeof module!="undefined"&&module.exports?module.exports[e]=t():this[e]=t()}("bowser",function(){function e(e){var t=!0,n=/(msie|trident)/i.test(e),r=/chrome/i.test(e),i=/phantom/i.test(e),s=/safari/i.test(e)&&!r&&!i,o=/iphone/i.test(e),u=/ipad/i.test(e),a=/touchpad/i.test(e),f=/android/i.test(e),l=/opera/i.test(e)||/opr/i.test(e),c=/firefox/i.test(e),h=/gecko\//i.test(e),p=/seamonkey\//i.test(e),d=/version\/(\d+(\.\d+)?)/i,v=/firefox\/(\d+(\.\d+)?)/i,m;if(n)m={msie:t,version:e.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]};else if(l)m={opera:t,version:e.match(d)?e.match(d)[1]:e.match(/opr\/(\d+(\.\d+)?)/i)};else if(r)m={webkit:t,chrome:t,version:e.match(/chrome\/(\d+(\.\d+)?)/i)[1]};else if(i)m={webkit:t,phantom:t,version:e.match(/phantomjs\/(\d+(\.\d+)+)/i)[1]};else{if(a)return{webkit:t,touchpad:t,version:e.match(/touchpad\/(\d+(\.\d+)?)/i)[1]};o||u?(m={webkit:t,mobile:t,ios:t,iphone:o,ipad:u},d.test(e)&&(m.version=e.match(d)[1])):f?m={webkit:t,android:t,mobile:t,version:(e.match(d)||e.match(v))[1]}:s?m={webkit:t,safari:t,version:e.match(d)[1]}:h?(m={gecko:t,mozilla:t,version:e.match(v)[1]},c&&(m.firefox=t)):p?m={seamonkey:t,version:e.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]}:m={}}return m.msie&&m.version>=8||m.chrome&&m.version>=10||m.firefox&&m.version>=4||m.safari&&m.version>=5||m.opera&&m.version>=10?m.a=t:m.msie&&m.version<8||m.chrome&&m.version<10||m.firefox&&m.version<4||m.safari&&m.version<5||m.opera&&m.version<10?m.c=t:m.x=t,m}var t=e(typeof navigator!="undefined"?navigator.userAgent:"");return t._detect=e,t})

View File

@ -22,44 +22,42 @@
* PhantomJS: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34" * PhantomJS: "Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34"
*/ */
var ua = navigator.userAgent function detect(ua) {
, t = true var t = true
, ie = /(msie|trident)/i.test(ua) , ie = /(msie|trident)/i.test(ua)
, chrome = /chrome/i.test(ua) , chrome = /chrome/i.test(ua)
, phantom = /phantom/i.test(ua) , phantom = /phantom/i.test(ua)
, safari = /safari/i.test(ua) && !chrome && !phantom , safari = /safari/i.test(ua) && !chrome && !phantom
, iphone = /iphone/i.test(ua) , iphone = /iphone/i.test(ua)
, ipad = /ipad/i.test(ua) , ipad = /ipad/i.test(ua)
, touchpad = /touchpad/i.test(ua) , touchpad = /touchpad/i.test(ua)
, android = /android/i.test(ua) , android = /android/i.test(ua)
, opera = /opera/i.test(ua) || /opr/i.test(ua) , opera = /opera/i.test(ua) || /opr/i.test(ua)
, firefox = /firefox/i.test(ua) , firefox = /firefox/i.test(ua)
, gecko = /gecko\//i.test(ua) , gecko = /gecko\//i.test(ua)
, seamonkey = /seamonkey\//i.test(ua) , seamonkey = /seamonkey\//i.test(ua)
, webkitVersion = /version\/(\d+(\.\d+)?)/i , webkitVersion = /version\/(\d+(\.\d+)?)/i
, firefoxVersion = /firefox\/(\d+(\.\d+)?)/i , firefoxVersion = /firefox\/(\d+(\.\d+)?)/i
, o; , o;
function detect() {
if (ie) { if (ie) {
return { o = {
msie: t msie: t
, version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2] , version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]
}; };
} else if (opera) { } else if (opera) {
return { o = {
opera: t opera: t
, version: ua.match(webkitVersion) ? ua.match(webkitVersion)[1] : ua.match(/opr\/(\d+(\.\d+)?)/i) , version: ua.match(webkitVersion) ? ua.match(webkitVersion)[1] : ua.match(/opr\/(\d+(\.\d+)?)/i)
}; };
} else if (chrome) { } else if (chrome) {
return { o = {
webkit: t webkit: t
, chrome: t , chrome: t
, version: ua.match(/chrome\/(\d+(\.\d+)?)/i)[1] , version: ua.match(/chrome\/(\d+(\.\d+)?)/i)[1]
}; };
} else if (phantom) { } else if (phantom) {
return { o = {
webkit: t webkit: t
, phantom: t , phantom: t
, version: ua.match(/phantomjs\/(\d+(\.\d+)+)/i)[1] , version: ua.match(/phantomjs\/(\d+(\.\d+)+)/i)[1]
@ -82,16 +80,15 @@
if (webkitVersion.test(ua)) { if (webkitVersion.test(ua)) {
o.version = ua.match(webkitVersion)[1]; o.version = ua.match(webkitVersion)[1];
} }
return o;
} else if (android) { } else if (android) {
return { o = {
webkit: t webkit: t
, android: t , android: t
, mobile: t , mobile: t
, version: (ua.match(webkitVersion) || ua.match(firefoxVersion))[1] , version: (ua.match(webkitVersion) || ua.match(firefoxVersion))[1]
}; };
} else if (safari) { } else if (safari) {
return { o = {
webkit: t webkit: t
, safari: t , safari: t
, version: ua.match(webkitVersion)[1] , version: ua.match(webkitVersion)[1]
@ -103,38 +100,47 @@
, version: ua.match(firefoxVersion)[1] , version: ua.match(firefoxVersion)[1]
}; };
if (firefox) o.firefox = t; if (firefox) o.firefox = t;
return o;
} else if (seamonkey) { } else if (seamonkey) {
return { o = {
seamonkey: t seamonkey: t
, version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1] , version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]
}; };
} else { } else {
return {}; o = {};
} }
// Graded Browser Support
// http://developer.yahoo.com/yui/articles/gbs
if ((o.msie && o.version >= 8) ||
(o.chrome && o.version >= 10) ||
(o.firefox && o.version >= 4.0) ||
(o.safari && o.version >= 5) ||
(o.opera && o.version >= 10.0)) {
o.a = t;
}
else if ((o.msie && o.version < 8) ||
(o.chrome && o.version < 10) ||
(o.firefox && o.version < 4.0) ||
(o.safari && o.version < 5) ||
(o.opera && o.version < 10.0)) {
o.c = t;
} else {
o.x = t;
}
return o;
} }
var bowser = detect() /* Get our main bowser object from navigators user agent if present. */
var bowser = detect(typeof navigator !== 'undefined' ? navigator.userAgent : '');
// Graded Browser Support
// http://developer.yahoo.com/yui/articles/gbs /*
if ((bowser.msie && bowser.version >= 8) || * Set our detect method to the main bowser object so we can
(bowser.chrome && bowser.version >= 10) || * reuse it to test other user agents.
(bowser.firefox && bowser.version >= 4.0) || * This is needed to implement future tests.
(bowser.safari && bowser.version >= 5) || */
(bowser.opera && bowser.version >= 10.0)) { bowser._detect = detect;
bowser.a = t;
}
else if ((bowser.msie && bowser.version < 8) ||
(bowser.chrome && bowser.version < 10) ||
(bowser.firefox && bowser.version < 4.0) ||
(bowser.safari && bowser.version < 5) ||
(bowser.opera && bowser.version < 10.0)) {
bowser.c = t;
} else {
bowser.x = t;
}
return bowser; return bowser;
}) });