From f36999721da9eb0263552230ad882e8339d321d6 Mon Sep 17 00:00:00 2001 From: Robert McGovern Date: Wed, 6 Mar 2024 12:53:09 +0000 Subject: [PATCH] made copy of page, don't ask --- generate-image-of-page.mjs | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 generate-image-of-page.mjs diff --git a/generate-image-of-page.mjs b/generate-image-of-page.mjs new file mode 100644 index 0000000..4961001 --- /dev/null +++ b/generate-image-of-page.mjs @@ -0,0 +1,52 @@ +import puppeteer from "puppeteer"; +import { scrollPageToBottom } from "puppeteer-autoscroll-down"; +import express from "express"; +import "dotenv/config"; + +const folderToServe = "/Users/tarasis/Programming/websites/rmcg.dev/www/"; + +const port = process.env.PORT || 4000; + +const app = express(); + +app.use(express.static(folderToServe)); +app.get("/screenshot", async (req, res) => { + const dynamicPage = express(); + dynamicPage.use(express.static(folderToServe)); + + const dynamicServer = dynamicPage.listen(0, async () => { + const dynamicPort = dynamicServer.address().port; + console.log( + `Dynamic server is running at http://localhost:${dynamicPort}` + ); + + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.goto(`http://localhost:${dynamicPort}`); + + await page.waitForResponse((response) => response.status() === 200); + + await scrollPageToBottom(page); + + const screenshotBuffer = await page.screenshot({ fullPage: true }); + + // Respond with the image + res.writeHead(200, { + "Content-Type": "image/png", + "Content-Length": screenshotBuffer.length, + }); + res.end(screenshotBuffer); + // other option is just to save the file to disk + // await page.screenshot({ path: 'example.png' }); + // Close the browser + + await browser.close(); + console.log(`Closing dynamic server http://localhost:${dynamicPort}`); + + await dynamicServer.close(); + }); +}); + +app.listen(port, () => { + console.log(`Server is running at http://localhost:${port}`); +});