-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenshot.js
58 lines (51 loc) · 2 KB
/
screenshot.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
49
50
51
52
53
54
55
56
57
58
const puppeteer = require('puppeteer');
const fs = require('node:fs');
const yaml = require('js-yaml');
const resizeImg = require('resize-img');
const executablePath = 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe';
async function captureScreenshots() {
try {
const data = fs.readFileSync('docs/_config.yml', 'utf8');
const parsedData = yaml.load(data);
const browser = await puppeteer.launch({
executablePath,
headless: 'new'
});
const page = await browser.newPage();
await page.setViewport({
width: 1540,
height: 771,
});
for (const {image, url, selector, autoscreenshot} of parsedData.links.portfolio) {
// if (autoscreenshot !== 'yes') continue;
await page.goto(url, {
timeout: 0
});
await new Promise(r => setTimeout(r, 2000)); //waiting for the page to load completely
if (selector) {
const selectorArr = await selector.split(',');
for (const item of selectorArr) {
if (await page.$(item)) {
const element = await page.waitForSelector(item);
await element.click();
console.log(`✅ Clicked to '${item}' selector to hide it (${url})`);
} else console.log(`❌ Selector '${item}' isn't found (${url})`);
}
await new Promise(r => setTimeout(r, 1000)); //waiting for the popup to close completely
}
await page.screenshot({path: `docs/assets/screens/${image}`});
console.log(`✅ Screenshot captured ${url} - ${image}`);
const resizeImage = await resizeImg(fs.readFileSync(`docs/assets/screens/${image}`), {
width: 527,
height: 264
});
fs.writeFileSync(`docs/assets/screens/${image}`, resizeImage);
console.log(`✅ Screenshot size was changed to 527x264 (${image})`);
console.log('-------------------------------------------------------------------');
}
await browser.close();
} catch (err) {
console.log("❌ Error: ", err.message);
}
}
captureScreenshots();