Spaces:
Paused
Paused
| import * as http from 'http'; | |
| import * as url from 'url'; | |
| import * as path from 'path'; | |
| import { chromium } from 'playwright'; | |
| import { locks } from 'web-locks'; | |
| // npm i playwright | |
| // npm i web-locks | |
| // nohup cloudflared tunnel --url http://localhost:8080 --no-autoupdate & (or setsid) | |
| // setsid node playwright.mjs (nohup doesnt work) | |
| globalThis.state = Object.assign(globalThis.state||{}, { | |
| browser: null, | |
| context: null, | |
| }); | |
| async function text_from_url(url, cookie) { | |
| let { browser, context } = globalThis.state; | |
| if (!browser) { | |
| await locks.request('playwright_browser', async lock => { | |
| browser = globalThis.state.browser = await chromium.launch(); | |
| context = globalThis.state.context = await browser.newContext({ | |
| javaScriptEnabled: false | |
| }/*devices['iPhone 11']*/); | |
| }); | |
| } | |
| const page = await context.newPage(); | |
| if (cookie) { | |
| if (cookie.endsWith(';')) cookie = cookie.slice(0, -1); | |
| let cookies = cookie.split(';'); | |
| cookies = cookies.map(it=>{ | |
| let [name, value] = it.split('='); | |
| return {name: name.trim(), value: value.trim(), url}; | |
| }); | |
| context.addCookies(cookies); | |
| } | |
| await context.route("**/*.{png,jpg,jpeg,css,js}", route => route.abort()); | |
| await page.goto(url); | |
| let text; | |
| let i = 0; | |
| while (true) { | |
| let new_text = await page.evaluate(() => document.body.innerText); | |
| if (i > 5 || new_text?.length > 200) { | |
| text = new_text; | |
| break; | |
| } | |
| i++; | |
| await new Promise(resolve=>setTimeout(resolve, 1000)); | |
| } | |
| await page.close(); | |
| //await context.close(); | |
| // await browser.close(); | |
| return text; | |
| } | |
| const server = http.createServer(async function(req, res) { | |
| const {query, pathname} = url.parse(req.url, true); | |
| res.setHeader('Access-Control-Allow-Origin', '*') | |
| let _url = query.url; | |
| let cookie = query.cookie; | |
| try { | |
| let text = await text_from_url(_url, cookie); | |
| res.end(text); | |
| } catch (e) { | |
| res.end(e.toString()); | |
| } | |
| }); | |
| server.listen(7860); | |