How to Debug in java webview→javascript→java in Windows

Asked 2 years ago, Updated 2 years ago, 69 views

How do I debug windows efficiently from javascript to java in the javaFX webview?

The java that runs from javascript is as follows:

window=(JSObject)webArea.getEngine().executeScript("window");

// JavaScript and Java I/F
window.setMember("app", this.app);

Only the javascript part can be debunked by chrome, but I'm in trouble because I can't call java from it. (I want to focus on debacking javascript, so if you have a way to debug javascript in webview, that's fine.)

The development environment is not connected to the Internet.
(Internet-connected machines and development environments are in different environments)

=============================
April 23, 2015 Additional information

I tried to write a comment in the answer, but it was over the number of characters.
Add it to the question.

question.

Based on the answers, we tested the following programs in the Internet environment, but the debugger screen did not open.
Also, html is local, so even if the Firebug Lite path is local, the debugger will not open.
Why?

public class Main extensions Application {

    private String appName;
    protected Stage stage;
    private static main instance;

    // Initial screen size value
    public static final int DEFALUT_WIN_WIN_WIDTH=1024;
    public static final int DEFALUT_WIN_HEIGHT=500;
    // Minimum Screen Size
    public static final int MINIMUM_WIN_WIN_WIDTH = 250;
    public static final int MINIMUM_WIN_HEIGHT=75;

    /**
     * Returns an instance of the Main class.
     * 
     * @return
     */
    public static MaingetInstance() {
        return instance;
    }

    /**
     * Returns the stage.
     * 
     * @return
     */
    public Stage getStage() {
        return this.stage;
    }

    @ Override
    public void start (Stage primaryStage) {
        try{
            Main.instance=this;
            This.stage=primaryStage;

            showView();

        } catch(Exceptione){
            Platform.exit();
        }
    }

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

    void showStage() {
        String strTitle=getAppName();

        // Setting the Title
        This.stage.setTitle(strTitle);
        // Setting the Minimum Screen Size
        This.stage.setMinWidth (MINIMUM_WIN_WIDTH);
        This.stage.setMinHeight(MINIMUM_WIN_HEIGHT);
        // display
        This.stage.show();
    }

    protected void showView() {
        Parent root;
        try{
            root=FXMLLoader.load(getClass().getClassLoader().getResource("test/view/Main.fxml"));
            Scenescene=new Scene(root, DEFALUT_WIN_WIDTH, DEFALUT_WIN_HEIGHT);
            This.stage.setScene(scene);
            showStage();
        } catch(IOExceptione){
            // TODO Auto-Generated Catch Blocks
            e.printStackTrace();
        }
    }

    public String getAppName() {
        return this.appName;
    }
}


public class WebViewController implements Initializable {
    @ FXML
    WebView webArea;

    WebEngine engine = null;
    JSObject window = null;

    String url = null;

    @ Override
    public void initialize (URL arg0, ResourceBundle arg1) {

        engine=webArea.getEngine();

        String appPath;
        appPath=System.getProperty("user.dir");
        final String html = File.separator + "WebContent" + File.separator + "main-view.html";
        url="file:///"+appPath+html;

        engine.setJavaScriptEnabled(true);

        // initial display
        engine.load(url);
    }

    // Assume that you have a button to start Firebug and call this method.
    @ FXML
    void handleFirebugButtonAction(ActionEvent){
        if(engine.getDocument()!=null){
            // Specify the URL of firebug-lite.js in the downloaded Firebug Lite deployments
            String firebugLiteUrl="https://getfirebug.com/firebug-lite.js#startOpened";
            Document document=engine.getDocument();
            Element scriptElement= document.createElement("script");
            scriptElement.setAttribute("type", "text/javascript");
            scriptElement.setAttribute("src", firebugLiteUrl);
            NodeList bodyList= document.getElementsByTagName("body");
            if(bodyList!=null&bodyList.getLength()>0){
                bodyList.item(0).appendChild(scriptElement);
            }
        }
    }
}

<AnchorPane styleClass="Animation_bg"xmlns="http://javafx.com/javafx/8"xmlns:fx="http://javafx.com/fxml/1">
  <children>
     <fx:include source="webArea.fxml" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/gt;
  </children>
</AnchorPane>

<AnchorPane style="-fx-background-color:#FFFFFF;"xmlns="http://javafx.com/javafx/8"xmlns:fx="http://javafx.com/fxml/1"fx:controller="test.view.WebViewController">
  <children>
       <WebView fx:id="webArea" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/gt;

       <Button onAction="#handleFirebugButtonAction"
             text="Firebug"
             AnchorPane.topAnchor="0.0" 
             AnchorPane.leftAnchor="0.0"/>

  </children>
</AnchorPane>

javascript java jquery

2022-09-30 20:25

1 Answers

Is it correct that you are using JavaFX's WebView?

If you want to debug JavaScript for content on WebView, you might want to use Firebug Lite.
https://getfirebug.com/firebuglite

Call the URL of the bookmarklet of the Firebug Lite with WebEngie#executeScript() and the Firebug Lite should start on WebView.

(Additional)
I was told that the environment was not accessible to the Internet, so I thought about how to deal with this situation.
The Firebug Lite can be downloaded here from here.You can place it locally and have it read, but JavaFX's WebView is designed to prevent simultaneous local content from being read when viewing a web page (This is helpful).

So you can set up a web server in your intra environment so that you can view the contents of Firebug Lite downloaded from it and browse through the JavaFX app.

//Assume you have a button to launch Firebug and call this method.
@ FXML
void handleFirebugButtonAction(ActionEvent){
    if(webEngine.getDocument()!=null){
        // Specify the URL of firebug-lite.js in the downloaded Firebug Lite deployments
        String firebugLiteUrl="http://example/firebug-lite/build/firebug-lite.js#startOpened";
        Document document=webEngine.getDocument();
        Element scriptElement= document.createElement("script");
        scriptElement.setAttribute("type", "text/javascript");
        scriptElement.setAttribute("src", firebugLiteUrl);
        NodeList bodyList= document.getElementsByTagName("body");
        if(bodyList!=null&bodyList.getLength()>0){
            bodyList.item(0).appendChild(scriptElement);
        }
    }
}

JavaFX can access the DOM of the content displayed in WebView using the WebEngine#getDocument() method.Using it, we have added <script> elements to the DOM to load the JavaScript code for the Firebug Lite (add #startOpen to the end of the URL, and it will start suddenly).

You need to set up a separate web server, which allows you to start Firebug Lite even in an environment where there is no Internet connection.


2022-09-30 20:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.