Skip to content

Commit

Permalink
Force news hubs links to open in default OS browser (#3276)
Browse files Browse the repository at this point in the history
* hook into all links on click and replace with java handler

* fix links via mutation observer

* change ordering to prevent race condition

* remove uneeded Array call

---------

Co-authored-by: obydog002 <>
  • Loading branch information
obydog002 authored Dec 6, 2024
1 parent d732ff0 commit a6c5abd
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/main/java/com/faforever/client/fx/WebViewConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,36 @@ public void configureWebView(WebView webView) {
themeService.registerWebView(webView);

((JSObject) engine.executeScript("window")).setMember(JAVA_REFERENCE_IN_JAVASCRIPT, browserCallback);

Document document = webView.getEngine().getDocument();
if (document == null) {
return;
}

engine.executeScript("""
let obs = new MutationObserver((mutations, observer) => {
const addedNodes = mutations.flatMap(mut => Array.from(mut.addedNodes));
const links = [];
for (const node of addedNodes) {
if (node?.querySelectorAll) {
links.push(...document.querySelectorAll("a"));
}
}
for (const elt of links) {
if (!elt.href.includes("javascript:java.openUrl")) {
elt.setAttribute("href", "javascript:java.openUrl('" + elt.href + "')");
}
}
});
obs.observe(document.body, {subtree:true, childList:true});
""");

NodeList nodeList = document.getElementsByTagName("a");
for (int i = 0; i < nodeList.getLength(); i++) {
Element link = (Element) nodeList.item(i);
String href = link.getAttribute("href");

link.setAttribute("href", "javascript:java.openUrl('" + href + "');");
if (!href.contains("javascript:java.openUrl")) {
link.setAttribute("href", "javascript:java.openUrl('" + href + "');");
}
}
});
}
Expand Down

0 comments on commit a6c5abd

Please sign in to comment.