Skip to content

Commit

Permalink
feat: Increase accuracy of Chromium Android detection (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-stormtrooper committed Nov 13, 2023
1 parent cf48c7d commit 349d56e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ interface HTMLAnchorElement {
/** Before Safari 15.4. The value is a string in Safari 14. */
attributionsourceid?: number | string
}

interface HTMLMediaElement {
sinkId?: string
}
37 changes: 22 additions & 15 deletions src/utils/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,23 +281,30 @@ export function exitFullscreen(): Promise<void> {
export function isAndroid(): boolean {
const isItChromium = isChromium()
const isItGecko = isGecko()

// Only 2 browser engines are presented on Android.
// Actually, there is also Android 4.1 browser, but it's not worth detecting it at the moment.
if (!isItChromium && !isItGecko) {
return false
}

const w = window

// Chrome removes all words "Android" from `navigator` when desktop version is requested
// Firefox keeps "Android" in `navigator.appVersion` when desktop version is requested
return (
countTruthy([
'onorientationchange' in w,
'orientation' in w,
isItChromium && !('SharedWorker' in w),
isItGecko && /android/i.test(navigator.appVersion),
]) >= 2
)
if (isItChromium) {
return countTruthy([!('SharedWorker' in w), isAndroidNotificationError(), !('sinkId' in new window.Audio())]) >= 2
} else if (isItGecko) {
return countTruthy(['onorientationchange' in w, 'orientation' in w, /android/i.test(navigator.appVersion)]) >= 2
} else {
// Only 2 browser engines are presented on Android.
// Actually, there is also Android 4.1 browser, but it's not worth detecting it at the moment.
return false
}
}

function isAndroidNotificationError() {
try {
new Notification('')
return false
} catch (error: unknown) {
// Details https://bugs.chromium.org/p/chromium/issues/detail?id=481856
if (error instanceof Error && error.message.includes('ServiceWorkerRegistration.showNotification()')) {
return true
}
}
return false
}

0 comments on commit 349d56e

Please sign in to comment.