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 2 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
23 changes: 7 additions & 16 deletions src/main/java/com/faforever/client/fx/WebViewConfigurer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
Expand Down Expand Up @@ -58,19 +55,13 @@ 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;
}

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 + "');");
}
engine.executeScript(
"document.onclick = function (elt) {" +
" document.querySelectorAll(\"a[target]\").forEach(e => {" +
" if (!e.href.includes(\"javascript\")) " +
" e.href = \"javascript:java.openUrl('\" + e.href + \"')\"" +
" });" +
"}");
Copy link
Member

Choose a reason for hiding this comment

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

I don't quite understand this function. So everytime the document is clicked it is swapping out the href of all the links in the document right? When someone first clicks a link wouldn't this not work because the onclick event propagates from the inside out? Also this assumes the event did not consume the event and stop propagation right?

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if MutationObserver is supported by webview and we would rather use that. https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yea your understanding is correct, its not great. it does seem to work on all the links in the news page, even on first click. Yes it assumes the event was not consumed.

Im happy to explore if mutationobserver will work because I dont like this solution at all, it was a starting point. Another option I explored is finding the href of the clicked element which was infeasible since sometimes its the clicked element, or a direct parent or child, or a descendant of either.

});
}
}
Loading