Spaces:
Sleeping
Sleeping
and
commited on
Commit
•
ae0a0ca
1
Parent(s):
a975599
index.mjs
CHANGED
@@ -3,12 +3,17 @@ 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,
|
@@ -60,6 +65,46 @@ async function text_from_url(url, cookie) {
|
|
60 |
return text;
|
61 |
}
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
const server = http.createServer(async function(req, res) {
|
65 |
const {query, pathname} = url.parse(req.url, true);
|
@@ -70,9 +115,20 @@ const server = http.createServer(async function(req, res) {
|
|
70 |
let cookie = query.cookie;
|
71 |
|
72 |
try {
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
} catch (e) {
|
77 |
res.end(e.toString());
|
78 |
}
|
|
|
3 |
import * as path from 'path';
|
4 |
import { chromium } from 'playwright';
|
5 |
import { locks } from 'web-locks';
|
6 |
+
import crypto from 'crypto';
|
7 |
+
import fs from 'fs';
|
8 |
|
9 |
// npm i playwright
|
10 |
// npm i web-locks
|
11 |
+
|
12 |
// nohup cloudflared tunnel --url http://localhost:8080 --no-autoupdate & (or setsid)
|
13 |
// setsid node playwright.mjs (nohup doesnt work)
|
14 |
|
15 |
+
// curl 'https://gowah44030-playwright.hf.space/..
|
16 |
+
|
17 |
globalThis.state = Object.assign(globalThis.state||{}, {
|
18 |
browser: null,
|
19 |
context: null,
|
|
|
65 |
return text;
|
66 |
}
|
67 |
|
68 |
+
async function screenshot(url, cookie) {
|
69 |
+
let { browser, context } = globalThis.state;
|
70 |
+
if (!browser) {
|
71 |
+
await locks.request('playwright_browser', async lock => {
|
72 |
+
browser = globalThis.state.browser = await chromium.launch();
|
73 |
+
context = globalThis.state.context = await browser.newContext({
|
74 |
+
javaScriptEnabled: false
|
75 |
+
}/*devices['iPhone 11']*/);
|
76 |
+
});
|
77 |
+
}
|
78 |
+
|
79 |
+
const page = await context.newPage();
|
80 |
+
|
81 |
+
if (cookie) {
|
82 |
+
if (cookie.endsWith(';')) cookie = cookie.slice(0, -1);
|
83 |
+
let cookies = cookie.split(';');
|
84 |
+
cookies = cookies.map(it=>{
|
85 |
+
let [name, value] = it.split('=');
|
86 |
+
return {name: name.trim(), value: value.trim(), url};
|
87 |
+
});
|
88 |
+
context.addCookies(cookies);
|
89 |
+
}
|
90 |
+
|
91 |
+
//await context.route("**/*.{png,jpg,jpeg,css,js}", route => route.abort());
|
92 |
+
await page.goto(url);
|
93 |
+
|
94 |
+
await setTimeout(2000);
|
95 |
+
|
96 |
+
let id = crypto.randomUUID();
|
97 |
+
let path = `${id}.png`;
|
98 |
+
|
99 |
+
await page.screenshot({ path, fullPage: true });
|
100 |
+
|
101 |
+
await page.close();
|
102 |
+
//await context.close();
|
103 |
+
// await browser.close();
|
104 |
+
|
105 |
+
return path;
|
106 |
+
}
|
107 |
+
|
108 |
|
109 |
const server = http.createServer(async function(req, res) {
|
110 |
const {query, pathname} = url.parse(req.url, true);
|
|
|
115 |
let cookie = query.cookie;
|
116 |
|
117 |
try {
|
118 |
+
if (pathname == '/text') {
|
119 |
+
let text = await text_from_url(_url, cookie);
|
120 |
+
|
121 |
+
res.end(text);
|
122 |
+
} else if (pathname == '/screenshot') {
|
123 |
+
let path = await screenshot(_url, cookie);
|
124 |
+
|
125 |
+
const readStream = fs.createReadStream(path);
|
126 |
+
res.writeHead(200,{'Content-type':'image/png'});
|
127 |
+
readStream.pipe(res);
|
128 |
+
res.end();
|
129 |
+
} else {
|
130 |
+
res.end('hello');
|
131 |
+
}
|
132 |
} catch (e) {
|
133 |
res.end(e.toString());
|
134 |
}
|