从 JavaFX 程序为 WebView 执行 Javascript 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14029964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:33:02  来源:igfitidea点击:

Execute a Javascript function for a WebView from a JavaFX program

javajavascriptjavafx-2javafx

提问by Adesh singh

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 函数?

回答by Sergey Grinev

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

要在其中运行 javascript,WebView您可以使用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://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();

回答by M. Atif Riaz

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