Skip to content

Commit 2a0a519

Browse files
authored
Merge pull request #13 from vapidinfinity/main
allow external links to work
2 parents 1d5820d + f93ab3e commit 2a0a519

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Discord/WebView.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ struct WebView: NSViewRepresentable {
145145
// Store a weak reference in Coordinator to break potential cycles
146146
context.coordinator.webView = webView
147147

148-
// Set UI delegate
148+
// delegates
149149
webView.uiDelegate = context.coordinator
150+
webView.navigationDelegate = context.coordinator
150151

151152
// Make background transparent
152153
webView.setValue(false, forKey: "drawsBackground")
@@ -247,7 +248,7 @@ struct WebView: NSViewRepresentable {
247248
// you can do so. Currently, no updates are necessary.
248249
}
249250

250-
class Coordinator: NSObject, WKScriptMessageHandler, WKUIDelegate {
251+
class Coordinator: NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationDelegate {
251252
// Weak reference to avoid strong reference cycles
252253
weak var webView: WKWebView?
253254

@@ -283,6 +284,15 @@ struct WebView: NSViewRepresentable {
283284

284285
func userContentController(_ userContentController: WKUserContentController,
285286
didReceive message: WKScriptMessage) {}
287+
288+
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
289+
if let url = navigationAction.request.url, navigationAction.navigationType == .linkActivated {
290+
NSWorkspace.shared.open(url)
291+
decisionHandler(.cancel)
292+
} else {
293+
decisionHandler(.allow)
294+
}
295+
}
286296
}
287297
}
288298

0 commit comments

Comments
 (0)