Skip to content

Commit

Permalink
Add os.phone and os.tablet (booleans); Detect Firefox mobile, PlayBooks
Browse files Browse the repository at this point in the history
  • Loading branch information
madrobby committed Dec 15, 2012
1 parent 025630b commit b4c610e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 11 deletions.
20 changes: 15 additions & 5 deletions src/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
kindle = ua.match(/Kindle\/([\d.]+)/),
silk = ua.match(/Silk\/([\d._]+)/),
blackberry = ua.match(/(BlackBerry).*Version\/([\d.]+)/),
chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/)
rimtabletos = ua.match(/(RIM\sTablet\sOS)\s([\d.]+)/),
playbook = ua.match(/PlayBook/),
chrome = ua.match(/Chrome\/([\d.]+)/) || ua.match(/CriOS\/([\d.]+)/),
firefox = ua.match(/Firefox\/([\d.]+)/)

// todo clean this up with a better OS/browser
// separation. we need to discern between multiple
// browsers on android, and decide if kindle fire in
// silk mode is android or not
// Todo: clean this up with a better OS/browser seperation:
// - discern (more) between multiple browsers on android
// - decide if kindle fire in silk mode is android or not
// - Firefox on Android doesn't specify the Android version
// - possibly devide in os, device and browser hashes

if (browser.webkit = !!webkit) browser.version = webkit[1]

Expand All @@ -29,10 +33,16 @@
if (webos) os.webos = true, os.version = webos[2]
if (touchpad) os.touchpad = true
if (blackberry) os.blackberry = true, os.version = blackberry[2]
if (rimtabletos) os.rimtabletos = true, os.version = rimtabletos[2]
if (playbook) browser.playbook = true
if (kindle) os.kindle = true, os.version = kindle[1]
if (silk) browser.silk = true, browser.version = silk[1]
if (!silk && os.android && ua.match(/Kindle Fire/)) browser.silk = true
if (chrome) browser.chrome = true, browser.version = chrome[1]
if (firefox) browser.firefox = true, browser.version = firefox[1]

os.tablet = !!(ipad || playbook || (android && !ua.match(/Mobile/)) || (firefox && ua.match(/Tablet/)))
os.phone = !!(!os.tablet && (android || iphone || webos || blackberry || chrome || firefox))
}

detect.call($, navigator.userAgent)
Expand Down
72 changes: 66 additions & 6 deletions test/detect.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ <h1>Browser detection</h1>

Android_1_5: "Mozilla/5.0 (Linux; U; Android 1.5; de-; HTC Magic Build/PLAT-RC33) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1",
Android_2_1: "Mozilla/5.0 (Linux; U; Android 2.1-update1; en-us; Nexus One Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 Chrome/4.1.249.1025",
Android_4_1_1: "Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03O) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19",
Android_4_1_1_Tablet: "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03S) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19",

BlackBerry_6_0_0_141: "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en-GB) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.141 Mobile Safari/534.1+",

Firefox_6_0_2: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:6.0.2) Gecko/20100101 Firefox/6.0.2",
Firefox_Mobile_Simulator: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:2.1.1) Gecko/ Firefox/4.0.2pre Fennec/4.0.1",
PlayBook_1_0_0: "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.8+ (KHTML, like Gecko) Version/0.0.1 Safari/534.8+",
PlayBook_2_1_0: "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML, like Gecko) Version/7.2.1.0 Safari/536.2+",

Opera_11_51: "Opera/9.80 (Macintosh; Intel Mac OS X 10.7.1; U; en) Presto/2.9.168 Version/11.51",
Opera_Mobile_Simulator: "Opera/9.80 (Macintosh; Intel Mac OS X; Opera Mobi/[BUILD_NR]; U; en) Presto/2.7.81 Version/11.00",
Expand All @@ -51,7 +52,12 @@ <h1>Browser detection</h1>
Silk_1_0: "Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Kindle Fire Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",

Chrome_Android_18_0: "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19",
Chrome_iOS_19_0: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3"
Chrome_iOS_19_0: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3",

Firefox_13_Tablet: "Mozilla/5.0 (Android; Tablet; rv:13.0) Gecko/13.0 Firefox/13.0",
Firefox_13_Phone: "Mozilla/5.0 (Android; Mobile; rv:13.0) Gecko/13.0 Firefox/13.0",
Firefox_6_0_2: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:6.0.2) Gecko/20100101 Firefox/6.0.2",
Firefox_Mobile_Simulator: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:2.1.1) Gecko/ Firefox/4.0.2pre Fennec/4.0.1"
}

