Spaces:
Sleeping
Sleeping
and
commited on
Commit
•
7bc4d92
1
Parent(s):
13fe225
index.mjs
CHANGED
@@ -107,6 +107,30 @@ async function screenshot(url, cookie, js_enabled) {
|
|
107 |
return buffer;
|
108 |
}
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
const server = http.createServer(async function(req, res) {
|
112 |
const {query, pathname} = url.parse(req.url, true);
|
@@ -132,6 +156,21 @@ const server = http.createServer(async function(req, res) {
|
|
132 |
// res.writeHead(200,{'Content-type':'image/png'});
|
133 |
// readStream.pipe(res);
|
134 |
// res.end();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
} else {
|
136 |
res.end('hello');
|
137 |
}
|
|
|
107 |
return buffer;
|
108 |
}
|
109 |
|
110 |
+
async function evaluate(url, code) {
|
111 |
+
let { browser, context } = globalThis.state;
|
112 |
+
if (!browser) {
|
113 |
+
await locks.request('playwright_browser', async lock => {
|
114 |
+
browser = globalThis.state.browser = await chromium.launch();
|
115 |
+
context = globalThis.state.context = await browser.newContext({
|
116 |
+
javaScriptEnabled: true
|
117 |
+
}/*devices['iPhone 11']*/);
|
118 |
+
});
|
119 |
+
}
|
120 |
+
|
121 |
+
const page = await context.newPage();
|
122 |
+
|
123 |
+
page.on('console', async (msg) => {console.log(msg);});
|
124 |
+
|
125 |
+
//await context.route("**/*.{png,jpg,jpeg,css,js}", route => route.abort());
|
126 |
+
await page.goto(url);
|
127 |
+
|
128 |
+
let result = await page.evaluate(code);
|
129 |
+
|
130 |
+
await page.close();
|
131 |
+
|
132 |
+
return result;
|
133 |
+
}
|
134 |
|
135 |
const server = http.createServer(async function(req, res) {
|
136 |
const {query, pathname} = url.parse(req.url, true);
|
|
|
156 |
// res.writeHead(200,{'Content-type':'image/png'});
|
157 |
// readStream.pipe(res);
|
158 |
// res.end();
|
159 |
+
} else if (pathname == '/evaluate') {
|
160 |
+
const buffers = [];
|
161 |
+
for await (const chunk of req) {
|
162 |
+
buffers.push(chunk);
|
163 |
+
}
|
164 |
+
const data = Buffer.concat(buffers).toString();
|
165 |
+
|
166 |
+
const {url, code} = JSON.parse(data);
|
167 |
+
|
168 |
+
try {
|
169 |
+
let result = await evaluate(url, code);
|
170 |
+
res.end(''+result);
|
171 |
+
} catch (e) {
|
172 |
+
res.end(e.toString());
|
173 |
+
}
|
174 |
} else {
|
175 |
res.end('hello');
|
176 |
}
|