gpt4 book ai didi

node.js - Headless Chrome 渲染整页

转载 作者:太空宇宙 更新时间:2023-11-03 21:55:00 27 4
gpt4 key购买 nike

当前 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com