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

Conversation

obydog002
Copy link
Collaborator

Fix the underlying issue preventing links opening correctly in #3262

This PR doesn't add a setting to control it, I assume everyone want the links to open in the OS browser.

Comment on lines 59 to 64
"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.

@obydog002
Copy link
Collaborator Author

@Sheikah45 Mutation observer works nicely to fix any links added to the page

Unsure but perhaps there is still a concern with a race condition between the java code and the js hook? Where a link can get added after the java code runs but before mutation observer is added.

@Sheikah45
Copy link
Member

@Sheikah45 Mutation observer works nicely to fix any links added to the page

Unsure but perhaps there is still a concern with a race condition between the java code and the js hook? Where a link can get added after the java code runs but before mutation observer is added.

The race condition concern would be fixed by first adding the mutation observer and then going through and changing all the links already added to the page. This would guarantee that all the links are set since we observe and then change the preexisting ones.

Comment on lines 77 to 81
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

@obydog002
Copy link
Collaborator Author

We could also prevent the race condition by writing the logic to handle pre-existing links in the js script

@Sheikah45 Sheikah45 merged commit a6c5abd into FAForever:develop Dec 6, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants