diff options
| author | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2025-07-04 02:11:49 +0100 |
|---|---|---|
| committer | Alex Pooley (@zuedev) <zuedev@gmail.com> | 2025-07-04 02:11:49 +0100 |
| commit | 673a2805cd8f5a0ed365b20fe1624bc7979f1953 (patch) | |
| tree | 5df680cea653cd7ae0d9847f888a8780675994d1 /source | |
| parent | e628564688a4a9d850f48f149e27ee49914facf4 (diff) | |
| download | zue.dev-673a2805cd8f5a0ed365b20fe1624bc7979f1953.tar zue.dev-673a2805cd8f5a0ed365b20fe1624bc7979f1953.tar.gz zue.dev-673a2805cd8f5a0ed365b20fe1624bc7979f1953.tar.bz2 zue.dev-673a2805cd8f5a0ed365b20fe1624bc7979f1953.tar.xz zue.dev-673a2805cd8f5a0ed365b20fe1624bc7979f1953.zip | |
fix errant browser rendering api endpoint
Diffstat (limited to 'source')
| -rw-r--r-- | source/main.js | 107 |
1 files changed, 29 insertions, 78 deletions
diff --git a/source/main.js b/source/main.js index 7eb433e..46fef97 100644 --- a/source/main.js +++ b/source/main.js @@ -165,87 +165,38 @@ export default { })(); case "/browser-rendering/screenshot": return (async () => { - /* - Checks if a twitch channel is live by fetching the "live" preview image of the channel, - if the image is fetched successfully, then the channel is live, otherwise it's offline. - - @param {string} channel - the twitch channel name - @returns {boolean} true if the channel is live, false otherwise - */ - async function isTwitchChannelLive(channel) { - // construct preview image url with channel name - const livePreviewUrl = `https://static-cdn.jtvnw.net/previews-ttv/live_user_${channel}-320x180.jpg`; - - // fetch the preview image, don't follow redirects - const response = await fetch(livePreviewUrl, { - redirect: "manual", + 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", }); - // check if the image was fetched successfully - return response.ok; - } - - const url = new URL(request.url); - const channel = url.searchParams.get("channel"); - - if (!channel) - return new Response( - JSON.stringify({ - error: `channel not provided`, - }), - { - headers: { - "Access-Control-Allow-Origin": "*", - "Content-Type": "application/json", - }, - } - ); - - const whitelist = [ - "zuedev", - ...["vtsweets", "bunnibana", "yayjaybae", "justawoney", "tygiwygi"], - ]; - - if (!whitelist.includes(channel)) - return new Response( - JSON.stringify({ - error: `channel not whitelisted`, - }), - { - headers: { - "Access-Control-Allow-Origin": "*", - "Content-Type": "application/json", - }, - } - ); - - const channelLive = await isTwitchChannelLive(channel); - - if (channelLive) - return new Response( - JSON.stringify({ - status: "live", - channel, - }), - { - headers: { - "Access-Control-Allow-Origin": "*", - "Content-Type": "application/json", - }, - } - ); + if (!/^https?:\/\//.test(url)) + return router.respond({ + error: "Invalid URL format", + }); - return new Response( - JSON.stringify({ - status: "offline", - }), - { - headers: { - "Access-Control-Allow-Origin": "*", - "Content-Type": "application/json", - }, - } - ); + 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}`, + }, + }); })(); default: return new Response( |
