Spaces:
Running
Running
File size: 2,436 Bytes
bb41030 2c2fae6 07aa35b bb41030 2c2fae6 07aa35b 2c2fae6 07aa35b 2c2fae6 07aa35b aaa04de 2c2fae6 07aa35b aaa04de 2c2fae6 fd1f461 aaa04de 07aa35b aaa04de 07aa35b aaa04de 4358f3a bb41030 fd1f461 bb41030 2c2fae6 07aa35b 2c2fae6 07aa35b fd1f461 2c2fae6 07aa35b 2c2fae6 07aa35b 2c2fae6 bb41030 2c2fae6 bb41030 2c2fae6 07aa35b 2c2fae6 bb41030 0bbe894 07aa35b 950475f 07aa35b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
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,
}
);
console.log("[uploadFile] Upload result:", response.data);
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 = {}) {
console.log("[patchScreenshot] Screenshot called");
const buffer = await orig.apply(this, [options]);
const url = await uploadFile(buffer);
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.bind(browser);
browser.newPage = async (...a) => {
const page = await origNewPage(...a);
patchScreenshot(page, images);
return page;
};
return browser;
},
};
try {
const func = new Function(
"chromium",
`return (async () => { ${code} })()`
);
const result = await func(chromium);
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}`);
});
|