From fd1d833dece569fb83a6183601447cd39bbdb094 Mon Sep 17 00:00:00 2001 From: BendingBender Date: Fri, 28 Feb 2014 01:58:30 +0100 Subject: [PATCH] refactored regex matching to be executed at the latest possible moment and after a match was found to skip all consecutive regex execution instead of running *all* of the checks upfront --- bowser.js | 200 ++++++++++++++++++++++++-------------------------- bowser.min.js | 2 +- src/bowser.js | 200 ++++++++++++++++++++++++-------------------------- 3 files changed, 191 insertions(+), 211 deletions(-) diff --git a/bowser.js b/bowser.js index 064f6b7..a64b4f0 100644 --- a/bowser.js +++ b/bowser.js @@ -17,126 +17,69 @@ function detect(ua) { - function getVersion(regex) { + function getFirstMatch(regex) { var match = ua.match(regex); return (match && match.length > 1 && match[1]) || ''; } - var ie = /msie|trident/i.test(ua) - , chrome = /chrome|crios|crmo/i.test(ua) - , phantom = /phantom/i.test(ua) - , ipod = /ipod/i.test(ua) - , iphone = !ipod && /iphone/i.test(ua) - , ipad = /ipad/i.test(ua) - , ios = iphone || ipad || ipod - , silk = /silk/i.test(ua) + var iosdevice = getFirstMatch(/(ipod|iphone|ipad)/i).toLowerCase() , likeAndroid = /like android/i.test(ua) , android = !likeAndroid && /android/i.test(ua) - , opera = /opera|opr/i.test(ua) - , firefox = /firefox|iceweasel/i.test(ua) - , gecko = /gecko\//i.test(ua) - , seamonkey = /seamonkey\//i.test(ua) - , webos = /(web|hpw)os/i.test(ua) - , touchpad = /touchpad\//i.test(ua) - , windowsphone = /windows phone/i.test(ua) - , blackberry = /blackberry|\bbb\d+/i.test(ua) - , rimtablet = /rim\stablet/i.test(ua) - , bada = /bada/i.test(ua) - , tizen = /tizen/i.test(ua) - , safari = /safari/i.test(ua) - , webkit = /applewebkit/i.test(ua) - , versionIdentifier = getVersion(/version\/(\d+(\.\d+)?)/i) - , mobile = /mobi/i.test(ua) + , versionIdentifier = getFirstMatch(/version\/(\d+(\.\d+)?)/i) , tablet = /tablet/i.test(ua) + , mobile = !tablet && /[^-]mobi/i.test(ua) , result - if (windowsphone) result = { - name: 'Windows Phone' - , windowsphone: t - , msie: t - , version: getVersion(/iemobile\/(\d+(\.\d+)?)/i) - } - else if (opera) { + if (/opera|opr/i.test(ua)) { result = { name: 'Opera' , opera: t - , version: versionIdentifier || - getVersion(/opr\/(\d+(\.\d+)?)/i) || - getVersion(/opera[ \/](\d+(\.\d+)?)/i) + , version: versionIdentifier || getFirstMatch(/(?:opera|opr)[\s\/](\d+(\.\d+)?)/i) } } - else if (ie) result = { + else if (/windows phone/i.test(ua)) { + result = { + name: 'Windows Phone' + , windowsphone: t + , msie: t + , version: getFirstMatch(/iemobile\/(\d+(\.\d+)?)/i) + } + } + else if (/msie|trident/i.test(ua)) { + result = { name: 'Internet Explorer' , msie: t - , version: getVersion(/(?:msie |rv:)(\d+(\.\d+)?)/i) + , version: getFirstMatch(/(?:msie |rv:)(\d+(\.\d+)?)/i) } - else if (chrome) { + } + else if (/chrome|crios|crmo/i.test(ua)) { result = { name: 'Chrome' , chrome: t - , version: getVersion(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i) + , version: getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i) } } - else if (phantom) result = { - name: 'PhantomJS' - , phantom: t - , version: getVersion(/phantomjs\/(\d+(\.\d+)?)/i) - } - else if (silk) result = { - name: 'Amazon Silk' - , silk: t - , version : getVersion(/silk\/(\d+(\.\d+)?)/i) - } - else if (ios) { + else if (iosdevice) { result = { - name : iphone ? 'iPhone' : ipad ? 'iPad' : 'iPod' + name : iosdevice == 'iphone' ? 'iPhone' : iosdevice == 'ipad' ? 'iPad' : 'iPod' } // WTF: version is not part of user agent in web apps if (versionIdentifier) { result.version = versionIdentifier } } - else if (blackberry || rimtablet) { - result = { - name: 'BlackBerry' - , blackberry: t - , version: versionIdentifier || getVersion(/blackberry[\d]+\/(\d+(\.\d+)?)/i) - } - } - else if (webos) { - result = { - name: 'WebOS' - , webos: t - , version: versionIdentifier || getVersion(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i) - }; - touchpad && (result.touchpad = t) - } - else if (bada) { - result = { - name: 'Bada' - , bada: t - , version: getVersion(/dolfin\/(\d+(\.\d+)?)/i) - }; - } - else if (tizen) { - result = { - name: 'Tizen' - , tizen: t - , version: getVersion(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i) || versionIdentifier - }; - } - else if (gecko) { + else if (/gecko\//i.test(ua)) { result = { name: 'Gecko' , gecko: t , mozilla: t - , version: getVersion(/(?:firefox|iceweasel)[ \/](\d+(\.\d+)?)/i) + , version: getFirstMatch(/(?:firefox|iceweasel)[ \/](\d+(\.\d+)?)/i) } - if (seamonkey) { + if (/seamonkey\//i.test(ua)) { result.name = 'SeaMonkey' result.seamonkey = t - result.version = getVersion(/seamonkey\/(\d+(\.\d+)?)/i) - } else if (firefox) { + result.version = getFirstMatch(/seamonkey\/(\d+(\.\d+)?)/i) + } else if (/firefox|iceweasel/i.test(ua)) { result.name = 'Firefox' result.firefox = t if (/\((mobile|tablet);[^\)]*rv:[\d\.]+\)/i.test(ua)) { @@ -144,45 +87,92 @@ } } } - else if (android) result = { + else if (/silk/i.test(ua)) { + result = { + name: 'Amazon Silk' + , silk: t + , version : getFirstMatch(/silk\/(\d+(\.\d+)?)/i) + } + } + else if (android) { + result = { name: 'Android' , version: versionIdentifier } - else if (safari) result = { + } + else if (/phantom/i.test(ua)) { + result = { + name: 'PhantomJS' + , phantom: t + , version: getFirstMatch(/phantomjs\/(\d+(\.\d+)?)/i) + } + } + else if (/blackberry|\bbb\d+/i.test(ua) || /rim\stablet/i.test(ua)) { + result = { + name: 'BlackBerry' + , blackberry: t + , version: versionIdentifier || getFirstMatch(/blackberry[\d]+\/(\d+(\.\d+)?)/i) + } + } + else if (/(web|hpw)os/i.test(ua)) { + result = { + name: 'WebOS' + , webos: t + , version: versionIdentifier || getFirstMatch(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i) + }; + /touchpad\//i.test(ua) && (result.touchpad = t) + } + else if (/bada/i.test(ua)) { + result = { + name: 'Bada' + , bada: t + , version: getFirstMatch(/dolfin\/(\d+(\.\d+)?)/i) + }; + } + else if (/tizen/i.test(ua)) { + result = { + name: 'Tizen' + , tizen: t + , version: getFirstMatch(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i) || versionIdentifier + }; + } + else if (/safari/i.test(ua)) { + result = { name: 'Safari' , safari: t , version: versionIdentifier } + } else result = {} // set OS flags for platforms that have multiple browsers - if (android || silk) { + if (android || result.silk) { result.android = t - } else if (ios) { - result[iphone ? 'iphone' : ipad ? 'ipad' : 'ipod'] = t + } else if (iosdevice) { + result[iosdevice] = t result.ios = t } - if (webkit) { + if (/applewebkit/i.test(ua)) { result.webkit = t } // OS version extraction var osVersion = ''; - if (ios) { - osVersion = getVersion(/os (\d+([_\s]\d+)*) like mac os x/i); + if (iosdevice) { + osVersion = getFirstMatch(/os (\d+([_\s]\d+)*) like mac os x/i); osVersion = osVersion.replace(/[_\s]/g, '.'); } else if (android) { - osVersion = getVersion(/android[ \/-](\d+(\.\d+)*)/i); - } else if (windowsphone) { - osVersion = getVersion(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i); - } else if (webos) { - osVersion = getVersion(/(?:web|hpw)os\/(\d+(\.\d+)*)/i); - } else if (rimtablet) { - osVersion = getVersion(/rim\stablet\sos\s(\d+(\.\d+)*)/i); - } else if (bada) { - osVersion = getVersion(/bada\/(\d+(\.\d+)*)/i); - } else if (tizen) { - osVersion = getVersion(/tizen[\/\s](\d+(\.\d+)*)/i); + osVersion = getFirstMatch(/android[ \/-](\d+(\.\d+)*)/i); + } else if (result.windowsphone) { + osVersion = getFirstMatch(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i); + } else if (result.webos) { + osVersion = getFirstMatch(/(?:web|hpw)os\/(\d+(\.\d+)*)/i); + } else if (result.blackberry) { + osVersion = getFirstMatch(/rim\stablet\sos\s(\d+(\.\d+)*)/i); + } else if (result.bada) { + osVersion = getFirstMatch(/bada\/(\d+(\.\d+)*)/i); + } else if (result.tizen) { + osVersion = getFirstMatch(/tizen[\/\s](\d+(\.\d+)*)/i); } if (osVersion) { result.osversion = osVersion; @@ -190,9 +180,9 @@ // device type extraction var osMajorVersion = osVersion.split('.')[0]; - if (tablet || ipad || (android && (osMajorVersion == 3 || (osMajorVersion == 4 && !mobile))) || rimtablet || silk || touchpad) { + if (tablet || iosdevice == 'ipad' || (android && (osMajorVersion == 3 || (osMajorVersion == 4 && !mobile))) || result.silk) { result.tablet = t - } else if (iphone || ipod || (android && mobile) || windowsphone || blackberry || webos || bada || tizen || mobile) { + } else if (mobile || iosdevice == 'iphone' || iosdevice == 'ipod' || android || result.blackberry || result.webos || result.bada) { result.mobile = t } diff --git a/bowser.min.js b/bowser.min.js index a330ae2..059a6c5 100644 --- a/bowser.min.js +++ b/bowser.min.js @@ -3,4 +3,4 @@ * https://github.com/ded/bowser * MIT License | (c) Dustin Diaz 2014 */ -!function(e,t){typeof module!="undefined"&&module.exports?module.exports.browser=t():typeof define=="function"?define(t):this[e]=t()}("bowser",function(){function t(t){function n(e){var n=t.match(e);return n&&n.length>1&&n[1]||""}var r=/msie|trident/i.test(t),i=/chrome|crios|crmo/i.test(t),s=/phantom/i.test(t),o=/ipod/i.test(t),u=!o&&/iphone/i.test(t),a=/ipad/i.test(t),f=u||a||o,l=/silk/i.test(t),c=/like android/i.test(t),h=!c&&/android/i.test(t),p=/opera|opr/i.test(t),d=/firefox|iceweasel/i.test(t),v=/gecko\//i.test(t),m=/seamonkey\//i.test(t),g=/(web|hpw)os/i.test(t),y=/touchpad\//i.test(t),b=/windows phone/i.test(t),w=/blackberry|\bbb\d+/i.test(t),E=/rim\stablet/i.test(t),S=/bada/i.test(t),x=/tizen/i.test(t),T=/safari/i.test(t),N=/applewebkit/i.test(t),C=n(/version\/(\d+(\.\d+)?)/i),k=/mobi/i.test(t),L=/tablet/i.test(t),A;b?A={name:"Windows Phone",windowsphone:e,msie:e,version:n(/iemobile\/(\d+(\.\d+)?)/i)}:p?A={name:"Opera",opera:e,version:C||n(/opr\/(\d+(\.\d+)?)/i)||n(/opera[ \/](\d+(\.\d+)?)/i)}:r?A={name:"Internet Explorer",msie:e,version:n(/(?:msie |rv:)(\d+(\.\d+)?)/i)}:i?A={name:"Chrome",chrome:e,version:n(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)}:s?A={name:"PhantomJS",phantom:e,version:n(/phantomjs\/(\d+(\.\d+)?)/i)}:l?A={name:"Amazon Silk",silk:e,version:n(/silk\/(\d+(\.\d+)?)/i)}:f?(A={name:u?"iPhone":a?"iPad":"iPod"},C&&(A.version=C)):w||E?A={name:"BlackBerry",blackberry:e,version:C||n(/blackberry[\d]+\/(\d+(\.\d+)?)/i)}:g?(A={name:"WebOS",webos:e,version:C||n(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i)},y&&(A.touchpad=e)):S?A={name:"Bada",bada:e,version:n(/dolfin\/(\d+(\.\d+)?)/i)}:x?A={name:"Tizen",tizen:e,version:n(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i)||C}:v?(A={name:"Gecko",gecko:e,mozilla:e,version:n(/(?:firefox|iceweasel)[ \/](\d+(\.\d+)?)/i)},m?(A.name="SeaMonkey",A.seamonkey=e,A.version=n(/seamonkey\/(\d+(\.\d+)?)/i)):d&&(A.name="Firefox",A.firefox=e,/\((mobile|tablet);[^\)]*rv:[\d\.]+\)/i.test(t)&&(A.firefoxos=e))):h?A={name:"Android",version:C}:T?A={name:"Safari",safari:e,version:C}:A={},h||l?A.android=e:f&&(A[u?"iphone":a?"ipad":"ipod"]=e,A.ios=e),N&&(A.webkit=e);var O="";f?(O=n(/os (\d+([_\s]\d+)*) like mac os x/i),O=O.replace(/[_\s]/g,".")):h?O=n(/android[ \/-](\d+(\.\d+)*)/i):b?O=n(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i):g?O=n(/(?:web|hpw)os\/(\d+(\.\d+)*)/i):E?O=n(/rim\stablet\sos\s(\d+(\.\d+)*)/i):S?O=n(/bada\/(\d+(\.\d+)*)/i):x&&(O=n(/tizen[\/\s](\d+(\.\d+)*)/i)),O&&(A.osversion=O);var M=O.split(".")[0];if(L||a||h&&(M==3||M==4&&!k)||E||l||y)A.tablet=e;else if(u||o||h&&k||b||w||g||S||x||k)A.mobile=e;return A.msie&&A.version>=9||A.chrome&&A.version>=20||A.firefox&&A.version>=10||A.safari&&A.version>=5||A.opera&&A.version>=10||A.ios&&A.osversion&&A.osversion.split(".")[0]>=6?A.a=e:A.msie&&A.version<9||A.chrome&&A.version<20||A.firefox&&A.version<10||A.safari&&A.version<5||A.opera&&A.version<10||A.ios&&A.osversion&&A.osversion.split(".")[0]<6?A.c=e:A.x=e,A}var e=!0,n=t(typeof navigator!="undefined"?navigator.userAgent:"");return n._detect=t,n}) \ No newline at end of file +!function(e,t){typeof module!="undefined"&&module.exports?module.exports.browser=t():typeof define=="function"?define(t):this[e]=t()}("bowser",function(){function t(t){function n(e){var n=t.match(e);return n&&n.length>1&&n[1]||""}var r=n(/(ipod|iphone|ipad)/i).toLowerCase(),i=/like android/i.test(t),s=!i&&/android/i.test(t),o=n(/version\/(\d+(\.\d+)?)/i),u=/tablet/i.test(t),a=!u&&/[^-]mobi/i.test(t),f;/opera|opr/i.test(t)?f={name:"Opera",opera:e,version:o||n(/(?:opera|opr)[\s\/](\d+(\.\d+)?)/i)}:/windows phone/i.test(t)?f={name:"Windows Phone",windowsphone:e,msie:e,version:n(/iemobile\/(\d+(\.\d+)?)/i)}:/msie|trident/i.test(t)?f={name:"Internet Explorer",msie:e,version:n(/(?:msie |rv:)(\d+(\.\d+)?)/i)}:/chrome|crios|crmo/i.test(t)?f={name:"Chrome",chrome:e,version:n(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i)}:r?(f={name:r=="iphone"?"iPhone":r=="ipad"?"iPad":"iPod"},o&&(f.version=o)):/gecko\//i.test(t)?(f={name:"Gecko",gecko:e,mozilla:e,version:n(/(?:firefox|iceweasel)[ \/](\d+(\.\d+)?)/i)},/seamonkey\//i.test(t)?(f.name="SeaMonkey",f.seamonkey=e,f.version=n(/seamonkey\/(\d+(\.\d+)?)/i)):/firefox|iceweasel/i.test(t)&&(f.name="Firefox",f.firefox=e,/\((mobile|tablet);[^\)]*rv:[\d\.]+\)/i.test(t)&&(f.firefoxos=e))):/silk/i.test(t)?f={name:"Amazon Silk",silk:e,version:n(/silk\/(\d+(\.\d+)?)/i)}:s?f={name:"Android",version:o}:/phantom/i.test(t)?f={name:"PhantomJS",phantom:e,version:n(/phantomjs\/(\d+(\.\d+)?)/i)}:/blackberry|\bbb\d+/i.test(t)||/rim\stablet/i.test(t)?f={name:"BlackBerry",blackberry:e,version:o||n(/blackberry[\d]+\/(\d+(\.\d+)?)/i)}:/(web|hpw)os/i.test(t)?(f={name:"WebOS",webos:e,version:o||n(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i)},/touchpad\//i.test(t)&&(f.touchpad=e)):/bada/i.test(t)?f={name:"Bada",bada:e,version:n(/dolfin\/(\d+(\.\d+)?)/i)}:/tizen/i.test(t)?f={name:"Tizen",tizen:e,version:n(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i)||o}:/safari/i.test(t)?f={name:"Safari",safari:e,version:o}:f={},s||f.silk?f.android=e:r&&(f[r]=e,f.ios=e),/applewebkit/i.test(t)&&(f.webkit=e);var l="";r?(l=n(/os (\d+([_\s]\d+)*) like mac os x/i),l=l.replace(/[_\s]/g,".")):s?l=n(/android[ \/-](\d+(\.\d+)*)/i):f.windowsphone?l=n(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i):f.webos?l=n(/(?:web|hpw)os\/(\d+(\.\d+)*)/i):f.blackberry?l=n(/rim\stablet\sos\s(\d+(\.\d+)*)/i):f.bada?l=n(/bada\/(\d+(\.\d+)*)/i):f.tizen&&(l=n(/tizen[\/\s](\d+(\.\d+)*)/i)),l&&(f.osversion=l);var c=l.split(".")[0];if(u||r=="ipad"||s&&(c==3||c==4&&!a)||f.silk)f.tablet=e;else if(a||r=="iphone"||r=="ipod"||s||f.blackberry||f.webos||f.bada)f.mobile=e;return f.msie&&f.version>=9||f.chrome&&f.version>=20||f.firefox&&f.version>=10||f.safari&&f.version>=5||f.opera&&f.version>=10||f.ios&&f.osversion&&f.osversion.split(".")[0]>=6?f.a=e:f.msie&&f.version<9||f.chrome&&f.version<20||f.firefox&&f.version<10||f.safari&&f.version<5||f.opera&&f.version<10||f.ios&&f.osversion&&f.osversion.split(".")[0]<6?f.c=e:f.x=e,f}var e=!0,n=t(typeof navigator!="undefined"?navigator.userAgent:"");return n._detect=t,n}) \ No newline at end of file diff --git a/src/bowser.js b/src/bowser.js index ac79ebf..f385175 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -11,126 +11,69 @@ function detect(ua) { - function getVersion(regex) { + function getFirstMatch(regex) { var match = ua.match(regex); return (match && match.length > 1 && match[1]) || ''; } - var ie = /msie|trident/i.test(ua) - , chrome = /chrome|crios|crmo/i.test(ua) - , phantom = /phantom/i.test(ua) - , ipod = /ipod/i.test(ua) - , iphone = !ipod && /iphone/i.test(ua) - , ipad = /ipad/i.test(ua) - , ios = iphone || ipad || ipod - , silk = /silk/i.test(ua) + var iosdevice = getFirstMatch(/(ipod|iphone|ipad)/i).toLowerCase() , likeAndroid = /like android/i.test(ua) , android = !likeAndroid && /android/i.test(ua) - , opera = /opera|opr/i.test(ua) - , firefox = /firefox|iceweasel/i.test(ua) - , gecko = /gecko\//i.test(ua) - , seamonkey = /seamonkey\//i.test(ua) - , webos = /(web|hpw)os/i.test(ua) - , touchpad = /touchpad\//i.test(ua) - , windowsphone = /windows phone/i.test(ua) - , blackberry = /blackberry|\bbb\d+/i.test(ua) - , rimtablet = /rim\stablet/i.test(ua) - , bada = /bada/i.test(ua) - , tizen = /tizen/i.test(ua) - , safari = /safari/i.test(ua) - , webkit = /applewebkit/i.test(ua) - , versionIdentifier = getVersion(/version\/(\d+(\.\d+)?)/i) - , mobile = /mobi/i.test(ua) + , versionIdentifier = getFirstMatch(/version\/(\d+(\.\d+)?)/i) , tablet = /tablet/i.test(ua) + , mobile = !tablet && /[^-]mobi/i.test(ua) , result - if (windowsphone) result = { - name: 'Windows Phone' - , windowsphone: t - , msie: t - , version: getVersion(/iemobile\/(\d+(\.\d+)?)/i) - } - else if (opera) { + if (/opera|opr/i.test(ua)) { result = { name: 'Opera' , opera: t - , version: versionIdentifier || - getVersion(/opr\/(\d+(\.\d+)?)/i) || - getVersion(/opera[ \/](\d+(\.\d+)?)/i) + , version: versionIdentifier || getFirstMatch(/(?:opera|opr)[\s\/](\d+(\.\d+)?)/i) } } - else if (ie) result = { + else if (/windows phone/i.test(ua)) { + result = { + name: 'Windows Phone' + , windowsphone: t + , msie: t + , version: getFirstMatch(/iemobile\/(\d+(\.\d+)?)/i) + } + } + else if (/msie|trident/i.test(ua)) { + result = { name: 'Internet Explorer' , msie: t - , version: getVersion(/(?:msie |rv:)(\d+(\.\d+)?)/i) + , version: getFirstMatch(/(?:msie |rv:)(\d+(\.\d+)?)/i) } - else if (chrome) { + } + else if (/chrome|crios|crmo/i.test(ua)) { result = { name: 'Chrome' , chrome: t - , version: getVersion(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i) + , version: getFirstMatch(/(?:chrome|crios|crmo)\/(\d+(\.\d+)?)/i) } } - else if (phantom) result = { - name: 'PhantomJS' - , phantom: t - , version: getVersion(/phantomjs\/(\d+(\.\d+)?)/i) - } - else if (silk) result = { - name: 'Amazon Silk' - , silk: t - , version : getVersion(/silk\/(\d+(\.\d+)?)/i) - } - else if (ios) { + else if (iosdevice) { result = { - name : iphone ? 'iPhone' : ipad ? 'iPad' : 'iPod' + name : iosdevice == 'iphone' ? 'iPhone' : iosdevice == 'ipad' ? 'iPad' : 'iPod' } // WTF: version is not part of user agent in web apps if (versionIdentifier) { result.version = versionIdentifier } } - else if (blackberry || rimtablet) { - result = { - name: 'BlackBerry' - , blackberry: t - , version: versionIdentifier || getVersion(/blackberry[\d]+\/(\d+(\.\d+)?)/i) - } - } - else if (webos) { - result = { - name: 'WebOS' - , webos: t - , version: versionIdentifier || getVersion(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i) - }; - touchpad && (result.touchpad = t) - } - else if (bada) { - result = { - name: 'Bada' - , bada: t - , version: getVersion(/dolfin\/(\d+(\.\d+)?)/i) - }; - } - else if (tizen) { - result = { - name: 'Tizen' - , tizen: t - , version: getVersion(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i) || versionIdentifier - }; - } - else if (gecko) { + else if (/gecko\//i.test(ua)) { result = { name: 'Gecko' , gecko: t , mozilla: t - , version: getVersion(/(?:firefox|iceweasel)[ \/](\d+(\.\d+)?)/i) + , version: getFirstMatch(/(?:firefox|iceweasel)[ \/](\d+(\.\d+)?)/i) } - if (seamonkey) { + if (/seamonkey\//i.test(ua)) { result.name = 'SeaMonkey' result.seamonkey = t - result.version = getVersion(/seamonkey\/(\d+(\.\d+)?)/i) - } else if (firefox) { + result.version = getFirstMatch(/seamonkey\/(\d+(\.\d+)?)/i) + } else if (/firefox|iceweasel/i.test(ua)) { result.name = 'Firefox' result.firefox = t if (/\((mobile|tablet);[^\)]*rv:[\d\.]+\)/i.test(ua)) { @@ -138,45 +81,92 @@ } } } - else if (android) result = { + else if (/silk/i.test(ua)) { + result = { + name: 'Amazon Silk' + , silk: t + , version : getFirstMatch(/silk\/(\d+(\.\d+)?)/i) + } + } + else if (android) { + result = { name: 'Android' , version: versionIdentifier } - else if (safari) result = { + } + else if (/phantom/i.test(ua)) { + result = { + name: 'PhantomJS' + , phantom: t + , version: getFirstMatch(/phantomjs\/(\d+(\.\d+)?)/i) + } + } + else if (/blackberry|\bbb\d+/i.test(ua) || /rim\stablet/i.test(ua)) { + result = { + name: 'BlackBerry' + , blackberry: t + , version: versionIdentifier || getFirstMatch(/blackberry[\d]+\/(\d+(\.\d+)?)/i) + } + } + else if (/(web|hpw)os/i.test(ua)) { + result = { + name: 'WebOS' + , webos: t + , version: versionIdentifier || getFirstMatch(/w(?:eb)?osbrowser\/(\d+(\.\d+)?)/i) + }; + /touchpad\//i.test(ua) && (result.touchpad = t) + } + else if (/bada/i.test(ua)) { + result = { + name: 'Bada' + , bada: t + , version: getFirstMatch(/dolfin\/(\d+(\.\d+)?)/i) + }; + } + else if (/tizen/i.test(ua)) { + result = { + name: 'Tizen' + , tizen: t + , version: getFirstMatch(/(?:tizen\s?)?browser\/(\d+(\.\d+)?)/i) || versionIdentifier + }; + } + else if (/safari/i.test(ua)) { + result = { name: 'Safari' , safari: t , version: versionIdentifier } + } else result = {} // set OS flags for platforms that have multiple browsers - if (android || silk) { + if (android || result.silk) { result.android = t - } else if (ios) { - result[iphone ? 'iphone' : ipad ? 'ipad' : 'ipod'] = t + } else if (iosdevice) { + result[iosdevice] = t result.ios = t } - if (webkit) { + if (/applewebkit/i.test(ua)) { result.webkit = t } // OS version extraction var osVersion = ''; - if (ios) { - osVersion = getVersion(/os (\d+([_\s]\d+)*) like mac os x/i); + if (iosdevice) { + osVersion = getFirstMatch(/os (\d+([_\s]\d+)*) like mac os x/i); osVersion = osVersion.replace(/[_\s]/g, '.'); } else if (android) { - osVersion = getVersion(/android[ \/-](\d+(\.\d+)*)/i); - } else if (windowsphone) { - osVersion = getVersion(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i); - } else if (webos) { - osVersion = getVersion(/(?:web|hpw)os\/(\d+(\.\d+)*)/i); - } else if (rimtablet) { - osVersion = getVersion(/rim\stablet\sos\s(\d+(\.\d+)*)/i); - } else if (bada) { - osVersion = getVersion(/bada\/(\d+(\.\d+)*)/i); - } else if (tizen) { - osVersion = getVersion(/tizen[\/\s](\d+(\.\d+)*)/i); + osVersion = getFirstMatch(/android[ \/-](\d+(\.\d+)*)/i); + } else if (result.windowsphone) { + osVersion = getFirstMatch(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i); + } else if (result.webos) { + osVersion = getFirstMatch(/(?:web|hpw)os\/(\d+(\.\d+)*)/i); + } else if (result.blackberry) { + osVersion = getFirstMatch(/rim\stablet\sos\s(\d+(\.\d+)*)/i); + } else if (result.bada) { + osVersion = getFirstMatch(/bada\/(\d+(\.\d+)*)/i); + } else if (result.tizen) { + osVersion = getFirstMatch(/tizen[\/\s](\d+(\.\d+)*)/i); } if (osVersion) { result.osversion = osVersion; @@ -184,9 +174,9 @@ // device type extraction var osMajorVersion = osVersion.split('.')[0]; - if (tablet || ipad || (android && (osMajorVersion == 3 || (osMajorVersion == 4 && !mobile))) || rimtablet || silk || touchpad) { + if (tablet || iosdevice == 'ipad' || (android && (osMajorVersion == 3 || (osMajorVersion == 4 && !mobile))) || result.silk) { result.tablet = t - } else if (iphone || ipod || (android && mobile) || windowsphone || blackberry || webos || bada || tizen || mobile) { + } else if (mobile || iosdevice == 'iphone' || iosdevice == 'ipod' || android || result.blackberry || result.webos || result.bada) { result.mobile = t }