node.js - Headless Chrome 渲染整页

标签 node.js google-chrome headless-browser

当前 headless Chrome 的问题是没有 API 来渲染整个页面,您只能获得在 CLI 参数中设置的“窗口”。

我正在使用chrome-remote-interface模块,这是捕获示例:

const fs = require('fs');
const CDP = require('chrome-remote-interface');

CDP({ port: 9222 }, client => {

    // extract domains
    const {Network, Page} = client;

    Page.loadEventFired(() => {
        const startTime = Date.now();
        setTimeout(() => {
            Page.captureScreenshot()
            .then(v => {
                let filename = `screenshot-${Date.now()}`;
                fs.writeFileSync(filename + '.png', v.data, 'base64');
                console.log(`Image saved as ${filename}.png`);
                let imageEnd = Date.now();
                console.log('image success in: ' + (+imageEnd - +startTime) + "ms");
                client.close();
            });
        }, 5e3);

    });
    // enable events then start!
    Promise.all([
        // Network.enable(),
        Page.enable()
    ]).then(() => {
        return Page.navigate({url: 'https://google.com'});
    }).catch((err) => {
        console.error(`ERROR: ${err.message}`);
        client.close();
    });
}).on('error', (err) => {
    console.error('Cannot connect to remote endpoint:', err);
});

要渲染整个页面,一种较慢且黑客的解决方案是部分渲染。设置固定高度并滚动页面并在每 X 像素后截取屏幕截图。问题是如何驱动滚动部分呢?注入(inject)自定义 JS 会更好还是可以通过 Chrome 远程接口(interface)实现?

最佳答案

你见过这个吗?

https://medium.com/@dschnr/using-headless-chrome-as-an-automated-screenshot-tool-4b07dffba79a

这听起来像是可以解决您的问题:

  // Wait for page load event to take screenshot
  Page.loadEventFired(async () => {
    // If the `full` CLI option was passed, we need to measure the height of
    // the rendered page and use Emulation.setVisibleSize
    if (fullPage) {
      const {root: {nodeId: documentNodeId}} = await DOM.getDocument();
      const {nodeId: bodyNodeId} = await DOM.querySelector({
        selector: 'body',
        nodeId: documentNodeId,
      });
      const {model: {height}} = await DOM.getBoxModel({nodeId: bodyNodeId});

      await Emulation.setVisibleSize({width: viewportWidth, height: height});
      // This forceViewport call ensures that content outside the viewport is
      // rendered, otherwise it shows up as grey. Possibly a bug?
      await Emulation.forceViewport({x: 0, y: 0, scale: 1});
    }

    setTimeout(async function() {
      const screenshot = await Page.captureScreenshot({format});
      const buffer = new Buffer(screenshot.data, 'base64');
      file.writeFile('output.png', buffer, 'base64', function(err) {
        if (err) {
          console.error(err);
        } else {
          console.log('Screenshot saved');
        }
        client.close();
      });
    }, delay);
  });

关于node.js - Headless Chrome 渲染整页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43419487/

相关文章:

azure - headless 浏览器和 Windows Azure 网站

node.js - Node IPC 如何在 2 个进程之间工作

javascript - 当我使用 "self"调用函数时,为什么会丢失对 "module_object[function_name]();"的引用?

node.js - 检查 req.files 对象是否为空

javascript - 自动提取一些数据并将其显示给用户的 Chrome 扩展程序

javascript - 如何清除 Chrome 中 setAttribute ('onclick' 、 'window.open()' )的属性?

node.js - 为什么我不能使用 Puppeteer 在 exposeFunction() 函数中访问 'window'?

javascript - jade to pug migration => 无法读取函数调用中未定义的属性 'every'

python - 抓取隐藏框架 JavaScript

authentication - 我们如何在 google puppeteer 的 headless chrome 中传递身份验证 token ?