From 5e793db8148718c762f2db853b50a1ebe80895a2 Mon Sep 17 00:00:00 2001 From: Andreas Lind Petersen Date: Tue, 9 Apr 2013 09:56:32 +0200 Subject: [PATCH] In node.js: Export a function that builds the bowser object instead of trying to dereference navigator.userAgent, which probably doesn't exist. --- src/bowser.js | 109 ++++++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 47 deletions(-) diff --git a/src/bowser.js b/src/bowser.js index 3b81fc5..b6eb957 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -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,39 +14,50 @@ * 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) - , chrome = /chrome/i.test(ua) - , safari = /safari/i.test(ua) && !chrome - , iphone = /iphone/i.test(ua) - , ipad = /ipad/i.test(ua) - , touchpad = /touchpad/i.test(ua) - , android = /android/i.test(ua) - , opera = /opera/i.test(ua) - , firefox = /firefox/i.test(ua) - , gecko = /gecko\//i.test(ua) - , seamonkey = /seamonkey\//i.test(ua) - , webkitVersion = /version\/(\d+(\.\d+)?)/i - , o + var t = true - function detect() { + 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) + , 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) + , android = /android/i.test(ua) + , opera = /opera/i.test(ua) + , firefox = /firefox/i.test(ua) + , gecko = /gecko\//i.test(ua) + , seamonkey = /seamonkey\//i.test(ua) + , webkitVersion = /version\/(\d+(\.\d+)?)/i + , o 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,51 +77,55 @@ 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 - if ((bowser.msie && bowser.version >= 7) || - (bowser.chrome && bowser.version >= 10) || - (bowser.firefox && bowser.version >= 4.0) || - (bowser.safari && bowser.version >= 5) || - (bowser.opera && bowser.version >= 10.0)) { - bowser.a = t; + // Graded Browser Support + // http://developer.yahoo.com/yui/articles/gbs + if ((bowser.msie && bowser.version >= 7) || + (bowser.chrome && bowser.version >= 10) || + (bowser.firefox && bowser.version >= 4.0) || + (bowser.safari && bowser.version >= 5) || + (bowser.opera && bowser.version >= 10.0)) { + bowser.a = t; + } + + else if ((bowser.msie && bowser.version < 7) || + (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; } - - else if ((bowser.msie && bowser.version < 7) || - (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 })