1
0
mirror of https://github.com/lancedikson/bowser synced 2026-03-02 03:40:27 +00:00

made version extraction less error-prone/more future-proof, will now always check

if result of regex match is truthy and array bounds are correct before actually
accessing the version number
This commit is contained in:
BendingBender
2014-02-21 01:23:58 +01:00
parent 24dd55d9b4
commit 0da898f906
3 changed files with 54 additions and 42 deletions

View File

@@ -10,6 +10,11 @@
var t = true,
v /* temporary placeholder for versions. */
function getVersion(ua, regex, matchedIdx) {
var match = ua.match(regex);
return (match && match.length > matchedIdx && match[matchedIdx]) || 0;
}
function detect(ua) {
var ie = /(msie|trident)/i.test(ua)
@@ -40,13 +45,12 @@
name: 'Windows Phone'
, windowsphone: t
, mobile: t
, version: ua.match(/iemobile\/(\d+(\.\d+)?)/i)[1]
, version: getVersion(ua, /iemobile\/(\d+(\.\d+)?)/i, 1)
}
else if (opera) {
if ((v = ua.match(webkitVersion)) && v.length > 1) v = v[1]
else if ((v = ua.match(/opr\/(\d+(\.\d+)?)/i)) && v.length > 1) v = v[1]
else if ((v = ua.match(/opera[ \/](\d+(\.\d+)?)/i)) && v.length > 1) v = v[1]
else v = 0
v = getVersion(ua, webkitVersion, 1) ||
getVersion(ua, /opr\/(\d+(\.\d+)?)/i, 1) ||
getVersion(ua, /opera[ \/](\d+(\.\d+)?)/i, 1);
o = {
name: 'Opera'
, opera: t
@@ -59,16 +63,17 @@
if (chrome) {
o.webkit = t
}
} else if (ie) o = {
}
else if (ie) o = {
name: 'Internet Explorer'
, msie: t
, version: ua.match(/(msie |rv:)(\d+(\.\d+)?)/i)[2]
, version: getVersion(ua, /(msie |rv:)(\d+(\.\d+)?)/i, 2)
}
else if (chrome) o = {
name: 'Chrome'
, webkit: t
, chrome: t
, version: ua.match(/(?:chrome|crios)\/(\d+(\.\d+)?)/i)[1]
, version: getVersion(ua, /(?:chrome|crios)\/(\d+(\.\d+)?)/i, 1)
, ipad: ipad
, iphone: iphone
, ipod: ipod
@@ -80,20 +85,20 @@
name: 'PhantomJS'
, webkit: t
, phantom: t
, version: ua.match(/phantomjs\/(\d+(\.\d+)?)/i)[1]
, version: getVersion(ua, /phantomjs\/(\d+(\.\d+)?)/i, 1)
}
else if (touchpad) o = {
name: 'TouchPad'
, webkit: t
, touchpad: t
, version : ua.match(/touchpad\/(\d+(\.\d+)?)/i)[1]
, version : getVersion(ua, /touchpad\/(\d+(\.\d+)?)/i, 1)
}
else if (silk) o = {
name: 'Amazon Silk'
, webkit: t
, android: t
, mobile: t
, version : ua.match(/silk\/(\d+(\.\d+)?)/i)[1]
, version : getVersion(ua, /silk\/(\d+(\.\d+)?)/i, 1)
}
else if (iphone || ipad || ipod) {
o = {
@@ -107,7 +112,7 @@
}
// WTF: version is not part of user agent in web apps
if (webkitVersion.test(ua)) {
o.version = ua.match(webkitVersion)[1]
o.version = getVersion(ua, webkitVersion, 1)
}
}
else if (blackberry) {
@@ -116,11 +121,11 @@
, blackberry: t
, mobile: t
}
if ((v = ua.match(webkitVersion))) {
o.version = v[1]
if ((v = getVersion(ua, webkitVersion, 1))) {
o.version = v
o.webkit = t
} else {
o.version = ua.match(/blackberry[\d]+\/(\d+(\.\d+)?)/i)[1]
o.version = getVersion(ua, /blackberry[\d]+\/(\d+(\.\d+)?)/i, 1)
}
}
else if (webos) o = {
@@ -128,19 +133,19 @@
, mobile: t
, webkit: t
, webos: t
, version: (ua.match(webkitVersion) || ua.match(/wosbrowser\/(\d+(\.\d+)?)/i))[1]
, version: (getVersion(ua, webkitVersion, 1) || getVersion(ua, /wosbrowser\/(\d+(\.\d+)?)/i, 1))
}
else if (gecko) {
o = {
name: 'Gecko'
, gecko: t
, mozilla: t
, version: ((v = ua.match(firefoxVersion)) && v? v[1] : 0)
, version: getVersion(ua, firefoxVersion, 1)
}
if (seamonkey) {
o.name = 'SeaMonkey'
o.seamonkey = t
o.version = ua.match(/seamonkey\/(\d+(\.\d+)?)/i)[1]
o.version = getVersion(ua, /seamonkey\/(\d+(\.\d+)?)/i, 1)
} else if (firefox) {
o.name = 'Firefox'
o.firefox = t
@@ -158,13 +163,13 @@
, webkit: t
, android: t
, mobile: t
, version: ua.match(webkitVersion)[1]
, version: getVersion(ua, webkitVersion, 1)
}
else if (safari) o = {
name: 'Safari'
, webkit: t
, safari: t
, version: ((v = ua.match(webkitVersion)) ? v[1] : 0)
, version: getVersion(ua, webkitVersion, 1)
}
// Graded Browser Support