Spaces:
Running
Running
and
commited on
Commit
·
98e9a75
1
Parent(s):
70bd71b
- Dockerfile +11 -0
- index.mjs +81 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM mcr.microsoft.com/playwright:v1.40.0-jammy
|
2 |
+
|
3 |
+
#WORKDIR /code
|
4 |
+
|
5 |
+
#COPY package.json ./package.json
|
6 |
+
|
7 |
+
RUN npm i [email protected]
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD [ "node", "index.mjs" ]
|
index.mjs
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import * as http from 'http';
|
2 |
+
import * as url from 'url';
|
3 |
+
import * as path from 'path';
|
4 |
+
import { chromium } from 'playwright';
|
5 |
+
import { locks } from 'web-locks';
|
6 |
+
|
7 |
+
// npm i playwright
|
8 |
+
// npm i web-locks
|
9 |
+
// nohup cloudflared tunnel --url http://localhost:8080 --no-autoupdate & (or setsid)
|
10 |
+
// setsid node playwright.mjs (nohup doesnt work)
|
11 |
+
|
12 |
+
globalThis.state = Object.assign(globalThis.state||{}, {
|
13 |
+
browser: null,
|
14 |
+
context: null,
|
15 |
+
});
|
16 |
+
|
17 |
+
|
18 |
+
async function text_from_url(url, cookie) {
|
19 |
+
let { browser, context } = globalThis.state;
|
20 |
+
if (!browser) {
|
21 |
+
await locks.request('playwright_browser', async lock => {
|
22 |
+
browser = globalThis.state.browser = await chromium.launch();
|
23 |
+
context = globalThis.state.context = await browser.newContext({
|
24 |
+
javaScriptEnabled: false
|
25 |
+
}/*devices['iPhone 11']*/);
|
26 |
+
});
|
27 |
+
}
|
28 |
+
|
29 |
+
const page = await context.newPage();
|
30 |
+
|
31 |
+
if (cookie) {
|
32 |
+
if (cookie.endsWith(';')) cookie = cookie.slice(0, -1);
|
33 |
+
let cookies = cookie.split(';');
|
34 |
+
cookies = cookies.map(it=>{
|
35 |
+
let [name, value] = it.split('=');
|
36 |
+
return {name: name.trim(), value: value.trim(), url};
|
37 |
+
});
|
38 |
+
context.addCookies(cookies);
|
39 |
+
}
|
40 |
+
|
41 |
+
await context.route("**/*.{png,jpg,jpeg,css,js}", route => route.abort());
|
42 |
+
await page.goto(url);
|
43 |
+
|
44 |
+
let text;
|
45 |
+
let i = 0;
|
46 |
+
while (true) {
|
47 |
+
let new_text = await page.evaluate(() => document.body.innerText);
|
48 |
+
if (i > 5 || new_text?.length > 200) {
|
49 |
+
text = new_text;
|
50 |
+
break;
|
51 |
+
}
|
52 |
+
i++;
|
53 |
+
await new Promise(resolve=>setTimeout(resolve, 1000));
|
54 |
+
}
|
55 |
+
|
56 |
+
await page.close();
|
57 |
+
//await context.close();
|
58 |
+
// await browser.close();
|
59 |
+
|
60 |
+
return text;
|
61 |
+
}
|
62 |
+
|
63 |
+
|
64 |
+
const server = http.createServer(async function(req, res) {
|
65 |
+
const {query, pathname} = url.parse(req.url, true);
|
66 |
+
|
67 |
+
res.setHeader('Access-Control-Allow-Origin', '*')
|
68 |
+
|
69 |
+
let _url = query.url;
|
70 |
+
let cookie = query.cookie;
|
71 |
+
|
72 |
+
try {
|
73 |
+
let text = await text_from_url(_url, cookie);
|
74 |
+
|
75 |
+
res.end(text);
|
76 |
+
} catch (e) {
|
77 |
+
res.end(e.toString());
|
78 |
+
}
|
79 |
+
});
|
80 |
+
|
81 |
+
server.listen(7860);
|