From 17a081ddaa790c1309d21f8c2ef1f16d73ddb4f4 Mon Sep 17 00:00:00 2001 From: obydog002 <> Date: Sun, 24 Nov 2024 18:47:41 +0800 Subject: [PATCH 1/4] hook into all links on click and replace with java handler --- .../client/fx/WebViewConfigurer.java | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/faforever/client/fx/WebViewConfigurer.java b/src/main/java/com/faforever/client/fx/WebViewConfigurer.java index bcb982f46d..ff4f632d79 100644 --- a/src/main/java/com/faforever/client/fx/WebViewConfigurer.java +++ b/src/main/java/com/faforever/client/fx/WebViewConfigurer.java @@ -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) @@ -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 + \"')\"" + + " });" + + "}"); }); } } From 532a18ead7e19a1bd9d7903b8bc0d1635266dda5 Mon Sep 17 00:00:00 2001 From: obydog002 <> Date: Wed, 4 Dec 2024 21:56:07 +0800 Subject: [PATCH 2/4] fix links via mutation observer --- .../client/fx/WebViewConfigurer.java | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/faforever/client/fx/WebViewConfigurer.java b/src/main/java/com/faforever/client/fx/WebViewConfigurer.java index ff4f632d79..3d411c7157 100644 --- a/src/main/java/com/faforever/client/fx/WebViewConfigurer.java +++ b/src/main/java/com/faforever/client/fx/WebViewConfigurer.java @@ -13,6 +13,9 @@ 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) @@ -55,13 +58,35 @@ public void configureWebView(WebView webView) { themeService.registerWebView(webView); ((JSObject) engine.executeScript("window")).setMember(JAVA_REFERENCE_IN_JAVASCRIPT, browserCallback); - engine.executeScript( - "document.onclick = function (elt) {" + - " document.querySelectorAll(\"a[target]\").forEach(e => {" + - " if (!e.href.includes(\"javascript\")) " + - " e.href = \"javascript:java.openUrl('\" + e.href + \"')\"" + - " });" + - "}"); + 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(""" + 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"))); + } + } + 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}); + """); }); } } From 7ac0080eb6e11054d0252ce7ae9526ff29e4c3db Mon Sep 17 00:00:00 2001 From: obydog002 <> Date: Thu, 5 Dec 2024 09:04:27 +0800 Subject: [PATCH 3/4] change ordering to prevent race condition --- .../faforever/client/fx/WebViewConfigurer.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/faforever/client/fx/WebViewConfigurer.java b/src/main/java/com/faforever/client/fx/WebViewConfigurer.java index 3d411c7157..259a428ec6 100644 --- a/src/main/java/com/faforever/client/fx/WebViewConfigurer.java +++ b/src/main/java/com/faforever/client/fx/WebViewConfigurer.java @@ -63,13 +63,6 @@ public void configureWebView(WebView webView) { 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(""" let obs = new MutationObserver((mutations, observer) => { const addedNodes = mutations.flatMap(mut => Array.from(mut.addedNodes)); @@ -87,6 +80,15 @@ public void configureWebView(WebView webView) { }); 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"); + if (!href.contains("javascript:java.openUrl")) { + link.setAttribute("href", "javascript:java.openUrl('" + href + "');"); + } + } }); } } From dfaddf26baa3bf159b1f4ba13b3f3b945e0fb3ee Mon Sep 17 00:00:00 2001 From: obydog002 <> Date: Thu, 5 Dec 2024 20:57:08 +0800 Subject: [PATCH 4/4] remove uneeded Array call --- src/main/java/com/faforever/client/fx/WebViewConfigurer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/faforever/client/fx/WebViewConfigurer.java b/src/main/java/com/faforever/client/fx/WebViewConfigurer.java index 259a428ec6..44c43084e9 100644 --- a/src/main/java/com/faforever/client/fx/WebViewConfigurer.java +++ b/src/main/java/com/faforever/client/fx/WebViewConfigurer.java @@ -69,7 +69,7 @@ public void configureWebView(WebView webView) { const links = []; for (const node of addedNodes) { if (node?.querySelectorAll) { - links.push(...Array.from(document.querySelectorAll("a"))); + links.push(...document.querySelectorAll("a")); } } for (const elt of links) {