Skip to content

Instantly share code, notes, and snippets.

@SimplGy
Created March 12, 2023 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimplGy/0e89bc0a60548b32cac9c0db806d9cd4 to your computer and use it in GitHub Desktop.
Save SimplGy/0e89bc0a60548b32cac9c0db806d9cd4 to your computer and use it in GitHub Desktop.
// 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment