Fix target=_blank links opening Safari — add WKUIDelegate
All checks were successful
CI / build-and-deploy (push) Successful in 21s
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:
@@ -28,7 +28,7 @@ enum ReadingProgress {
|
|||||||
// MARK: - BrowserState
|
// MARK: - BrowserState
|
||||||
|
|
||||||
@Observable
|
@Observable
|
||||||
final class BrowserState: NSObject, WKNavigationDelegate {
|
final class BrowserState: NSObject, WKNavigationDelegate, WKUIDelegate {
|
||||||
let webView: WKWebView
|
let webView: WKWebView
|
||||||
var isLoading = false
|
var isLoading = false
|
||||||
var canGoBack = false
|
var canGoBack = false
|
||||||
@@ -56,6 +56,7 @@ final class BrowserState: NSObject, WKNavigationDelegate {
|
|||||||
scrollHandler = handler
|
scrollHandler = handler
|
||||||
webView.configuration.userContentController.add(handler, name: "scrollProgress")
|
webView.configuration.userContentController.add(handler, name: "scrollProgress")
|
||||||
webView.navigationDelegate = self
|
webView.navigationDelegate = self
|
||||||
|
webView.uiDelegate = self
|
||||||
webView.load(URLRequest(url: url))
|
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, didFail _: WKNavigation!, withError _: Error) { isLoading = false }
|
||||||
func webView(_: WKWebView, didFailProvisionalNavigation _: 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) {
|
func enterReaderMode(completion: @escaping () -> Void) {
|
||||||
webView.evaluateJavaScript(readerModeJS) { _, _ in
|
webView.evaluateJavaScript(readerModeJS) { _, _ in
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
|
|||||||
Reference in New Issue
Block a user