function detect(ua, callback){
Expand Down Expand Up @@ -89,10 +95,29 @@ <h1>Browser detection</h1>
t.assertTrue(os.android)
t.assertTrue(browser.webkit)
t.assertEqual("1.5", os.version)
t.assertTrue(os.phone)
})
detect(UA.Android_2_1, function(os){
detect(UA.Android_2_1, function(os, browser){
t.assertTrue(os.android)
t.assertTrue(browser.webkit)
t.assertEqual("2.1", os.version)
t.assertTrue(os.phone)
})
detect(UA.Android_4_1_1, function(os, browser){
t.assertTrue(os.android)
t.assertTrue(browser.webkit)
t.assertFalse(!!os.ios)
t.assertEqual("4.1.1", os.version)
t.assertTrue(os.phone)
t.assertFalse(!!os.iphone)
t.assertTrue(browser.chrome)
})
detect(UA.Android_4_1_1_Tablet, function(os, browser){
t.assertTrue(os.android)
t.assertTrue(browser.webkit)
t.assertEqual("4.1.1", os.version)
t.assertTrue(os.tablet)
t.assertTrue(browser.chrome)
})
},

Expand All @@ -103,40 +128,47 @@ <h1>Browser detection</h1>
t.assertTrue(browser.webkit)
t.assertEqual("3.0", os.version)
t.assertEqual("420.1", browser.version)
t.assertTrue(os.phone)
})
detect(UA.iOS_3_1_1_iPod, function(os){
t.assertTrue(os.ios)
t.assertTrue(os.iphone)
t.assertUndefined(os.ipod)
t.assertEqual("3.1.1", os.version)
t.assertTrue(os.phone)
})
detect(UA.iOS_3_2_iPad, function(os){
t.assertTrue(os.ios)
t.assertTrue(os.ipad)
t.assert(!os.iphone)
t.assertEqual("3.2", os.version)
t.assertTrue(os.tablet)
})
detect(UA.iOS_3_2_iPad_2, function(os){
t.assertTrue(os.ios)
t.assertTrue(os.ipad)
t.assert(!os.iphone)
t.assertEqual("3.2", os.version)
t.assertTrue(os.tablet)
})
detect(UA.iOS_4_0_iPhone, function(os){
t.assertTrue(os.ios)
t.assertTrue(os.iphone)
t.assert(!os.ipad)
t.assertEqual("4.0", os.version)
t.assertTrue(os.phone)
})
detect(UA.iOS_4_2_iPad, function(os){
t.assertTrue(os.ios)
t.assertTrue(os.ipad)
t.assertEqual("4.2", os.version)
t.assertTrue(os.tablet)
})
detect(UA.iOS_4_3_iPhone_Simulator, function(os){
t.assertTrue(os.ios)
t.assertTrue(os.iphone)
t.assertEqual("4.3", os.version)
t.assertTrue(os.phone)
})
},

Expand All @@ -145,6 +177,18 @@ <h1>Browser detection</h1>
t.assertTrue(os.blackberry)
t.assertTrue(browser.webkit)
t.assertEqual("6.0.0.141", os.version)
}),
detect(UA.PlayBook_1_0_0, function(os, browser){
t.assertTrue(os.rimtabletos)
t.assertTrue(browser.webkit)
t.assertTrue(os.tablet)
t.assertEqual("1.0.0", os.version)
})
detect(UA.PlayBook_2_1_0, function(os, browser){
t.assertTrue(os.rimtabletos)
t.assertTrue(browser.webkit)
t.assertTrue(os.tablet)
t.assertEqual("2.1.0", os.version)
})
},

Expand Down Expand Up @@ -173,7 +217,23 @@ <h1>Browser detection</h1>
testFirefox: function(t) {
detect(UA.Firefox_6_0_2, function(os, browser){
t.assertFalse(browser.webkit)
t.assertUndefined(browser.version)
t.assertEqual('6.0.2', browser.version)

t.assertTrue(browser.firefox)
})

detect(UA.Firefox_13_Tablet, function(os, browser){
t.assertTrue(browser.firefox)
t.assertFalse(browser.webkit)
t.assertFalse(os.phone)
t.assertTrue(os.tablet)
})

detect(UA.Firefox_13_Phone, function(os, browser){
t.assertTrue(browser.firefox)
t.assertFalse(browser.webkit)
t.assertTrue(os.phone)
t.assertFalse(os.tablet)
})
},

Expand Down

0 comments on commit b4c610e

Please sign in to comment.