运行 犀牛中的 jsdom

Running jsdom in Nashorn

我想在服务器上渲染 d3 图表。我有 3 个选择:Node、Phantom 和 Nashorn。

我更喜欢 Nashorn,因为我的 API 是 Scala Play,我不想管理另一个进程。 (部署、加载、队列等)

所以现在我需要让 JSDom 在 Nashorn 中工作,这样 D3 才有东西可以渲染。

到目前为止这有效,但我不知道如何添加 jsdom

class Application @Inject() (val messagesApi: MessagesApi) extends api.ApiController {

  def test = ApiAction { implicit request =>
    ok("The API is ready")
  }

  def pptx = Action { implicit request =>
    val manager: ScriptEngineManager = new ScriptEngineManager
    val engine: ScriptEngine = manager.getEngineByName("nashorn")
    engine.eval(new FileReader(Play.getFile("/ext/lodash.js")))
    val output = engine.eval("function hello(){return _.join('Hello world nashorn does this thing'.split(' '), '-');} hello();")
    Ok(output.toString)
  }
}

我正在加载 nashorn 中缺少的 domino in nashorn to run d3. Domino is a server-side DOM implementation based on Mozilla's dom.js. I loaded it using r.js from require.js. You can use 函数的分支以使 require.js 正常工作。

您可以使用无头浏览器 selenium Chrome 驱动程序。

public class Test {
    public static void main(String args[]) {
        System.setProperty("webdriver.chrome.driver", "D:\Program-Files\chromedriver_win32\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");

        WebDriver driver = new ChromeDriver(options);
        try {
           // String html_content = "<html><head></head><body><div>Hello World</div></body></html>";
            //driver.get("data:text/html;charset=utf-8," + html_content);
            driver.get("D:\Projects\Selenium1\inputfile.html");
            JavascriptExecutor js = (JavascriptExecutor) driver;
            String html1 = (String) js.executeScript("return document.head.innerHTML;");
            System.out.println(html1);
            String html = (String) js.executeScript("return document.body.innerHTML;");
            System.out.println(html);
        } finally {
            driver.quit();
        }
    }
}