51 lines
1.4 KiB
JavaScript
Executable File
51 lines
1.4 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
// const puppeteer = require('puppeteer');
|
|
// getting timeout error for https://interaktogames.beanstalkedu.com/drawing/?id=27
|
|
const yargs = require('yargs');
|
|
const path = require('path');
|
|
|
|
const puppeteer = require('puppeteer-extra');
|
|
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
|
|
|
|
puppeteer.use(StealthPlugin());
|
|
|
|
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({ headless: 'new' });
|
|
const page = await browser.newPage();
|
|
await page.setViewport({ width, height });
|
|
//await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
|
|
await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 });
|
|
await page.screenshot({ path: path.resolve(filename) });
|
|
await browser.close();
|
|
console.log(`Screenshot saved as ${filename}`);
|
|
})();
|