From f01baf9ba7887fca372bf7c7fcabd7e1edf73755 Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Thu, 19 Jun 2025 12:16:42 +0800 Subject: [PATCH] fix: enhance NativeWebView to handle window opening and improve URL parsing error handling --- .../native/webview/native-webview.android.tsx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/mobile/src/components/native/webview/native-webview.android.tsx b/apps/mobile/src/components/native/webview/native-webview.android.tsx index 7f0a48218..e6260dcc2 100644 --- a/apps/mobile/src/components/native/webview/native-webview.android.tsx +++ b/apps/mobile/src/components/native/webview/native-webview.android.tsx @@ -61,6 +61,13 @@ export const NativeWebView: React.ComponentType< allowsBackForwardNavigationGestures allowsFullscreenVideo injectedJavaScriptBeforeContentLoaded={atStart} + // setSupportMultipleWindows={false} + onOpenWindow={(e) => { + const { targetUrl } = e.nativeEvent + if (targetUrl) { + openLink(targetUrl) + } + }} onNavigationStateChange={onNavigationStateChange} onLoadEnd={onLoadEnd} onMessage={(e) => { @@ -99,7 +106,13 @@ const useWebViewNavigation = ({ webViewRef }: { webViewRef: RefObject { const { url: urlStr } = newNavState - const url = URL.canParse(urlStr) ? new URL(urlStr) : null + let url = null + try { + url = new URL(urlStr) + } catch (error) { + console.warn("Invalid URL", urlStr, error) + return + } if (!url) return if (url.protocol === "file:") return // if (allowHosts.has(url.host)) return