const express = require("express"); const { chromium: origChromium } = require("playwright"); const axios = require("axios") const Form = require("form-data") const http = require('http'); 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: formData.getHeaders(), maxBodyLength: Infinity, }); return response.data || {}; } catch (error) { console.error('Error uploading file', error.response?.data || error.message); return {}; } }; const patchScreenshot = (page, images) => { const orig = page.screenshot; page.screenshot = async function (options = {}) { const buffer = await orig.apply(this, [options]); const url = await uploadFile(buffer); console.log("[patchScreenshot] Upload result:", url); if (url && url.path) { images.push(url.path); } return buffer; }; }; app.all("/", async (req, res) => { const code = req.body?.code || req.query?.code; if (!code) return res.status(400).json({ error: "No code provided" }); let browser; let images = []; 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, images); 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 {} } } }); const PORT = process.env.PORT || 7860; const server = http.createServer({ maxHeaderSize: 64 * 1024 }, app); server.listen(PORT, () => { console.log(`Server running on port ${PORT}`); });