具有屏幕截图功能的无头 node.js javascript 浏览器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7108256/
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-25 22:59:24  来源:igfitidea点击:

Headless node.js javascript browser with screenshot capability?

javascriptbrowsernode.jsheadless-browser

提问by ryeguy

Are there any headless browsers for node.js that support dumping a rendered page out to a file? I know phantomjssupports rendering to a file, but it doesn't run on node.js. I know zombie.jsis a node.js headless browser, but it doesn't support rendering to a file.

是否有支持将渲染页面转储到文件的 node.js 无头浏览器?我知道phantomjs支持渲染到文件,但它不能在 node.js 上运行。我知道zombie.js是一个node.js 无头浏览器,但它不支持渲染到文件。

回答by Peter Lyons

I doubt you will find anything that is going to work as well as phantomjs. I would just treat the rendering as an async backend process and execute phantom in a subprocess from your main node.js process and call it a day. Rendering a web page is HARD, and since phantom is based on WebKit, it can actually do it. I don't think there will ever be a node library that can render a web page to a graphic file that isn't built upon an existing browser rendering engine. But maybe one day phantomjs will integrate more seamlessly with node.

我怀疑你会发现任何和 phantomjs 一样有效的东西。我只是将渲染视为异步后端进程,并在主 node.js 进程的子进程中执行 phantom 并称之为一天。渲染一个网页很困难,而且由于 phantom 是基于 WebKit 的,它实际上可以做到。我认为永远不会有一个节点库可以将网页渲染为不是建立在现有浏览器渲染引擎上的图形文件。但也许有一天 phantomjs 会与 node.js 更无缝地集成。

回答by Rilke Petrosky

Try nightmare, it uses the electron, it is way faster than phantomjs, and it's API easy and uses modern ES6 javascript.

试试nightmare,它使用电子,它比 phantomjs 快得多,而且它的 API 很简单,并且使用现代 ES6 javascript。

回答by David

You might find this helpful, though it's not javascript specific.

您可能会发现这很有帮助,尽管它不是特定于 javascript 的。

There is a webkit-based tool called "wkhtmltopdf" that I understand includes javascript support using the QT web-kit widget. It outputs a visual representation ("screenshot" if you will) of the page in PDF format.

有一个名为“ wkhtmltopdf”的基于 webkit 的工具,据我所知,它包括使用 QT web-kit 小部件的 javascript 支持。它以 PDF 格式输出页面的可视化表示(“屏幕截图”,如果您愿意的话)。

FWIW, there are also PHP bindings for it here: php-wkthmltox

FWIW,这里也有 PHP 绑定:php-wkthmltox

回答by Rhaen

This might look like a solution with a little bit overhead...

这可能看起来像一个有点开销的解决方案......

You can use the Mozilla Firefox with the MozRepl plugin. Basically this plugin gives you a telnet port to your Firefox which allows you to control the browser from the outside. You can open URLs, take screenshots, etc. Running the Firefox with the Xvfb server will run it in headless mode.

您可以将 Mozilla Firefox 与 MozRepl 插件一起使用。基本上这个插件给你一个 telnet 端口到你的 Firefox,它允许你从外部控制浏览器。您可以打开 URL、截取屏幕截图等。使用 Xvfb 服务器运行 Firefox 将在无头模式下运行。

Now you just have to control the browser from the outside with node.js. I've seen a few examples where someone has implemented a http alike interface inside the chrome.js of Firefox. So you can run a http command to get a screenshot. You can then use http calls from node.js. This might look strange, it actually is but might work well for you.

现在你只需要使用 node.js 从外部控制浏览器。我见过一些例子,有人在 Firefox 的 chrome.js 中实现了类似 http 的接口。因此,您可以运行 http 命令来获取屏幕截图。然后,您可以使用来自 node.js 的 http 调用。这可能看起来很奇怪,实际上确实如此,但可能对您有用。

I'm running a slightly modified version in production with Perl Mojolicious in async mode to trigger the screenshots. However, there is a small problem. When plugins are required they do work, however Flash usually gets activated when it's in the visible area, this won't happen so movies/flash things might not get initialized.

我在生产中使用 Perl Mojolicious 在异步模式下运行一个稍微修改过的版本来触发屏幕截图。但是,有一个小问题。当需要插件时,它们确实可以工作,但是 Flash 通常会在它处于可见区域时被激活,这不会发生,因此电影/Flash 内容可能无法初始化。

回答by krakatoa

There's a project called Node-Chimera. Although it's not as mature as Phantomjs, it has all the features you have mentioned: it runs on native Nodejs, and allows you to render pages to a file. Repository is here: https://github.com/deanmao/node-chimera. It has examples to do exactly what you need.

有一个名为 Node-Chimera 的项目。尽管它不如 Phantomjs 成熟,但它具有您提到的所有功能:它在原生 Nodejs 上运行,并允许您将页面渲染到文件中。存储库在这里:https: //github.com/deanmao/node-chimera。它有一些例子可以完全满足您的需求。

回答by kev

The Chrome dev team has released Puppeteerwhich can be used in node. It uses Chrome with the headlessoption.

Chrome 开发团队发布了可以在node.js中使用的Puppeteer。它使用带有无头选项的Chrome 。