gpt4 book ai didi

Java Swing webEngine.executeScript 未在 HTML Javafx web View 中触发

转载 作者:行者123 更新时间:2023-12-02 10:02:15 29 4
gpt4 key购买 nike

嘿,我已经从事这个工作有一段时间了。从迄今为止我看到的所有例子中,我似乎无法得到正确的结果。看起来很容易,但我不知所措。

我的 HTML:

WebView webView = new WebView();

webView.getEngine().setJavaScriptEnabled(true);
webView.setContextMenuEnabled(false);
webView.getEngine().loadContent(""+
"<!DOCTYPE html>\r\n" +
"<html>\r\n" +
"<head>\r\n" +
"</head>\r\n" +
"<body>" +
"<div class=\"chatbox\" style=\"document.body.style.overflow = 'hidden';\">\r\n" +
" <div class=\"chatlogs\">\r\n" +
" <div class=\"animated animatedFadeInUp fadeInUp\">" +
" <div class=\"chat friend\">\r\n" +
" <div class=\"user-photo\"><img src=\"" + pic1 + "\"></div>\r\n" +
" <p class=\"chat-message\">Whats up!</p>\r\n" +
" <span class=\"timefriend\">11:01pm Oct 25 2019</span>\r\n" +
" </div>\r\n" +
" </div>" +
" <div class=\"animated animatedFadeInUp fadeInUp\">" +
" <div class=\"chat self\">\r\n" +
" <div class=\"user-photo\"><img src=\"" + pic2 + "\"></div>" +
" <p class=\"chat-message\">Not much yo!</p>\r\n" +
" <span class=\"timeself\">11:01 PM | Oct 11 2019</span>\r\n" +
" </div>\r\n" +
" </div>\r\n" +
" <div class=\"animated animatedFadeInUp fadeInUp\">" +
" <div class=\"chat friend\">\r\n" +
" <div class=\"user-photo\"><img src=\"" + pic1 + "\"></div>" +
" <p class=\"chat-message\">Whats up!</p>\r\n" +
" <span class=\"timefriend\">11:01 PM | Oct 11 2019</span>\r\n" +
" </div>\r\n" +
" </div>\r\n" +
" <div class=\"animated animatedFadeInUp fadeInUp\">" +
" <div class=\"chat friend\">\r\n" +
" <div class=\"user-photo\"><img src=\"" + pic1 + "\"></div>" +
" <p class=\"chat-message\">Whats up!</p>\r\n" +
" <span class=\"timefriend\">11:01 PM | Oct 11 2019</span>\r\n" +
" </div>\r\n" +
" </div>\r\n" +
" <div id=\"nextChatHolder\"></div>" +
" </div>\r\n" +
" </div>\r\n" +
" <div class=\"footer\"></div>" +
" <script language=\"javascript\">\r\n" +
" function app.test() {\r\n" +
" window.scrollBy(0, 20); \r\n" +
" alert('done');\r\n" +
" }\r\n" +
" </script>" +
"</body>\r\n" +
"</html>");

这是我正在使用的java(并试图开始工作):

webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>()
{
public void changed(ObservableValue<? extends State> o, State old, final State state)
{
if (state == State.SUCCEEDED)
{
JSObject win = (JSObject) webView.getEngine().executeScript("window");
win.setMember("app", new JavaApp());
//webView.getEngine().executeScript("javaObj.start()");
}
}
});

private class JavaApp {
public void test() {
Platform.exit();
}
}

我认为这样做:

function app.test() {\r\n" + 
window.scrollBy(0, 20); \r\n" +
alert('done');\r\n" +
}

并通过以下方式调用它:

webView.getEngine().executeScript("test()");

连这个都不起作用....../

webView.getEngine().executeScript("alert('testing')");

最佳答案

从 JavaScript 调用 Java 方法

对于从 JavaScript 调用 Java 方法 JavaApp#test(),实现的最重要部分已包含在您的第二代码片段中。只有调用本身仍然丢失,例如

<button onclick='app.test();'>Call JavaApp#test-method</button>

但是,必须考虑两件重要的事情:首先,嵌套类 JavaApp 必须public。其次,应用程序必须持有对 JavaApp 实例的引用,这是 JavaScript 回调执行方法所必需的(请参阅 here调用从 JavaScript 返回 Java)。

因此您必须按如下方式修改代码:

JavaApp javaApp = new JavaApp();             // Hold a reference...
webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
public void changed(ObservableValue<? extends State> o, State old, final State state) {
if (state == State.SUCCEEDED) {
JSObject win = (JSObject) webView.getEngine().executeScript("window");
win.setMember("app", javaApp); // Use the reference...
}
}
});

public class JavaApp {                       // Change the access modifier from private to public...
public void test() {
Platform.exit();
}
}
<小时/>

