如何从javafx中的javascript中获取返回值?

时间:2015-05-22 06:58:59

标签: javascript javafx javafx-webengine

我正在我的JavaFX应用程序的webview中执行javascript代码。每当鼠标单击并将元素详细信息转换为java变量时,我需要重复执行它。我正在使用以下代码并使用Firebug Lite。在Firebug控制台中,所需的项目正在打印。但我希望它返回到java应用程序。

engine.documentProperty().addListener(new ChangeListener<Document>() {
            @Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
                enableFirebug(engine);
                Object obj=engine.executeScript("var lastElement = null; "
                        + "document.addEventListener('click', function(e) {"
                        + "if (e.target != lastElement) {"
                        + "lastElement = e.target;"
                        + "console.log(lastElement.name);"
                        + "return lastElement.name;"
                        + "}}, false);");
                System.out.println(obj.toString());
            }
        });

加载页面时会执行,但每次单击鼠标后都不会执行。请建议我如何修改它。

1 个答案:

答案 0 :(得分:2)

尝试反过来调用它。假设您正在将页面加载为

final WebEngine webEngine = webview.getEngine();
webEngine.load("http://localhost/demo/clickHandler/");

我们要为成功的状态设置一个监听器 - 基本上我们要在JavaScript中注入一个Java类并让它回拨给我们。让我们创建一个WebController来检查传入的内容并打印出ID:

public class WebController {
    public void printId(Object object) {
        if (org.w3c.dom.html.HTMLElement.class.isAssignableFrom(object.getClass())) {
            org.w3c.dom.html.HTMLElement it = (org.w3c.dom.html.HTMLElement) object;
            System.out.println("Id is " + it.getId());
        }
    }
}

现在加载成功后,我们会将其作为clickController注入应用。

    webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
          @Override
          public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
              if (newState == State.SUCCEEDED) {
                  JSObject window = (JSObject) webEngine.executeScript("window");
                  window.setMember("clickController", new WebController());
              }
          }
      }
    );

现在我们需要页面上的一些JavaScript给我们回电话。假设您正在使用jQuery,请将此代码添加到您的页面:

$(function () {
    $('*').click(function (event) {
        event.preventDefault();
        event.stopPropagation();
        clickController.printId(this);
    });
});

现在,当点击任何内容时,将回调webController,这将检查对象是否为HTMLElement并打印出其ID(如果没有ID,则为null)

不使用jQuery会让它变得更难,但你可以将它添加到文档的末尾:

var nonJQuery = function (event) {
    clickController.printId(this);
    event.preventDefault();
    event.stopPropagation();
};
var elements = document.querySelectorAll("*");
for (var i = 0; i < elements.length; i++) {
    elements[i].addEventListener("click", nonJQuery, false);
}

如果无法向文档添加内容,则可以执行脚本以在成功函数中添加单击处理程序。更新成功的函数如下所示:

    webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
          @Override
          public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
              if (newState == State.SUCCEEDED) {
                  JSObject window = (JSObject) webEngine.executeScript("window");
                  webEngine.executeScript("var nonJQuery = function (event) {\n" +
                          "    clickController.printId(this);\n" +
                          "    event.preventDefault();\n" +
                          "    event.stopPropagation();\n" +
                          "};\n" +
                          "var elements = document.querySelectorAll(\"*\");\n" +
                          "for (var i = 0; i < elements.length; i++) {\n" +
                          "    elements[i].addEventListener(\"click\", nonJQuery, false);\n" +
                          "}");
                  window.setMember("clickController", new WebController());
              }
          }
      }
    );

最后,如果你想阻止页面导航,例如。从链接中,您可以将return false;添加到JavaScript:

    webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
          @Override
          public void changed(ObservableValue<? extends State> ov, State oldState, State newState) {
              if (newState == State.SUCCEEDED) {
                  JSObject window = (JSObject) webEngine.executeScript("window");
                  webEngine.executeScript("var nonJQuery = function (event) {\n" +
                          "    clickController.printId(this);\n" +
                          "    event.preventDefault();\n" +
                          "    event.stopPropagation();\n" +
                          "    return false;\n" +
                          "};\n" +
                          "var elements = document.querySelectorAll(\"*\");\n" +
                          "for (var i = 0; i < elements.length; i++) {\n" +
                          "    elements[i].addEventListener(\"click\", nonJQuery, false);\n" +
                          "}");
                  window.setMember("clickController", new WebController());
              }
          }
      }
    );