gpt4 book ai didi

JavaFx:使用webengine在服务器程序中获取javascript渲染的dom

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:17 25 4
gpt4 key购买 nike

在服务器程序中(意味着没有安装X)我真的只想加载一个包含一些javascript的html站点并获取渲染的dom。我想通过使用静态函数的方式来做到这一点。我的问题是我无法连接/完成线程。

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

有人可以帮我获取一个静态函数,只需调用 Web 引擎并返回 dom。

编辑:甚至 Thread.currentThread().getThreadGroup().interrupt();仅 System.exit(1) 不会阻止应用程序运行,但我不想关闭 JVM。

最佳答案

最后证明,这是一个Bug:https://bugs.openjdk.java.net/browse/JDK-8092869

关于JavaFx:使用webengine在服务器程序中获取javascript渲染的dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18191297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com