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 <noreply@anthropic.com>

* Preserve cmd+click new tab behavior in createWebViewWith

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Austin Wang 2026-02-18 21:53:36 -08:00 committed by GitHub
parent 0851cff378
commit ab89fab897
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
}