JavaFX JSObject stop working after a few minutes?

早过忘川 提交于 2019-12-04 10:40:35

To make the solution more obvious: the approach suggested by @sillyfly works.

Instead of creating a new Bridge (or in my case Console) instance for every state change, make a single instance and reassign it every time. Something along the following lines.

public class AppController {
    public WebView webview;

    @FXML
    public void initialize() {
        final Console console = new Console();
        final WebEngine engine = webview.getEngine();
        engine.setJavaScriptEnabled(true);
        engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
            @Override
            public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {
                final JSObject window = (JSObject) engine.executeScript("window");
                window.setMember("console", console);
            }
        });
    }

    public class Console {
        public void log(String text) {
            System.out.println("[WebView console] " + text);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!