From ab89fab897e979cf6e676da8eb9488408d5fe402 Mon Sep 17 00:00:00 2001 From: Austin Wang Date: Wed, 18 Feb 2026 21:53:36 -0800 Subject: [PATCH] Fix browser opening new tabs on link click (#92) * Fix browser panel opening new tabs on every link click Navigate target=_blank and window.open() links in the current webview instead of spawning new browser tabs. Co-Authored-By: Claude Opus 4.6 * Preserve cmd+click new tab behavior in createWebViewWith Co-Authored-By: Claude Opus 4.6 --------- Co-authored-by: Claude Opus 4.6 --- Sources/Panels/BrowserPanel.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/Panels/BrowserPanel.swift b/Sources/Panels/BrowserPanel.swift index 728b63a4..290a1797 100644 --- a/Sources/Panels/BrowserPanel.swift +++ b/Sources/Panels/BrowserPanel.swift @@ -1532,10 +1532,10 @@ private class BrowserNavigationDelegate: NSObject, WKNavigationDelegate { decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void ) { - // target=_blank or window.open() — open in a new tab instead of a new window + // target=_blank or window.open() — navigate in the current webview if navigationAction.targetFrame == nil, let url = navigationAction.request.url { - openInNewTab?(url) + webView.load(URLRequest(url: url)) decisionHandler(.cancel) return } @@ -1558,8 +1558,8 @@ private class BrowserNavigationDelegate: NSObject, WKNavigationDelegate { private class BrowserUIDelegate: NSObject, WKUIDelegate { var openInNewTab: ((URL) -> Void)? - /// Handle cmd+click / target=_blank links. Returning nil tells WebKit not to open a new window; - /// instead we open the URL as a new surface in the same pane. + /// Returning nil tells WebKit not to open a new window. + /// Cmd+click opens in a new tab; regular target=_blank navigates in-place. func webView( _ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, @@ -1567,7 +1567,11 @@ private class BrowserUIDelegate: NSObject, WKUIDelegate { windowFeatures: WKWindowFeatures ) -> WKWebView? { if let url = navigationAction.request.url { - openInNewTab?(url) + if navigationAction.modifierFlags.contains(.command) { + openInNewTab?(url) + } else { + webView.load(URLRequest(url: url)) + } } return nil }