-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (70 loc) · 2.25 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
const puppeteer = require("puppeteer");
const fs = require("fs");
const path = require("path");
const { program } = require("commander");
const sites = [];
const delayInSeconds = 3;
(async () => {
program
.option('-n, --name <string>', 'your user in GitHub');
program.parse(process.argv);
const options = program.opts();
const browser = await puppeteer.launch({ headless: "new" });
const fetchprojects = async (sites) => {
try {
const response = await fetch(`https://api.github.com/users/${options.name}/repos`);
const repos = await response.json();
repos
.filter(
(repo) =>
repo.homepage &&
repo.homepage !== "" &&
repo.homepage !== `https://github.com/${options.name}`
)
.map((repo) => {
sites.push(repo.homepage);
});
console.log(sites);
} catch (err) {
console.log("an error ocurred: " + err);
}
};
await fetchprojects(sites);
for (const url of sites) {
const siteName = new URL(url).hostname;
const directoryPath = path.join(__dirname, "screenshots", siteName);
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath);
}
const desktopPage = await browser.newPage();
await desktopPage.setViewport({ width: 1280, height: 720 });
const mobilePage = await browser.newPage();
await mobilePage.emulate(puppeteer.KnownDevices["iPhone X"]);
const pages = [
{ name: "desktop", page: desktopPage },
{ name: "mobile", page: mobilePage },
];
for (const { name, page } of pages) {
await page.goto(url, { waitUntil: "networkidle2" });
await page.evaluate((modalSelector) => {
const modal = document.querySelector(modalSelector);
if (modal) {
modal.style.display = "none";
}
}, "#modal");
await new Promise((resolve) =>
setTimeout(resolve, delayInSeconds * 1000)
);
await page.screenshot({
path: path.join(directoryPath, `${siteName}_${name}.jpeg`),
quality: 100,
type: "jpeg",
});
}
await desktopPage.close();
await mobilePage.close();
}
await browser.close();
console.log("screenshots are ready!");
})(sites, delayInSeconds);