57 lines
1.5 KiB
JavaScript
Executable File
57 lines
1.5 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const puppeteer = require('puppeteer');
|
|
const yargs = require('yargs');
|
|
const path = require('path');
|
|
|
|
const argv = yargs
|
|
.usage('Usage: url-to-image <url> [options]')
|
|
.demandCommand(1, 'You need to provide a URL')
|
|
.option('w', {
|
|
alias: 'width',
|
|
describe: 'Set viewport width',
|
|
type: 'number',
|
|
default: 1920,
|
|
})
|
|
.option('h', {
|
|
alias: 'height',
|
|
describe: 'Set viewport height',
|
|
type: 'number',
|
|
default: 1080,
|
|
})
|
|
.option('n', {
|
|
alias: 'name',
|
|
describe: 'Filename for the screenshot',
|
|
type: 'string',
|
|
})
|
|
.help()
|
|
.argv;
|
|
|
|
const url = argv._[0];
|
|
const width = argv.w;
|
|
const height = argv.h;
|
|
const filename = argv.n || `${Date.now()}.jpg`;
|
|
|
|
(async () => {
|
|
const browser = await puppeteer.launch(
|
|
{
|
|
// executablePath: '/usr/bin/google-chrome-stable', // Use real Chrome if available -stable for ARCH derivative like Garuda linux (my os)
|
|
// executablePath: '/usr/bin/firefox', // Adjust based on `which firefox`
|
|
headless: false,
|
|
args: [
|
|
'--disable-blink-features=AutomationControlled',
|
|
'--no-sandbox',
|
|
'--disable-setuid-sandbox',
|
|
'--disable-infobars',
|
|
'--disable-dev-shm-usage'
|
|
]
|
|
}
|
|
);
|
|
const page = await browser.newPage();
|
|
await page.setViewport({ width, height });
|
|
await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 });
|
|
await page.screenshot({ path: path.resolve(filename) });
|
|
await browser.close();
|
|
console.log(`Screenshot saved as ${filename}`);
|
|
})();
|