Created
March 12, 2023 01:24
-
-
Save SimplGy/0e89bc0a60548b32cac9c0db806d9cd4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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