1
0
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:
Andreas Lind Petersen 2013-04-09 09:56:32 +02:00
parent 23545d4403
commit 5e793db814

View File

@ -1,7 +1,7 @@
!function (name, definition) {
if (typeof define == 'function') define(definition)
else if (typeof module != 'undefined' && module.exports) module.exports['browser'] = definition()
else this[name] = definition()
if (typeof define == 'function') define(definition()(navigator.userAgent))
else if (typeof module != 'undefined' && module.exports) module.exports = definition()
else this[name] = definition()(navigator.userAgent)
}('bowser', function () {
/**
* 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",
* 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"
* 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
, t = true
, ie = /msie/i.test(ua)
var t = true
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)
, safari = /safari/i.test(ua) && !chrome
, phantom = /phantom/i.test(ua)
, safari = /safari/i.test(ua) && !chrome && !phantom
, iphone = /iphone/i.test(ua)
, ipad = /ipad/i.test(ua)
, touchpad = /touchpad/i.test(ua)
@ -32,21 +40,24 @@
, webkitVersion = /version\/(\d+(\.\d+)?)/i
, o
function detect() {
if (ie) return {
msie: t
, version: ua.match(/msie (\d+(\.\d+)?);/i)[1]
, version: getFirstMatch(/msie (\d+(\.\d+)?);/i)
}
if (chrome) return {
webkit: 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 {
webkit: t
, touchpad: t
, version : ua.match(/touchpad\/(\d+(\.\d+)?)/i)[1]
, version: getFirstMatch(/touchpad\/(\d+(\.\d+)?)/i)
}
if (iphone || ipad) {
o = {
@ -58,7 +69,7 @@
}
// WTF: version is not part of user agent in web apps
if (webkitVersion.test(ua)) {
o.version = ua.match(webkitVersion)[1]
o.version = getFirstMatch(webkitVersion)
}
return o
}
@ -66,33 +77,37 @@
webkit: t
, android: t
, mobile: t
, version: ua.match(webkitVersion)[1]
, version: getFirstMatch(webkitVersion)
}
if (safari) return {
webkit: t
, safari: t
, version: ua.match(webkitVersion)[1]
, version: getFirstMatch(webkitVersion)
}
if (opera) return {
opera: t
, version: ua.match(webkitVersion)[1]
, version: getFirstMatch(webkitVersion)
}
if (gecko) {
o = {
gecko: t
, mozilla: t
, version: ua.match(/firefox\/(\d+(\.\d+)?)/i)[1]
, version: getFirstMatch(/firefox\/(\d+(\.\d+)?)/i)
}
if (firefox) o.firefox = t
return o
}
if (seamonkey) return {
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
// http://developer.yahoo.com/yui/articles/gbs
@ -111,6 +126,6 @@
(bowser.opera && bowser.version < 10.0)) {
bowser.c = t
} else bowser.x = t
return bowser
return bowser;
}
})