forked from recodehive/awesome-github-profiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture-screenshot.js
49 lines (42 loc) · 1.46 KB
/
capture-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
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
async function takeScreenshot(username) {
const url = `https://github.com/${username}`;
const browser = await puppeteer.launch({
headless: true, // Ensure the browser is launched in headless mode
args: ['--no-sandbox', '--disable-setuid-sandbox'] // Add these arguments for compatibility
});
const page = await browser.newPage();
try {
// Set the page to use light mode
await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'light' }]);
await page.goto(url, { waitUntil: 'networkidle2' });
await page.setViewport({ width: 1280, height: 800 });
const screenshotPath = path.join('screenshots', `${username}.png`);
await page.screenshot({ path: screenshotPath, fullPage: true });
await browser.close();
return screenshotPath;
} catch (error) {
await browser.close();
throw error;
}
}
async function main() {
const username = process.argv[2];
if (!username) {
console.error('No username provided');
process.exit(1);
}
const screenshotDir = path.resolve(__dirname, 'screenshots');
if (!fs.existsSync(screenshotDir)) {
fs.mkdirSync(screenshotDir);
}
try {
const screenshotPath = await takeScreenshot(username);
console.log(`Screenshot taken for ${username}: ${screenshotPath}`);
} catch (error) {
console.error(`Error taking screenshot for ${username}:`, error);
}
}
main();