Fix target=_blank links opening Safari — add WKUIDelegate
All checks were successful
CI / build-and-deploy (push) Successful in 21s

Without a UI delegate, WKWebView hands target="_blank" and window.open()
links to the system (Safari). createWebViewWith now loads them in the
existing webview instead.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Krishna Kumar
2026-05-22 15:47:29 -05:00
parent 544fcd442c
commit f796dabcff

View File

@@ -28,7 +28,7 @@ enum ReadingProgress {
// MARK: - BrowserState
@Observable
final class BrowserState: NSObject, WKNavigationDelegate {
final class BrowserState: NSObject, WKNavigationDelegate, WKUIDelegate {
let webView: WKWebView
var isLoading = false
var canGoBack = false
@@ -56,6 +56,7 @@ final class BrowserState: NSObject, WKNavigationDelegate {
scrollHandler = handler
webView.configuration.userContentController.add(handler, name: "scrollProgress")
webView.navigationDelegate = self
webView.uiDelegate = self
webView.load(URLRequest(url: url))
}
@@ -94,6 +95,15 @@ final class BrowserState: NSObject, WKNavigationDelegate {
func webView(_: WKWebView, didFail _: WKNavigation!, withError _: Error) { isLoading = false }
func webView(_: WKWebView, didFailProvisionalNavigation _: WKNavigation!, withError _: Error) { isLoading = false }
// target="_blank" and window.open() load in the same webview instead of opening Safari
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration,
for action: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if let url = action.request.url {
webView.load(URLRequest(url: url))
}
return nil
}
func enterReaderMode(completion: @escaping () -> Void) {
webView.evaluateJavaScript(readerModeJS) { _, _ in
DispatchQueue.main.async {