从 Java 调用 JavaScript 方法

如果您想从 Java 调用 JavaScript 方法,例如test(),这是通过

完成的
webView.getEngine().executeScript("test()");

作为示例,我使用第三个​​代码段中给出的test()方法,但是名称必须app.test()test()。如果调用 webView.getEngine().executeScript("test()");,则执行 test()异常(exception) alert()-方法。这是因为对于某些 JavaScript 方法(例如 window.alert()),WebEngine 将请求转发给回调(在本例中为 onAlert) window.alert())。如果未定义回调,则该请求将被忽略(请参阅 here,用户界面回调部分)。例如,alert() 的回调可以定义如下:

webView.getEngine().setOnAlert(event -> showAlert(event.getData()));

其中 showAlert(String msg) 是自定义方法,msg 包含原始消息(在 的情况下为 done test()-方法),例如:

private void showAlert(String msg){
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Custom Alert");
alert.setHeaderText("");
alert.setContentText(msg);
alert.showAndWait();
}

您可以在 How can I make JavaFX web browser displays alert and confirm message 中找到更多示例.

编辑:

从 JavaScript 调用 Java 方法的示例代码:按下按钮时,从 JavaScript 调用 Java 方法 JavaApp#test(),该方法输出文本 在文本区域中完成

import javafx.application.Application;
import javafx.concurrent.Worker.State;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

public class WebViewExecuteJavaFromJavaScript extends Application {

private TextArea textArea;

private static String HTML_STRING = "" +
"<!DOCTYPE html>\r\n" +
"<html>\r\n" +
" <head>\r\n" +
" </head>\r\n" +
" <body>" +
" <button onclick='app.test();'>Call Java-method JavaApp#test()...</button>" + // Call Java-method JavaApp#test()...
" </body>\r\n" +
"</html>";

@Override
public void start(final Stage stage) {

WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.setJavaScriptEnabled(true);
webEngine.loadContent(HTML_STRING);

JavaApp javaApp = new JavaApp();
webEngine.getLoadWorker().stateProperty().addListener((o, oldState, newState) -> {
if (newState == State.SUCCEEDED) {
JSObject win = (JSObject) webView.getEngine().executeScript("window");
win.setMember("app", javaApp); // Enable call of Java-method from JavaScript...
}
});

textArea = new TextArea();
textArea.setWrapText(true);

VBox root = new VBox();
root.setPadding(new Insets(5));
root.setSpacing(5);
root.getChildren().addAll(webView, textArea);

Scene scene = new Scene(root);
stage.setTitle("Demo: Execute Java from JavaScript");
stage.setScene(scene);
stage.setWidth(450);
stage.setHeight(300);
stage.show();
}

public static void main(String[] args) {
launch(args);
}

public class JavaApp {
public void test() {
textArea.setText(textArea.getText() + "done ");
}
}
}

从 Java 调用 JavaScript 方法的示例代码:按下按钮时,从 Java 调用 JavaScript 方法 test(),输出文本 done 在文本区域和对话框中。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewExecuteJavaScriptFromJava extends Application {

private static String HTML_STRING = "" +
"<!DOCTYPE html>\r\n" +
"<html>\r\n" +
" <head>\r\n" +
" </head>\r\n" +
" <body>" +
" <textarea id=\"textarea\" name=\"textarea\" cols=55 rows=3 wrap=\"virtual\"></textarea>" +
" <script language=\"javascript\">\r\n" +
" function test() {\r\n" +
" textarea.value+='done '; \r\n" +
" alert('done');\r\n" + // Is ignored as long as no callback is defined...
" }\r\n" +
" </script>" +
" </body>\r\n" +
"</html>";

@Override
public void start(final Stage stage) {

WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
webEngine.setJavaScriptEnabled(true);
webEngine.loadContent(HTML_STRING);
webEngine.setOnAlert(event -> showAlert(event.getData())); // Define callback for JavaScript-alert...

Button button = new Button("Call JavaScript-method test()...");
button.setOnAction(event -> webView.getEngine().executeScript("test()")); // Call JavaScript-method test()...

VBox root = new VBox();
root.setPadding(new Insets(5));
root.setSpacing(5);
root.getChildren().addAll(button, webView);

Scene scene = new Scene(root);
stage.setTitle("Demo: Execute JavaScript from Java");
stage.setScene(scene);
stage.setWidth(450);
stage.setHeight(300);
stage.show();
}

public static void main(String[] args) {
launch(args);
}

private void showAlert(String msg){
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Custom Alert");
alert.setHeaderText("");
alert.setContentText(msg);
alert.showAndWait();
}
}

关于Java Swing webEngine.executeScript 未在 HTML Javafx web View 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55541775/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com