Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a Javascript function for a WebView from a JavaFX program

I am trying to execute a Javascript function from a Java program. The Javascript function takes the content of a HTML file and highlights the occurrences of a particular word.

Is it possible to call a Javascript function from a webview object?

like image 470
Adesh singh Avatar asked Dec 25 '12 10:12

Adesh singh


People also ask

How to use WebView in JavaFX?

Java Program to create a WebView and load a website, set the font scale, also set the zoom and display it on the stage: In this program we will create a WebView named webview. We will extract WebEngine from the WebView by using getEngine() method.

Can we use JavaScript in JavaFX?

A JavaFX application can communicate with the web page in which it is embedded by using a JavaScript engine. The host web page can also communicate to embedded JavaFX applications using JavaScript.

Can you use HTML in JavaFX?

It supports Cascading Style Sheets (CSS), JavaScript, Document Object Model (DOM), and HTML5. The embedded browser enables you to perform the following tasks in your JavaFX applications: Render HTML content from local and remote URLs. Obtain Web history.


1 Answers

To run javascript in WebView you can use WebEngine.executeScript() method.

And there are plenty of ways to highlight text by javascript. E.g. Highlight word in HTML text (but not markup)

All together:

    WebView webView = new WebView();
    final WebEngine engine = webView.getEngine();
    engine.load("https://stackoverflow.com/questions/14029964/execute-a-javascript-function-for-a-webview-from-a-javafx-program");

    engine.getLoadWorker().stateProperty().addListener(
            new ChangeListener<State>() {
                @Override
                public void changed(ObservableValue ov, State oldState, State newState) {
                    if (newState == State.SUCCEEDED) {
                        engine.executeScript(
                                "function highlightWord(root,word){"
                                + "  textNodesUnder(root).forEach(highlightWords);"
                                + ""
                                + "  function textNodesUnder(root){"
                                + "    var n,a=[],w=document.createTreeWalker(root,NodeFilter.SHOW_TEXT,null,false);"
                                + "    while(n=w.nextNode()) a.push(n);"
                                + "    return a;"
                                + "  }"
                                + ""
                                + "  function highlightWords(n){"
                                + "    for (var i; (i=n.nodeValue.indexOf(word,i)) > -1; n=after){"
                                + "      var after = n.splitText(i+word.length);"
                                + "      var highlighted = n.splitText(i);"
                                + "      var span = document.createElement('span');"
                                + "      span.style.backgroundColor='#f00';"
                                + "      span.appendChild(highlighted);"
                                + "      after.parentNode.insertBefore(span,after);"
                                + "    }"
                                + "  }"
                                + "}"
                                + "\n"
                                + "highlightWord(document.body,'function');");
                    }
                }
            });


    Scene scene = new Scene(webView, 500, 500);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
like image 119
Sergey Grinev Avatar answered Oct 07 '22 01:10

Sergey Grinev