5

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?

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
Adesh singh
  • 2,083
  • 9
  • 24
  • 36

2 Answers2

8

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();
Community
  • 1
  • 1
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
-1

You can use ScriptEngine to execute a javascript. initialize ScriptEngine, then load the script in and evaluate it, as shown in below mentioned code

ScriptEngineManager manager = new ScriptEngineManager();  
ScriptEngine engine = manager.getEngineByName("JavaScript");  

// JavaScript code in a String  
String script = "function hello(arg) { print(arg); }";  

// evaluate script  
engine.eval(script);
M. Atif Riaz
  • 492
  • 1
  • 9
  • 22
  • 2
    `ScriptEngine` allows you to execute a script but not to execute a script in the context of a `WebEngine` document model. – jewelsea Dec 26 '12 at 16:05