Eric Miller

Eric Miller

// Name: Screenshot URL
import "@johnlindquist/kit"
const { chromium }: typeof import("playwright") = await npm(
"playwright"
)
// get URL from user
let urlFromUser = await arg("Enter the URL to screenshot");
if (!urlFromUser.match(/^https?:\/\//)) {
urlFromUser = `http://${urlFromUser}`;
}
const pathObj = path.parse(urlFromUser);
log(pathObj);
// config
let timeout = 5_000;
const FOLDER = 'Downloads/screenshot-url';
const screenshotFolder = home(FOLDER);
const filename = `${pathObj.name}${pathObj.ext}.png`
const screenshotPath = home(FOLDER, filename);
// Open the window
const browser = await chromium.launch({ timeout, headless: false });
const context = await browser.newContext({ colorScheme: "dark" });
const page = await context.newPage();
await page.setViewportSize({
width: 800,
height: 600,
});
page.setDefaultTimeout(timeout);
try {
// docs: https://playwright.dev/docs/api/class-page
await page.goto(urlFromUser);
await page.screenshot({ path: screenshotPath })
// TODO: shrink the file to a thumbnail
await revealFile(screenshotFolder)
log(`Done`)
} catch (error) {
warn('error', error);
}
await browser.close();