const express = require("express"); const { chromium: origChromium } = require("playwright"); const axios = require("axios") const Form = require("form-data") const app = express(); app.use(express.json({ limit: "5mb" })); const uploadFile = async (file) => { const key = "AIzaBj7z2z3xBjsk" let domain = 'https://c.termai.cc' const formData = new Form(); formData.append('file', file, { filename: 'file.png'}); try { const response = await axios.post(`${domain}/api/upload?uploader=drive&key=`+key, formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); return response.data } catch (error) { console.error('Error uploading file', error.response?.data || error.message); } }; app.post("/", async (req, res) => { const { code } = req.body; if (!code) return res.status(400).json({ error: "No code provided" }); let browser; const images = []; const patchScreenshot = (target) => { const orig = target.screenshot; target.screenshot = async function (...args) { const buffer = await orig.apply(this, args); const url = await uploadFile(buffer); delete url.status images.push(url); return buffer; }; }; const chromium = { ...origChromium, launch: async (...args) => { browser = await origChromium.launch(...args); const origNewPage = browser.newPage; browser.newPage = async (...a) => { const page = await origNewPage.apply(browser, a); patchScreenshot(page); const origElemScreenshot = page.locator("").screenshot; patchScreenshot(page); return page; }; return browser; } }; try { const result = await eval(` (async () => { ${code} })() `); res.json({ success: true, result, images }); } catch (err) { res.status(500).json({ error: err.message, images }); } finally { if (browser) { try { await browser.close(); } catch {} } } }); app.listen(3000, () => { console.log("Playwright AI server running on port 3000"); });