mirror of
https://github.com/lancedikson/bowser
synced 2024-10-27 20:34:22 +00:00
In node.js: Export a function that builds the bowser object instead of trying to dereference navigator.userAgent, which probably doesn't exist.
This commit is contained in:
parent
23545d4403
commit
5e793db814
@ -1,7 +1,7 @@
|
|||||||
!function (name, definition) {
|
!function (name, definition) {
|
||||||
if (typeof define == 'function') define(definition)
|
if (typeof define == 'function') define(definition()(navigator.userAgent))
|
||||||
else if (typeof module != 'undefined' && module.exports) module.exports['browser'] = definition()
|
else if (typeof module != 'undefined' && module.exports) module.exports = definition()
|
||||||
else this[name] = definition()
|
else this[name] = definition()(navigator.userAgent)
|
||||||
}('bowser', function () {
|
}('bowser', function () {
|
||||||
/**
|
/**
|
||||||
* navigator.userAgent =>
|
* navigator.userAgent =>
|
||||||
@ -14,13 +14,21 @@
|
|||||||
* iPad: "Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5",
|
* iPad: "Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5",
|
||||||
* Android: "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile G2 Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
|
* Android: "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile G2 Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
|
||||||
* Touchpad: "Mozilla/5.0 (hp-tabled;Linux;hpwOS/3.0.5; U; en-US)) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/234.83 Safari/534.6 TouchPad/1.0"
|
* Touchpad: "Mozilla/5.0 (hp-tabled;Linux;hpwOS/3.0.5; U; en-US)) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/234.83 Safari/534.6 TouchPad/1.0"
|
||||||
|
* 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
|
var t = true
|
||||||
, t = true
|
|
||||||
, ie = /msie/i.test(ua)
|
function detect(ua) {
|
||||||
|
function getFirstMatch(regexp) {
|
||||||
|
var match = ua.match(regexp);
|
||||||
|
return match && match[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
var ie = /msie/i.test(ua)
|
||||||
, chrome = /chrome/i.test(ua)
|
, chrome = /chrome/i.test(ua)
|
||||||
, safari = /safari/i.test(ua) && !chrome
|
, phantom = /phantom/i.test(ua)
|
||||||
|
, 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)
|
||||||
@ -32,21 +40,24 @@
|
|||||||
, webkitVersion = /version\/(\d+(\.\d+)?)/i
|
, webkitVersion = /version\/(\d+(\.\d+)?)/i
|
||||||
, o
|
, o
|
||||||
|
|
||||||
function detect() {
|
|
||||||
|
|
||||||
if (ie) return {
|
if (ie) return {
|
||||||
msie: t
|
msie: t
|
||||||
, version: ua.match(/msie (\d+(\.\d+)?);/i)[1]
|
, version: getFirstMatch(/msie (\d+(\.\d+)?);/i)
|
||||||
}
|
}
|
||||||
if (chrome) return {
|
if (chrome) return {
|
||||||
webkit: t
|
webkit: t
|
||||||
, chrome: t
|
, chrome: t
|
||||||
, version: ua.match(/chrome\/(\d+(\.\d+)?)/i)[1]
|
, version: getFirstMatch(/chrome\/(\d+(\.\d+)?)/i)
|
||||||
|
}
|
||||||
|
if (phantom) return {
|
||||||
|
webkit: t
|
||||||
|
, phantom: t
|
||||||
|
, version: getFirstMatch(/phantomjs\/(\d+(\.\d+)+)/i)
|
||||||
}
|
}
|
||||||
if (touchpad) return {
|
if (touchpad) return {
|
||||||
webkit: t
|
webkit: t
|
||||||
, touchpad: t
|
, touchpad: t
|
||||||
, version : ua.match(/touchpad\/(\d+(\.\d+)?)/i)[1]
|
, version: getFirstMatch(/touchpad\/(\d+(\.\d+)?)/i)
|
||||||
}
|
}
|
||||||
if (iphone || ipad) {
|
if (iphone || ipad) {
|
||||||
o = {
|
o = {
|
||||||
@ -58,7 +69,7 @@
|
|||||||
}
|
}
|
||||||
// WTF: version is not part of user agent in web apps
|
// WTF: version is not part of user agent in web apps
|
||||||
if (webkitVersion.test(ua)) {
|
if (webkitVersion.test(ua)) {
|
||||||
o.version = ua.match(webkitVersion)[1]
|
o.version = getFirstMatch(webkitVersion)
|
||||||
}
|
}
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
@ -66,33 +77,37 @@
|
|||||||
webkit: t
|
webkit: t
|
||||||
, android: t
|
, android: t
|
||||||
, mobile: t
|
, mobile: t
|
||||||
, version: ua.match(webkitVersion)[1]
|
, version: getFirstMatch(webkitVersion)
|
||||||
}
|
}
|
||||||
if (safari) return {
|
if (safari) return {
|
||||||
webkit: t
|
webkit: t
|
||||||
, safari: t
|
, safari: t
|
||||||
, version: ua.match(webkitVersion)[1]
|
, version: getFirstMatch(webkitVersion)
|
||||||
}
|
}
|
||||||
if (opera) return {
|
if (opera) return {
|
||||||
opera: t
|
opera: t
|
||||||
, version: ua.match(webkitVersion)[1]
|
, version: getFirstMatch(webkitVersion)
|
||||||
}
|
}
|
||||||
if (gecko) {
|
if (gecko) {
|
||||||
o = {
|
o = {
|
||||||
gecko: t
|
gecko: t
|
||||||
, mozilla: t
|
, mozilla: t
|
||||||
, version: ua.match(/firefox\/(\d+(\.\d+)?)/i)[1]
|
, version: getFirstMatch(/firefox\/(\d+(\.\d+)?)/i)
|
||||||
}
|
}
|
||||||
if (firefox) o.firefox = t
|
if (firefox) o.firefox = t
|
||||||
return o
|
return o
|
||||||
}
|
}
|
||||||
if (seamonkey) return {
|
if (seamonkey) return {
|
||||||
seamonkey: t
|
seamonkey: t
|
||||||
, version: ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]
|
, version: getFirstMatch(/seamonkey\/(\d+(\.\d+)?)/i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var bowser = detect()
|
return function createBowser(ua) {
|
||||||
|
var bowser = detect(ua)
|
||||||
|
if (!bowser) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Graded Browser Support
|
// Graded Browser Support
|
||||||
// http://developer.yahoo.com/yui/articles/gbs
|
// http://developer.yahoo.com/yui/articles/gbs
|
||||||
@ -111,6 +126,6 @@
|
|||||||
(bowser.opera && bowser.version < 10.0)) {
|
(bowser.opera && bowser.version < 10.0)) {
|
||||||
bowser.c = t
|
bowser.c = t
|
||||||
} else bowser.x = t
|
} else bowser.x = t
|
||||||
|
return bowser;
|
||||||
return bowser
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user