简体   繁体   English

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

[英]Headless node.js javascript browser with screenshot capability?

Are there any headless browsers for node.js that support dumping a rendered page out to a file? 是否有任何无头浏览器支持将呈现的页面转储到文件中的node.js? I know phantomjs supports rendering to a file, but it doesn't run on node.js. 我知道phantomjs支持渲染到文件,但它不能在node.js上运行。 I know zombie.js is a node.js headless browser, but it doesn't support rendering to a file. 我知道zombie.js是一个node.js无头浏览器,但它不支持渲染到文件。

I doubt you will find anything that is going to work as well as phantomjs. 我怀疑你会发现任何与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. 我只是将渲染视为异步后端进程,并在主node.js进程的子进程中执行幻像并将其调用一天。 Rendering a web page is HARD, and since phantom is based on WebKit, it can actually do it. 渲染网页很难,因为幻像基于WebKit,它实际上可以做到。 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会更加无缝地与节点集成。

尝试噩梦 ,它使用电子,它比phantomjs更快,它的API很容易使用现代的ES6 javascript。

This might look like a solution with a little bit overhead... 这可能看起来像一个有点开销的解决方案......

You can use the Mozilla Firefox with the MozRepl plugin. 您可以将Mozilla Firefox与MozRepl插件一起使用。 Basically this plugin gives you a telnet port to your Firefox which allows you to control the browser from the outside. 基本上这个插件为您提供了一个到您的Firefox的telnet端口,允许您从外部控制浏览器。 You can open URLs, take screenshots, etc. Running the Firefox with the Xvfb server will run it in headless mode. 您可以打开URL,截取屏幕截图等。使用Xvfb服务器运行Firefox将以无头模式运行它。

Now you just have to control the browser from the outside with node.js. 现在你只需要使用node.js从外部控制浏览器。 I've seen a few examples where someone has implemented a http alike interface inside the chrome.js of Firefox. 我已经看到一些例子,其中有人在Fi​​refox的chrome.js中实现了一个http相似的界面。 So you can run a http command to get a screenshot. 因此,您可以运行http命令来获取屏幕截图。 You can then use http calls from node.js. 然后,您可以使用node.js的http调用。 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. 我在生产中使用Perl Mojolicious在异步模式下运行稍微修改过的版本以触发屏幕截图。 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. 当需要插件时它们可以正常工作,但Flash通常在可见区域时被激活,这不会发生,因此电影/闪光灯可能无法初始化。

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. 我知道有一个名为“ wkhtmltopdf ”的基于webkit的工具包括使用QT web-kit小部件的javascript支持。 It outputs a visual representation ("screenshot" if you will) of the page in PDF format. 它以PDF格式输出页面的可视化表示(如果您愿意,则为“屏幕截图”)。

FWIW, there are also PHP bindings for it here: php-wkthmltox FWIW,这里还有PHP绑定: php-wkthmltox

There's a project called Node-Chimera. 有一个名为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. 虽然它没有Phantomjs那么成熟,但它具有您提到的所有功能:它在本机Nodej上运行,并允许您将页面呈现到文件中。 Repository is here: https://github.com/deanmao/node-chimera . 存储库在这里: https//github.com/deanmao/node-chimera It has examples to do exactly what you need. 它有一些例子可以完全满足您的需求。

The Chrome dev team has released Puppeteer which can be used in node. Chrome开发团队发布了可用于节点的Puppeteer It uses Chrome with the headless option. 它使用Chrome和无头选项。

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

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