Skip to content

Commit

Permalink
feat: Add tests and fix regex for some obscure android browsers (#1196)
Browse files Browse the repository at this point in the history
* Add tests and fix regex for some obscure android browsers

* More fixes

* Handle Chrome on Android better
  • Loading branch information
robbie-c authored May 24, 2024
1 parent 8a96ac0 commit 9192e0a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
63 changes: 45 additions & 18 deletions src/__tests__/utils/event-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,43 @@ describe(`event-utils`, () => {
expectedBrowser: string
}[] = [
{
name: 'Chrome 91',
name: 'Chrome 91 on Windows',
userAgent:
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
vendor: '',
expectedVersion: 91.0,
expectedBrowser: 'Chrome',
},
{
name: 'Chrome 112 on mac',
userAgent:
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
expectedVersion: 112.0,
expectedBrowser: 'Chrome',
},
{
name: 'Chrome 111 on Linux',
userAgent:
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',

expectedVersion: 111.0,
expectedBrowser: 'Chrome',
},
{
name: 'Chrome 16 on Android',
userAgent:
'Mozilla/5.0 (Linux; U; Android-4.0.3; en-us; Galaxy Nexus Build/IML74K) AppleWebKit/535.7 (KHTML, like Gecko) CrMo/16.0.912.75 Mobile Safari/535.7',
expectedVersion: 16.0,
expectedBrowser: 'Chrome',
},
{
name: 'Chrome 21 iOS',
userAgent:
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/21.0.1180.82 Mobile/9B206 Safari/7534.48.3',
vendor: '',
expectedVersion: 21.0,
expectedBrowser: 'Chrome iOS',
},
{
name: 'Firefox 89',
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
Expand Down Expand Up @@ -89,14 +119,6 @@ describe(`event-utils`, () => {
expectedVersion: 44.17763,
expectedBrowser: 'Microsoft Edge',
},
{
name: 'Chrome 21 iOS',
userAgent:
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/21.0.1180.82 Mobile/9B206 Safari/7534.48.3',
vendor: '',
expectedVersion: 21.0,
expectedBrowser: 'Chrome iOS',
},
{
name: 'UC Browser',
userAgent:
Expand Down Expand Up @@ -160,15 +182,6 @@ describe(`event-utils`, () => {
expectedVersion: 10.1,
expectedBrowser: 'BlackBerry',
},
{
name: 'Android Mobile',
userAgent:
'Mozilla/5.0 (Linux; StarOS Must use __system_property_read_callback() to read; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/100.0.4896.127 Mobile Safari/537.36',
vendor: '',
// TODO should we detect this as Chrome or Android Mobile?
expectedVersion: 100,
expectedBrowser: 'Chrome',
},
{
name: 'Samsung Internet',
userAgent:
Expand Down Expand Up @@ -208,6 +221,20 @@ describe(`event-utils`, () => {
expectedVersion: 106.0,
expectedBrowser: 'Firefox iOS',
},
{
name: 'Android Browser on Galaxy Nexus',
userAgent:
'Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
expectedVersion: 4.0,
expectedBrowser: 'Android Mobile',
},
{
name: 'Android Browser on Galaxy S3',
userAgent:
'Mozilla/5.0 (Linux; Android 4.4.4; en-us; SAMSUNG GT-I9300I Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36',
expectedVersion: 4.4,
expectedBrowser: 'Android Mobile',
},
]

test.each(browserTestcases)('browser version %s', ({ userAgent, vendor, expectedVersion }) => {
Expand Down
18 changes: 10 additions & 8 deletions src/utils/user-agent-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,18 @@ export const detectBrowser = function (user_agent: string, vendor: string | unde
return MICROSOFT_EDGE
} else if (includes(user_agent, 'FBIOS')) {
return FACEBOOK + ' ' + MOBILE
} else if (includes(user_agent, CHROME)) {
return CHROME
} else if (includes(user_agent, 'CriOS')) {
return CHROME_IOS
} else if (includes(user_agent, 'UCWEB') || includes(user_agent, 'UCBrowser')) {
return 'UC Browser'
} else if (includes(user_agent, 'CriOS')) {
return CHROME_IOS // why not just Chrome?
} else if (includes(user_agent, 'CrMo')) {
return CHROME
} else if (includes(user_agent, ANDROID) && includes(user_agent, SAFARI)) {
return ANDROID_MOBILE
} else if (includes(user_agent, CHROME)) {
return CHROME
} else if (includes(user_agent, 'FxiOS')) {
return FIREFOX_IOS
} else if (includes(user_agent, ANDROID)) {
return ANDROID_MOBILE
} else if (includes(user_agent.toLowerCase(), KONQUEROR.toLowerCase())) {
return KONQUEROR
} else if (safariCheck(user_agent, vendor)) {
Expand All @@ -135,7 +137,7 @@ export const detectBrowser = function (user_agent: string, vendor: string | unde
const versionRegexes: Record<string, RegExp[]> = {
[INTERNET_EXPLORER_MOBILE]: [new RegExp('rv:' + BROWSER_VERSION_REGEX_SUFFIX)],
[MICROSOFT_EDGE]: [new RegExp(EDGE + '?\\/' + BROWSER_VERSION_REGEX_SUFFIX)],
[CHROME]: [new RegExp(CHROME + '/' + BROWSER_VERSION_REGEX_SUFFIX)],
[CHROME]: [new RegExp('(' + CHROME + '|CrMo)\\/' + BROWSER_VERSION_REGEX_SUFFIX)],
[CHROME_IOS]: [new RegExp('CriOS\\/' + BROWSER_VERSION_REGEX_SUFFIX)],
'UC Browser': [new RegExp('(UCBrowser|UCWEB)\\/' + BROWSER_VERSION_REGEX_SUFFIX)],
[SAFARI]: [DEFAULT_BROWSER_VERSION_REGEX],
Expand All @@ -146,7 +148,7 @@ const versionRegexes: Record<string, RegExp[]> = {
[KONQUEROR]: [new RegExp('Konqueror[:/]?' + BROWSER_VERSION_REGEX_SUFFIX, 'i')],
// not every blackberry user agent has the version after the name
[BLACKBERRY]: [new RegExp(BLACKBERRY + ' ' + BROWSER_VERSION_REGEX_SUFFIX), DEFAULT_BROWSER_VERSION_REGEX],
[ANDROID_MOBILE]: [new RegExp('android\\s' + BROWSER_VERSION_REGEX_SUFFIX)],
[ANDROID_MOBILE]: [new RegExp('android\\s' + BROWSER_VERSION_REGEX_SUFFIX, 'i')],
[SAMSUNG_INTERNET]: [new RegExp(SAMSUNG_BROWSER + '\\/' + BROWSER_VERSION_REGEX_SUFFIX)],
[INTERNET_EXPLORER]: [new RegExp('(rv:|MSIE )' + BROWSER_VERSION_REGEX_SUFFIX)],
Mozilla: [new RegExp('rv:' + BROWSER_VERSION_REGEX_SUFFIX)],
Expand Down

0 comments on commit 9192e0a

Please sign in to comment.