简体   繁体   English

JavaFx:使用WebEngine在服务器程序中获取JavaScript呈现的dom

[英]JavaFx: use webengine to get javascript rendered dom in a server program

In a server program (means no X installed) I really just want to load a html site containing some javascript and get the rendered dom. 在服务器程序中(意味着未安装X),我真的只想加载一个包含一些javascript的html站点并获取呈现的dom。 I want to do this in the way of using a static function. 我想通过使用静态函数的方式来做到这一点。 My problem is that I am not able to get the threads joined/finished. 我的问题是我无法使线程加入/完成。

package svg;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import kic.engine.util.DomUtils;
import javafx.embed.swing.JFXPanel;

public class Render extends Application {
    private static final JFXPanel USELESS = new JFXPanel(); // do I really need this? just to avoid "Toolkit not initialized"

    public static void main(String[] args) throws InterruptedException { 

        //launch(args); 
        compile();
        System.out.println("stop");
    }

    public static void compile() throws InterruptedException {
        Thread t = new Thread() {
            @Override
            public void run() {
                new Render().start(new Stage()); //EDIT: copy/paste mistake
            }
        };

        Platform.runLater(t);
        t.join(); // never get here!
    }

    public void start(final Stage primaryStage) {
        final WebView webView = new WebView();
        final WebEngine engine = webView.getEngine();
        //final WebEngine engine = new WebEngine();

        //engine.documentProperty().addListener( new ChangeListener<Document>() { @Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
        engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() { 
            public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
                if (newState == Worker.State.SUCCEEDED) {
                    System.out.println(DomUtils.getDocumentString(engine.getDocument()));

                    primaryStage.close();
                    //Platform.setImplicitExit(true);

                    Platform.exit();  // platform will not exit ... it will exit if I use Application.launch(), but throws an error: java.lang.IllegalStateException: Attempt to call defer when toolkit not running
                    System.out.println("after exit");
                } 
            }
        });

        engine.load("file:///myD3.html");
        System.out.println("after load");
    }
}

Can someone help me getting a static function just call the webengine and return the dom. 有人可以帮我获取静态函数,只需调用Webengine并返回dom。

EDIT: Even a Thread.currentThread().getThreadGroup().interrupt(); 编辑:即使是Thread.currentThread()。getThreadGroup()。interrupt(); will not stop the Application from running only a System.exit(1) does, but I do not want to shutdown JVM. 不会停止仅运行System.exit(1)的应用程序,但是我不想关闭JVM。

最终发现它是一个Bug: https : //bugs.openjdk.java.net/browse/JDK-8092869

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM