url2jpg/index.js

60 lines
1.8 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: 'new', // Use the newer headless mode
args: [
'--use-gl=swiftshader', // Enables WebGL rendering
'--enable-webgl', // Ensures WebGL is enabled
'--ignore-gpu-blocklist', // Prevents GPU restrictions
'--disable-software-rasterizer', // Helps with WebGL rendering
'--disable-gpu', // Ensures it runs properly in headless mode
'--disable-dev-shm-usage', // Prevents crashes in Docker/Linux
'--no-sandbox', // Helps in some environments
'--disable-setuid-sandbox'
]
}
);
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}`);
})();