termai commited on
Commit
2c2fae6
·
verified ·
1 Parent(s): bb41030

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +59 -12
index.js CHANGED
@@ -1,33 +1,80 @@
1
  const express = require("express");
2
- const { chromium } = require("playwright");
 
 
3
 
4
  const app = express();
5
  app.use(express.json({ limit: "5mb" }));
6
 
7
- app.post("/eval", async (req, res) => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  const { code } = req.body;
9
  if (!code) return res.status(400).json({ error: "No code provided" });
10
 
11
  let browser;
12
- try {
13
- browser = await chromium.launch({ headless: true });
14
- const context = await browser.newContext();
15
- const page = await context.newPage();
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  const result = await eval(`
18
  (async () => {
19
  ${code}
20
  })()
21
  `);
22
-
23
- res.json({ success: true, result });
24
  } catch (err) {
25
- res.status(500).json({ error: err.message });
26
  } finally {
27
- if (browser) await browser.close();
 
 
28
  }
29
  });
30
 
31
  app.listen(3000, () => {
32
- console.log("Playwright API server running on port 3000");
33
- });
 
1
  const express = require("express");
2
+ const { chromium: origChromium } = require("playwright");
3
+ const axios = require("axios")
4
+ const Form = require("form-data")
5
 
6
  const app = express();
7
  app.use(express.json({ limit: "5mb" }));
8
 
9
+ const uploadFile = async (file) => {
10
+ const key = "AIzaBj7z2z3xBjsk"
11
+ let domain = 'https://c.termai.cc'
12
+ const formData = new Form();
13
+ formData.append('file', file, { filename: 'file.png'});
14
+
15
+ try {
16
+ const response = await axios.post(`${domain}/api/upload?uploader=drive&key=`+key, formData, {
17
+ headers: {
18
+ 'Content-Type': 'multipart/form-data',
19
+ },
20
+ });
21
+
22
+ return response.data
23
+ } catch (error) {
24
+ console.error('Error uploading file', error.response?.data || error.message);
25
+ }
26
+ };
27
+
28
+ app.post("/", async (req, res) => {
29
  const { code } = req.body;
30
  if (!code) return res.status(400).json({ error: "No code provided" });
31
 
32
  let browser;
33
+ const images = [];
 
 
 
34
 
35
+ const patchScreenshot = (target) => {
36
+ const orig = target.screenshot;
37
+ target.screenshot = async function (...args) {
38
+ const buffer = await orig.apply(this, args);
39
+ const url = await uploadFile(buffer);
40
+ delete url.status
41
+ images.push(url);
42
+ return buffer;
43
+ };
44
+ };
45
+
46
+ const chromium = {
47
+ ...origChromium,
48
+ launch: async (...args) => {
49
+ browser = await origChromium.launch(...args);
50
+ const origNewPage = browser.newPage;
51
+ browser.newPage = async (...a) => {
52
+ const page = await origNewPage.apply(browser, a);
53
+ patchScreenshot(page);
54
+ const origElemScreenshot = page.locator("").screenshot;
55
+ patchScreenshot(page);
56
+ return page;
57
+ };
58
+ return browser;
59
+ }
60
+ };
61
+
62
+ try {
63
  const result = await eval(`
64
  (async () => {
65
  ${code}
66
  })()
67
  `);
68
+ res.json({ success: true, result, images });
 
69
  } catch (err) {
70
+ res.status(500).json({ error: err.message, images });
71
  } finally {
72
+ if (browser) {
73
+ try { await browser.close(); } catch {}
74
+ }
75
  }
76
  });
77
 
78
  app.listen(3000, () => {
79
+ console.log("Playwright AI server running on port 3000");
80
+ });