Skip to content
This repository was archived by the owner on Jun 24, 2021. It is now read-only.
This repository was archived by the owner on Jun 24, 2021. It is now read-only.

Issue with calling Java method from JavaScript #623

@arunan123

Description

@arunan123

I have the following code, when I run and click anywhere in the webpage, it should invoke fromDom() with the string passed right. But it is not working as expected. It seems stackoverflow community for javafx is dead, no one answers over there. If it is not a bug please forgive me.

`import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
import static org.joox.JOOX.$;

public class Main extends Application {

public String fromDom(String e){
    System.out.println(e);
    return e;
}
@Override
public void start(Stage stage) throws Exception {
    Main main = new Main();
    try {
        stage.setTitle("Test");

        WebView w = new WebView();
        WebEngine e = w.getEngine();
        e.load("https://webscraper.io/test-sites/e-commerce/ajax");
        // create a scene
        Scene scene = new Scene(w, w.getPrefWidth(),
                w.getPrefHeight());
        stage.setScene(scene);
        stage.show();

        JSObject obj = (JSObject) e.executeScript("window");
        obj.setMember("main",main);
        obj.setMember("window.onclick","function(e){\n"+
                "main.fromDom('Hello');\n" +
                "}");
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
public static void main(String[] args) {
    launch(args);
}

}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions