diff options
| author | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2025-06-23 23:02:11 +0100 |
|---|---|---|
| committer | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2025-06-23 23:02:11 +0100 |
| commit | 6c63bb9b831a8dce2b69d5906a164a7812b25982 (patch) | |
| tree | ac9160b50be0deb7e299940aea2204cded4eb4ff /source/index.js | |
| parent | cfed7ff345511002d2154debeff5c2c5cc53a7d6 (diff) | |
| download | zue.dev-6c63bb9b831a8dce2b69d5906a164a7812b25982.tar zue.dev-6c63bb9b831a8dce2b69d5906a164a7812b25982.tar.gz zue.dev-6c63bb9b831a8dce2b69d5906a164a7812b25982.tar.bz2 zue.dev-6c63bb9b831a8dce2b69d5906a164a7812b25982.tar.xz zue.dev-6c63bb9b831a8dce2b69d5906a164a7812b25982.zip | |
add /browser-rendering/screenshot url
Diffstat (limited to 'source/index.js')
| -rw-r--r-- | source/index.js | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/source/index.js b/source/index.js index ad3472a..fb25ff9 100644 --- a/source/index.js +++ b/source/index.js @@ -1,5 +1,5 @@ +import puppeteer from "@cloudflare/puppeteer"; import Router from "./library/router/index.js"; - import talent96 from "./data/talent96.js"; export default { @@ -103,6 +103,41 @@ export default { }); }); + router.add("/browser-rendering/screenshot", async (request) => { + const { searchParams } = new URL(request.url); + const url = searchParams.get("url"); + const type = searchParams.get("type") || "webp"; + const fullPage = searchParams.get("fullPage") !== null; + const width = parseInt(searchParams.get("width"), 10) || 1920; + const height = parseInt(searchParams.get("height"), 10) || 1080; + + if (!url) + return router.respond({ + error: "URL parameter is required", + }); + + if (!/^https?:\/\//.test(url)) + return router.respond({ + error: "Invalid URL format", + }); + + const browser = await puppeteer.launch(environment.MYBROWSER); + const page = await browser.newPage(); + await page.setViewport({ width, height }); + await page.goto(url); + const screenshot = await page.screenshot({ + type, + fullPage, + }); + await browser.close(); + + return new Response(screenshot, { + headers: { + "content-type": `image/${type}`, + }, + }); + }); + return router.route(); }, |
