具有屏幕截图功能的无头 Node.js JavaScript 浏览器?
是否有任何支持将渲染页面转储到文件的 Node.js 无头浏览器?我知道 phantomjs 支持渲染到文件,但它不能在 node.js 上运行。我知道 zombie.js 是一个 Node.js 无头浏览器,但它不支持渲染到文件。
Are there any headless browsers for node.js that support dumping a rendered page out to a file? I know phantomjs supports rendering to a file, but it doesn't run on node.js. I know zombie.js is a node.js headless browser, but it doesn't support rendering to a file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有一个名为 Node-Chimera 的项目。虽然它不像 Phantomjs 那么成熟,但它具有您提到的所有功能:它在本机 Nodejs 上运行,并允许您将页面渲染到文件。存储库位于:https://github.com/deanmao/node-chimera。它有一些示例可以完全满足您的需要。
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.
我怀疑你会找到任何比 phantomjs 更有效的东西。我只是将渲染视为异步后端进程,并在主 Node.js 进程的子进程中执行 phantom,然后就到此为止了。渲染网页很困难,但由于 phantom 基于 WebKit,所以它实际上可以做到。我认为不会有一个节点库可以将网页渲染为不是基于现有浏览器渲染引擎构建的图形文件。但也许有一天 phantomjs 会与 Node 更加无缝地集成。
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.
尝试 nightmare,它使用 Electron,比 phantomjs 快得多,并且 API 简单并使用现代 ES6 javascript 。
Try nightmare, it uses the electron, it is way faster than phantomjs, and it's API easy and uses modern ES6 javascript.
这可能看起来是一个需要一点开销的解决方案...
您可以将 Mozilla Firefox 与 MozRepl 插件一起使用。基本上,这个插件为您提供了 Firefox 的 telnet 端口,允许您从外部控制浏览器。您可以打开 URL、截取屏幕截图等。
使用 Xvfb 服务器运行 Firefox 将以无头模式运行。
现在您只需使用 node.js 从外部控制浏览器即可。我见过一些例子,其中有人在 Firefox 的 chrome.js 中实现了类似 http 的接口。所以你可以运行http命令来获取屏幕截图。然后,您可以使用来自 node.js 的 http 调用。这可能看起来很奇怪,但实际上很奇怪,但可能对你有用。
我正在使用 Perl 在生产中运行稍作修改的版本Mojolicious 在异步模式下触发屏幕截图。然而,有一个小问题。当需要插件时,它们确实可以工作,但是 Flash 通常在位于可见区域时被激活,这种情况不会发生,因此电影/Flash 可能不会被初始化。
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.
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.
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.
Chrome 开发团队发布了可在 Node 中使用的 Puppeteer。它使用带有 headless 选项的 Chrome。
The Chrome dev team has released Puppeteer which can be used in node. It uses Chrome with the headless option.
您可能会发现这很有帮助,尽管它不是特定于 javascript 的。
有一个基于 webkit 的工具,名为“wkhtmltopdf”,据我所知,它包括使用 QT web-kit 小部件的 javascript 支持。它以 PDF 格式输出页面的视觉表示(如果您愿意,可以称为“屏幕截图”)。
FWIW,这里也有 PHP 绑定: php-wkthmltox
You might find this helpful, though it's not javascript specific.
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.
FWIW, there are also PHP bindings for it here: php-wkthmltox