|
| 1 | +export interface AndroidFocusBlurLoopGuardOptions { |
| 2 | + /** |
| 3 | + * Android 运行时检测(默认通过 UA 判断) |
| 4 | + */ |
| 5 | + isAndroid?: () => boolean; |
| 6 | + /** |
| 7 | + * 是否将 iframe 元素视为需拦截目标(默认 true) |
| 8 | + */ |
| 9 | + blockIframeElement?: boolean; |
| 10 | +} |
| 11 | + |
| 12 | +type GuardState = { |
| 13 | + restore: () => void; |
| 14 | +}; |
| 15 | + |
| 16 | +type GuardWindow = Window & |
| 17 | + typeof globalThis & { |
| 18 | + __biochainAndroidFocusBlurLoopGuardState__?: GuardState; |
| 19 | + }; |
| 20 | + |
| 21 | +const GUARD_KEY = '__biochainAndroidFocusBlurLoopGuardState__' as const; |
| 22 | + |
| 23 | +function isBlockedElement(element: Element | null, blockIframeElement: boolean): element is HTMLElement { |
| 24 | + if (!(element instanceof HTMLElement)) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + |
| 28 | + if (element === document.body || element === document.documentElement) { |
| 29 | + return true; |
| 30 | + } |
| 31 | + |
| 32 | + if (blockIframeElement && element instanceof HTMLIFrameElement) { |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + return false; |
| 37 | +} |
| 38 | + |
| 39 | +export function isAndroidUserAgent(userAgent: string): boolean { |
| 40 | + return /android/i.test(userAgent); |
| 41 | +} |
| 42 | + |
| 43 | +function isAndroidRuntime(): boolean { |
| 44 | + if (typeof navigator === 'undefined') { |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | + const nav = navigator as Navigator & { |
| 49 | + userAgentData?: { |
| 50 | + platform?: string; |
| 51 | + }; |
| 52 | + }; |
| 53 | + |
| 54 | + const platform = nav.userAgentData?.platform; |
| 55 | + if (platform && /android/i.test(platform)) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + return isAndroidUserAgent(nav.userAgent); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * 在 Android WebView 环境为全局 focus/blur 死循环做止血补丁。 |
| 64 | + * - 拦截 window blur/focus 事件在 body/html/iframe 激活时的传播 |
| 65 | + * - 禁止对 body/html/iframe 执行 blur() |
| 66 | + * - 对 blur() 做最小重入保护 |
| 67 | + */ |
| 68 | +export function installAndroidFocusBlurLoopGuard( |
| 69 | + options: AndroidFocusBlurLoopGuardOptions = {}, |
| 70 | +): () => void { |
| 71 | + if (typeof window === 'undefined' || typeof document === 'undefined') { |
| 72 | + return () => {}; |
| 73 | + } |
| 74 | + |
| 75 | + const guardWindow = window as GuardWindow; |
| 76 | + const existing = guardWindow[GUARD_KEY]; |
| 77 | + if (existing) { |
| 78 | + return existing.restore; |
| 79 | + } |
| 80 | + |
| 81 | + const checkAndroid = options.isAndroid ?? isAndroidRuntime; |
| 82 | + if (!checkAndroid()) { |
| 83 | + return () => {}; |
| 84 | + } |
| 85 | + |
| 86 | + const blockIframeElement = options.blockIframeElement ?? true; |
| 87 | + const nativeBlur = HTMLElement.prototype.blur; |
| 88 | + let blurring = false; |
| 89 | + |
| 90 | + const eventFirewall = (event: Event) => { |
| 91 | + if (!isBlockedElement(document.activeElement, blockIframeElement)) { |
| 92 | + return; |
| 93 | + } |
| 94 | + event.stopImmediatePropagation(); |
| 95 | + event.stopPropagation(); |
| 96 | + }; |
| 97 | + |
| 98 | + window.addEventListener('blur', eventFirewall, true); |
| 99 | + window.addEventListener('focus', eventFirewall, true); |
| 100 | + |
| 101 | + HTMLElement.prototype.blur = function patchedBlur(this: HTMLElement): void { |
| 102 | + if (isBlockedElement(this, blockIframeElement)) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + if (blurring) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + blurring = true; |
| 111 | + try { |
| 112 | + nativeBlur.call(this); |
| 113 | + } finally { |
| 114 | + queueMicrotask(() => { |
| 115 | + blurring = false; |
| 116 | + }); |
| 117 | + } |
| 118 | + }; |
| 119 | + |
| 120 | + const restore = () => { |
| 121 | + window.removeEventListener('blur', eventFirewall, true); |
| 122 | + window.removeEventListener('focus', eventFirewall, true); |
| 123 | + HTMLElement.prototype.blur = nativeBlur; |
| 124 | + delete guardWindow[GUARD_KEY]; |
| 125 | + }; |
| 126 | + |
| 127 | + guardWindow[GUARD_KEY] = { restore }; |
| 128 | + return restore; |
| 129 | +} |
| 130 | + |
| 131 | +export function uninstallAndroidFocusBlurLoopGuard(): void { |
| 132 | + if (typeof window === 'undefined') { |
| 133 | + return; |
| 134 | + } |
| 135 | + const guardWindow = window as GuardWindow; |
| 136 | + guardWindow[GUARD_KEY]?.restore(); |
| 137 | +} |
0 commit comments