Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Singlefile | 11,996 | 8 hours ago | 44 | agpl-3.0 | JavaScript | |||||
Web Extension and CLI tool for saving a faithful copy of a complete web page in a single HTML file | ||||||||||
Browsershot | 4,444 | 98 | 27 | 5 days ago | 132 | July 27, 2023 | 4 | mit | PHP | |
Convert HTML to an image, PDF or string | ||||||||||
Puppeteer Examples | 2,377 | 3 years ago | 14 | apache-2.0 | JavaScript | |||||
Puppeteer example scripts for running Headless Chrome from Node. | ||||||||||
Tlapse | 1,955 | 1 | 1 | 5 years ago | 11 | August 24, 2018 | 1 | mit | JavaScript | |
📷 Create a timelapse of your web development... or just automatically take screenshots of your hard work ;) | ||||||||||
Screenshoteer | 1,643 | 1 | 3 years ago | 12 | September 02, 2020 | mit | JavaScript | |||
Make website screenshots and mobile emulations from the command line. | ||||||||||
Capture Website | 1,582 | 19 | 29 | a year ago | 41 | October 26, 2022 | 24 | mit | JavaScript | |
Capture screenshots of websites | ||||||||||
Capture Website Cli | 720 | 1 | 1 | 5 months ago | 16 | February 03, 2022 | 7 | mit | JavaScript | |
Capture screenshots of websites from the command-line | ||||||||||
Storycap | 643 | 2 | 13 | 7 days ago | 39 | July 01, 2022 | 71 | mit | TypeScript | |
A Storybook Addon, Save the screenshot image of your stories :camera: via puppeteer. | ||||||||||
React Screenshot Test | 618 | 1 | 4 | 6 months ago | 45 | July 17, 2021 | 1 | mit | TypeScript | |
A dead simple library to screenshot test React components | ||||||||||
Chromda | 456 | 3 years ago | mit | JavaScript | ||||||
λ 🖼️ Chromda is an AWS Lambda function for capturing screenshots of websites. |
A screenshot tool with puppeteer
期望通过传入一段 HTML,或者一个 URL,然后能输出一张图片。
npm install puppeteer-screenshot
或者
yarn add puppeteer-screenshot
const Screenshot = require('puppeteer-screenshot');
const screenshot = new Screenshot({
storage: {
type: 'filesystem',
path: ''
},
pageOption: {
timeout: 30000,
},
view: {
deviceScaleFactor: 2, // 可以理解为多倍图
width: 750,
height: 1200
},
captureOption: {
type: 'jpeg',
quality: 75,
fullPage: false, // 是否截取全屏
clip: { // 裁剪
x: 0,
y: 0,
width: 400,
height: 400,
},
omitBackground: false, // 是否隐藏背景
encoding: 'binary'
},
hooks: {
beforeCapture: function(){
// do something
},
afterCapture: function(){
// do something
}
}
})
const result = await screenshot.capture('http://www.baidu.com');
// 传入对象方式覆盖options
const result2 = await screenshot.capture({
url: 'http://www.baidu.com',
type: 'url',
view: {
deviceScaleFactor: 1,
width: 750,
height: 1200,
},
});
await screenshot.close();
Browser
实例每个Screenshot
实例只会使用一个Browser
,所以在所有任务完毕后应该销毁Browser
, 否则可能会导致内存泄漏;另外,服务关闭时也应该销毁Browser
。
process.on('exit', code => {
screenshot.close();
});
const screenshot = new Screenshot(options);
参数名 | 类型 | 必填 | 描述 |
---|---|---|---|
optimize | boolean(number) | 否 | 是否压缩图片 |
timeout | number | 否 | 超时时间 |
storage | object(function) | 否 | 存储方式 |
view | object | 否 | 窗口大小 |
captureOption | object | 否 | 截图参数 |
hooks | object | 否 | 钩子 |
更多配置项请参考文档: https://pptr.dev/#?product=Puppeteer&version=v1.9.0&show=api-pagegotourl-options
type
: 目前支持两种:filesystem
,qiniu
, 'custom'。 filesystem
即存储截图到本地文件系统, qiniu
是存储截图到七牛服务, 自定义是自行处理存储;
path
: 本地文件系统路径,默认是process.cwd()
,仅在type: "filesystem"
有效;
accessKey
: 七牛参数
secretKey
: 七牛参数
bucket
: 七牛参数, 存储空间
bucketType
: 七牛参数, public
or private
存储空间
deadline
: 七牛参数, 仅对bucketType
为private
时有效
domain
: 七牛参数, 资源访问域名
func
: 当 type
为 custom
时有效,传出处理存储的函数,func(buffer, storage, captureOption)
注意, 当type
为空时,不执行任何存储动作,直接跳到下一步
deviceScaleFactor
: number, 窗口缩放,默认是 1 ,当数值为 2 可以理解为截图是两倍图;width
: number, 单位px
, 默认 1280height
: number, 单位px
, 默认 720boolean
注意:当isSetRequestInterception
为 true
时,缓存失效
function
例如:
{
interceptedRequest : (interceptedRequest) => {
if (interceptedRequest.url().endsWith('.png') || interceptedRequest.url().endsWith('.jpg'))
interceptedRequest.abort();
else
interceptedRequest.continue();
}
}
详细请参考文档:https://pptr.dev/#?product=Puppeteer&version=v1.9.0&show=api-pagescreenshotoptions
type
: 截图格式,目前只支持jpeg
和png
quality
: 截图质量fullPage
: boolean 是否截取整页,false
是只截取可见部分clip
: 裁剪,具体如下:
{
x: 0,
y: 0,
width: 400,
height: 400,
omitBackground: false // 是否隐藏背景
}
为了更加方便地扩展,提供了四个基本的 hooks:
beforeCreatePage(browser, ctx)
afterCreatePage(page, browser, ctx)
beforeCapture(ctx)
afterCapture(ctx)
如果钩子无法满足扩展的需求,那么可以使用 middleware 来扩展,例如:
screenshot.use({
enable: true,
module: async (ctx, next) => {
// do something...
next();
},
priority: 60,
});
priority
代表执行权重:
priority
< 50: 在页面初始化之前执行
priority
> 50 && priority
< 150: 在截图前执行
priority
> 150 && priority
< 200: 在截图后,存储前执行
priority
> 200 在存储后执行
npm run test