从JavaFX程序为WebView执行一个Javascript函数

时间:2021-01-31 17:03:43

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.

我正在尝试从Java程序执行一个Javascript函数。Javascript函数获取HTML文件的内容并突出显示特定单词的出现。

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

是否可以从webview对象调用Javascript函数?

3 个解决方案

#1


7  

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

要在WebView中运行javascript,可以使用WebEngine.executeScript()方法。

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

有很多方法可以通过javascript突出显示文本。在HTML文本中突出显示单词(而不是标记)

All together:

在一起:

    WebView webView = new WebView();
    final WebEngine engine = webView.getEngine();
    engine.load("https://*.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();

#2


2  

Have you checked here

你在这里检查

Scripting Guide.

脚本指南。

#3


-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

可以使用ScriptEngine执行javascript。初始化ScriptEngine,然后加载脚本并对其进行评估,如下面的代码所示

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);

#1


7  

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

要在WebView中运行javascript,可以使用WebEngine.executeScript()方法。

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

有很多方法可以通过javascript突出显示文本。在HTML文本中突出显示单词(而不是标记)

All together:

在一起:

    WebView webView = new WebView();
    final WebEngine engine = webView.getEngine();
    engine.load("https://*.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();

#2


2  

Have you checked here

你在这里检查

Scripting Guide.

脚本指南。

#3


-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

可以使用ScriptEngine执行javascript。初始化ScriptEngine,然后加载脚本并对其进行评估,如下面的代码所示

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);