Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force news hubs links to open in default OS browser #3276

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/main/java/com/faforever/client/fx/WebViewConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ 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;
Expand All @@ -68,9 +67,26 @@ public void configureWebView(WebView webView) {
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 + "');");
}

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(...Array.from(document.querySelectorAll("a")));
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only thing besides switching this and the href setting above. But don't we only need to push node.querySelectorAll("a")?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think so

It returns a nodelist and pushing that alone didnt seem to explode it correctly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node of node list is an array? But you are already running queryselector on it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill investigate

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});
""");
});
}
}
Loading