I have a strange problem; JSBridge doesn't work after the page loads but it does when the button is clicked.
Test site:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body bgcolor="#A0BEC4">
<input type="button" value="PLAY" onclick="play()"><br><br>
<script>
window.onload = function() { //does not work
stb.play("hello3");
}
function play() { // work
stb.play("hello2");
}
window.addEventListener('load', (event) => {
stb.play("hello4"); //does not work
});
stb.play("hello"); //does not work
function checkJSBridge(){ // !!! WORK !!!
if (typeof (stb.play) === "function") {
stb.play("WORK");
clearTimeout(bridge_timer);
}
}
let bridge_timer = setTimeout(checkJSBridge, 1000);
</script>
</body>
</html>
My code (JavaFX 18.0.1 and JDK 18.0.1.1):
private WebEngine webEngine = webView.getEngine();
private Worker<?> engineWorker = webEngine.getLoadWorker();
private JSBridge bridge = new JSBridge();
@Override
public final void start(Stage stage) throws Exception {
engineWorker.stateProperty().addListener(new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
if (newState == State.SUCCEEDED) {
JSObject jsobj = (JSObject) webEngine.executeScript("window");
jsobj.setMember("stb", bridge);
}
}
});
}