【问题标题】:Headless Chrome rendering full page无头 Chrome 渲染整页
【发布时间】:2017-04-14 21:27:05
【问题描述】:

当前无头 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 个像素后截取屏幕截图。问题是如何驱动滚动部分?注入自定义JS会更好还是通过Chrome远程界面可行?

【问题讨论】:

    标签: node.js google-chrome headless-browser


    【解决方案1】:

    你见过吗?

    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);
      });
    

    【讨论】:

    • 这是一个好的开始,但是延迟加载的图片会显示它们的占位符
    【解决方案2】:

    Chrome 远程界面支持使用 Input 域模拟滚动手势。

    // scroll down y axis 9000px 
    Input.synthesizeScrollGesture({x: 500, y: 500, yDistance: -9000});
    

    更多信息: https://chromedevtools.github.io/devtools-protocol/tot/Input/

    您可能还对仿真域感兴趣。 dpd 的答案包含一些现已删除的方法。我相信 Emulation.setVisibleSize 可能对你有用。

    https://chromedevtools.github.io/devtools-protocol/tot/Emulation/

    【讨论】:

      猜你喜欢
      • 2019-06-26
      • 2020-11-19
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 2013-08-12
      • 2022-10-31
      相关资源
      最近更新 更多