cdp-screenshot
chrome headless 通用截图引擎。
安装
命令行安装
npm i -g cdp-screenshot
安装源码包
npm i -S cdp-screenshot
命令
如下图:
自定义
继承截图类
import CDPScreenshot from 'cdp-screenshot';
class MyCDPScreenshot extends CDPScreenshot {
}
自定义属性
injectedCodePaths = [] as string[];
url = 'http://www.taobao.com';
scotFilename = 'scot.png';
viewportWidth = 900;
viewportHeight = 400;
format = CDPScreenshotFormat.PNG;
自定义生命周期
didPageNavigated() {
}
async willScreenshot() {
}
async screenshot() {
if (this.format === CDPScreenshotFormat.PNG) {
await this.screenshotPng();
} else {
await this.screenshotPDF();
}
}
async didScreenshot() {
}
帮助方法
async injectFunction(func: any, ...args: any[]) {
}
async injectPromiseFunction(func: any, ...args: any[]) {
}
async injectCode(code: string) {
}
async injectPromiseCode(code: string) {
return await this.client.Runtime.evaluate({
expression: code,
awaitPromise: true,
});
}
执行截图
const myClient = new MyCDPScreenshot();
myClient.run();