-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScreenshotManager.js
48 lines (40 loc) · 1.19 KB
/
ScreenshotManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const puppeteer = require('puppeteer');
class ScreenshotManager {
browser;
page;
onScreenshot;
onBeforePage;
onPage;
constructor(){}
async init(onLoaded, onScreenshot, onBeforePage, onPage){
this.browser = await puppeteer.launch();
this.page = await this.browser.newPage();
this.onScreenshot = onScreenshot;
this.onBeforePage = onBeforePage;
this.onPage = onPage;
if(onLoaded){
onLoaded();
}
}
async close(){
await this.browser.close();
}
async generate(domain, urls, width, height, fullPage){
await this.page.setViewport({ width, height });
for(const url of urls){
let path = './screenshots/'+(new Date()).getTime()+'.jpg';
if(this.onBeforePage){
this.onBeforePage(url);
}
await this.page.goto(domain+url);
if(this.onPage){
this.onPage(url);
}
await this.page.screenshot({path: path, fullPage: fullPage});
if(this.onScreenshot){
this.onScreenshot(path);
}
}
}
}
module.exports = ScreenshotManager;