Line data Source code
1 13 : const SOCIAL_APP_PATTERNS =
2 : /FBAN|FBAV|Instagram|Line\/|Snapchat|TikTok|musical_ly/i
3 :
4 13 : const IOS_FIRST_PARTY_BROWSER_PATTERNS = /CriOS|FxiOS|OPiOS|EdgiOS/i
5 :
6 13 : function isAndroidWebView(ua: string): boolean {
7 13 : return /\bwv\b/.test(ua) && /Android/.test(ua)
8 : }
9 :
10 13 : function isIOSWebView(ua: string): boolean {
11 13 : if (!/AppleWebKit/i.test(ua)) return false
12 13 : if (/Safari\//i.test(ua)) return false
13 0 : if (IOS_FIRST_PARTY_BROWSER_PATTERNS.test(ua)) return false
14 0 : return true
15 : }
16 :
17 13 : function isSocialAppBrowser(ua: string): boolean {
18 13 : return SOCIAL_APP_PATTERNS.test(ua)
19 : }
20 :
21 13 : function isFirstPartyIOSBrowser(ua: string): boolean {
22 13 : if (IOS_FIRST_PARTY_BROWSER_PATTERNS.test(ua)) return true
23 13 : if (!/iPhone|iPad|iPod|Macintosh.*Mobile\//i.test(ua)) return false
24 13 : return /Safari\//i.test(ua) && /Version\//i.test(ua)
25 : }
26 :
27 13 : function hasWkWebViewMessageBridge(): boolean {
28 13 : try {
29 13 : const win = globalThis as Record<string, unknown>
30 13 : return (
31 : typeof win.webkit === 'object' &&
32 : win.webkit !== null &&
33 : typeof (win.webkit as Record<string, unknown>).messageHandlers ===
34 : 'object'
35 : )
36 : } catch {
37 0 : return false
38 : }
39 : }
40 :
41 13 : function hasReactNativeWebViewBridge(): boolean {
42 13 : try {
43 : const win = globalThis as Record<string, unknown>
44 13 : return win.ReactNativeWebView != null
45 : } catch {
46 0 : return false
47 : }
48 : }
49 :
50 : /**
51 : * Whether the page runs inside an app's embedded browser, where Google's
52 : * popup sign-in is blocked. Both hosts show the same notice on that answer.
53 : */
54 13 : export function isEmbeddedWebView(ua: string = navigator.userAgent): boolean {
55 13 : if (isSocialAppBrowser(ua)) return true
56 13 : if (isAndroidWebView(ua)) return true
57 13 : if (isIOSWebView(ua)) return true
58 13 : if (hasReactNativeWebViewBridge()) return true
59 13 : if (!isFirstPartyIOSBrowser(ua) && hasWkWebViewMessageBridge()) return true
60 13 : return false
61 : }
|