JavaFx的Javascript函数侦听器功能 [英] Javascript function listener functionality for JavaFx

查看:152
本文介绍了JavaFx的Javascript函数侦听器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司正在寻求将嵌入式浏览器切换到JavaFx。但是,我们当前的浏览器目前具有侦听javascript函数的功能,并在调用时调用java。它看起来像这样:

My company is looking to switch up our embedded browser to JavaFx. However, our current browser currently has functionality that listens to a javascript function, and calls back to java whenever it is called. It looks like this:

embeddedBrowser.registerFunction("ajavascriptFunction", new BrowserFunction() {

public JSValue invoke(JSValue... args) {
    //Do callback work
}
});

这不需要修改html方面(一项要求),实际上只需要知道javascript函数名称(我可能会研究更多信息,但这是首选)。

This requires no modification of the html side (a requirement) and in fact only requires knowledge of the javascript function name (I might be able to research more information, but this is highly preferred).

无论如何都要使用 JavaFx 以同样的方式?

Is there anyway to use JavaFx in the same way?

推荐答案

我认为这可以满足您的需求。我从这个问题中借鉴了一个想法来弄清楚javascript:

I think this does what you need. I borrowed an idea from this question to figure out the javascript:

import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

public class JavaScriptFunctionListener extends Application {

    @Override
    public void start(Stage primaryStage) {
        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();

        engine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {
                addFunctionHandlerToDocument(engine);
            }
        });

        // Just a demo: in real life can load external HTML resource:
        engine.loadContent(
                "<html><head><script>"
                + "var count = 0 ;"
                + "function someFunction(x) {"
                + "    count ++ ;"
                + "    document.getElementById(x).innerHTML = 'Count: '+count ;"
                + "}"
                + "</script></head>"
                + "<body>"
                + "    <input type=\"button\" value=\"Click Me\" onclick=\"someFunction('display');\"/>"
                + "    <div id='display'></div>"
                + "</body>"
                + "</html>"
        );

        Button registerButton = new Button("Register handler for 'someFunction'");
        registerButton.setOnAction(event -> {
            registerFunction("someFunction", engine);
            // registering the same function twice will break everything
            // so don't allow this to happen again:
            registerButton.setDisable(true);
        });

        HBox controls = new HBox(5, registerButton);
        controls.setPadding(new Insets(10));
        controls.setAlignment(Pos.CENTER);

        BorderPane root = new BorderPane(webView, null, null, controls, null);
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    private void registerFunction(String functionName, WebEngine engine) {
        engine.executeScript(
            "var fun = " + functionName + " ;"
            + functionName + " = function() {"
            + "    app.functionCalled('" + functionName + "');"
            + "    fun.apply(this, arguments)"
            + "}"
        );
    }

    private void addFunctionHandlerToDocument(WebEngine engine) {
        JSObject window = (JSObject) engine.executeScript("window");
        window.setMember("app", this);
    }

    public void functionCalled(String name) {
        System.out.println(name + " was called");
    }

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

这篇关于JavaFx的Javascript函数侦听器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