webcapt
Version:
A simple cli to screen capture web pages and save them to disk as images or pdfs.
28 lines (27 loc) • 845 B
JavaScript
import { getChromeBrowser } from '../utils/find-chrome.js';
const defaultScreenshotOptions = {
fullPage: true,
};
export async function generateImage({ url, screenshotOptions = {}, }) {
const browser = await getChromeBrowser();
const page = await browser.newPage();
screenshotOptions.type ??= 'png';
try {
await page.setViewport({
width: 1280,
height: 800,
});
await page.goto(url, { waitUntil: 'networkidle0' });
screenshotOptions.path = screenshotOptions.path
? `${screenshotOptions.path}.${screenshotOptions.type}`
: undefined;
await page.screenshot({
...defaultScreenshotOptions,
...screenshotOptions,
});
return screenshotOptions.path;
}
finally {
await page.close();
}
}