Create index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from 'express'
|
| 2 |
+
import morgan from 'morgan'
|
| 3 |
+
import puppeteer from 'puppeteer-ghost'
|
| 4 |
+
import serveIndex from 'serve-index'
|
| 5 |
+
|
| 6 |
+
import { tmpdir } from 'node:os'
|
| 7 |
+
import { env } from 'node:process'
|
| 8 |
+
import { setTimeout } from 'node:timers/promises'
|
| 9 |
+
import { format } from 'node:util'
|
| 10 |
+
|
| 11 |
+
const app = express()
|
| 12 |
+
|
| 13 |
+
const limitSize = '200mb'
|
| 14 |
+
app.set('json spaces', 2)
|
| 15 |
+
app.use(express.json({ limit: limitSize }))
|
| 16 |
+
app.use(express.urlencoded({
|
| 17 |
+
extended: true,
|
| 18 |
+
limit: limitSize
|
| 19 |
+
}))
|
| 20 |
+
app.use(morgan('dev'))
|
| 21 |
+
|
| 22 |
+
const tmpDir = tmpdir()
|
| 23 |
+
app.use(
|
| 24 |
+
tmpDir,
|
| 25 |
+
express.static(tmpDir),
|
| 26 |
+
serveIndex(
|
| 27 |
+
tmpDir,
|
| 28 |
+
{ hidden: true, icons: true }
|
| 29 |
+
)
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
app.all(
|
| 33 |
+
'/',
|
| 34 |
+
(_, res) => res.json({ msg: 'Hello World' })
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
app.get('/ss', async (req, res) => {
|
| 38 |
+
const {
|
| 39 |
+
delay = 0,
|
| 40 |
+
full = false,
|
| 41 |
+
url = 'https://example.com'
|
| 42 |
+
} = req.query
|
| 43 |
+
if (String(url).includes('whatsapp.com'))
|
| 44 |
+
return res.redirect('/')
|
| 45 |
+
const browser = await puppeteer.launch({
|
| 46 |
+
executablePath: env.CHROME_BIN,
|
| 47 |
+
headless: 'new'
|
| 48 |
+
})
|
| 49 |
+
try {
|
| 50 |
+
const page = await browser.newPage()
|
| 51 |
+
await page.goto(
|
| 52 |
+
url,
|
| 53 |
+
{ waitUntil: 'networkidle0'}
|
| 54 |
+
)
|
| 55 |
+
const name = format(
|
| 56 |
+
'%s/%s.png',
|
| 57 |
+
tmpDir,
|
| 58 |
+
Math.random().toString(36).slice(2)
|
| 59 |
+
)
|
| 60 |
+
if (/^\d$/.test(delay) && delay > 0)
|
| 61 |
+
await setTimeout(+delay)
|
| 62 |
+
await page.screenshot({
|
| 63 |
+
fullPage: full,
|
| 64 |
+
path: name,
|
| 65 |
+
omitBackground: true
|
| 66 |
+
})
|
| 67 |
+
res.redirect(name)
|
| 68 |
+
} catch (e) {
|
| 69 |
+
console.error(e)
|
| 70 |
+
res
|
| 71 |
+
.status(500)
|
| 72 |
+
.json({
|
| 73 |
+
err: true,
|
| 74 |
+
msg: format(e?.message || e)
|
| 75 |
+
})
|
| 76 |
+
} finally {
|
| 77 |
+
await browser.close()
|
| 78 |
+
}
|
| 79 |
+
})
|
| 80 |
+
|
| 81 |
+
const PORT = env.SERVER_PORT || env.PORT || 7860
|
| 82 |
+
app.listen(
|
| 83 |
+
PORT,
|
| 84 |
+
() => console.log('App running on port', PORT)
|
| 85 |
+
)
|