I need to run a javascript function from java and save the response in a variable. I wrote the following code but it does not work:
public Object execute(String value) {
SwingUtilities.invokeLater(() -> {
new JFXPanel();
Platform.runLater(() -> {
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.setJavaScriptEnabled(true);
Object response = webEngine.executeScript(value);
Platform.exit();
});
});
return response;
}
I'm using "WebView" because my javascript code, uses functions like "window", "document" and the libraries as "ScriptEngineManage" does not work in my case.
How can i solve this problem? My code works but the "response" is returned before that the executeScript
ends.