Update index.js
Browse files
index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import express from 'express'
|
| 2 |
import morgan from 'morgan'
|
|
|
|
| 3 |
import * as prb from 'puppeteer-real-browser'
|
| 4 |
import serveIndex from 'serve-index'
|
| 5 |
|
|
@@ -38,30 +39,50 @@ app.get('/ss', async (req, res) => {
|
|
| 38 |
const {
|
| 39 |
delay = 0,
|
| 40 |
full = false,
|
|
|
|
| 41 |
transparent = false,
|
| 42 |
url = 'https://example.com'
|
| 43 |
} = req.query
|
| 44 |
if (String(url).includes('whatsapp.com'))
|
| 45 |
return res.redirect('/')
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
try {
|
| 55 |
-
const page =
|
|
|
|
|
|
|
| 56 |
await page.goto(
|
| 57 |
url,
|
| 58 |
{ waitUntil: 'networkidle0'}
|
| 59 |
)
|
|
|
|
| 60 |
const name = format(
|
| 61 |
'%s/%s.png',
|
| 62 |
tmpDir,
|
| 63 |
Math.random().toString(36).slice(2)
|
| 64 |
)
|
|
|
|
| 65 |
if (/^\d$/.test(delay) && delay > 0)
|
| 66 |
await setTimeout(+delay)
|
| 67 |
await page.screenshot({
|
|
@@ -69,6 +90,7 @@ app.get('/ss', async (req, res) => {
|
|
| 69 |
path: name,
|
| 70 |
omitBackground: transparent
|
| 71 |
})
|
|
|
|
| 72 |
res.redirect(name)
|
| 73 |
} catch (e) {
|
| 74 |
console.error(e)
|
|
@@ -79,6 +101,7 @@ app.get('/ss', async (req, res) => {
|
|
| 79 |
msg: format(e?.message || e)
|
| 80 |
})
|
| 81 |
} finally {
|
|
|
|
| 82 |
await ctx.browser.close()
|
| 83 |
}
|
| 84 |
})
|
|
|
|
| 1 |
import express from 'express'
|
| 2 |
import morgan from 'morgan'
|
| 3 |
+
import pg from 'puppeteer-ghost'
|
| 4 |
import * as prb from 'puppeteer-real-browser'
|
| 5 |
import serveIndex from 'serve-index'
|
| 6 |
|
|
|
|
| 39 |
const {
|
| 40 |
delay = 0,
|
| 41 |
full = false,
|
| 42 |
+
mode = 'ghost',
|
| 43 |
transparent = false,
|
| 44 |
url = 'https://example.com'
|
| 45 |
} = req.query
|
| 46 |
if (String(url).includes('whatsapp.com'))
|
| 47 |
return res.redirect('/')
|
| 48 |
+
|
| 49 |
+
const isGhost = /^ghost$/i.test(mode)
|
| 50 |
+
|
| 51 |
+
const label = format(
|
| 52 |
+
'[%s]', isGhost ?
|
| 53 |
+
'PuppeteerGhost' : 'PuppeteerRealBrowser'
|
| 54 |
+
)
|
| 55 |
+
console.time(label)
|
| 56 |
+
|
| 57 |
+
const ctx = isGhost ?
|
| 58 |
+
await pg.launch({
|
| 59 |
+
executablePath: env.CHROME_BIN,
|
| 60 |
+
headless: 'new'
|
| 61 |
+
}) :
|
| 62 |
+
await prb.connect({
|
| 63 |
+
customConfig: {
|
| 64 |
+
executablePath: env.CHROME_BIN
|
| 65 |
+
},
|
| 66 |
+
disableXvfb: true,
|
| 67 |
+
headless: 'new',
|
| 68 |
+
turnstile: true
|
| 69 |
+
})
|
| 70 |
+
|
| 71 |
try {
|
| 72 |
+
const page = isGhost ?
|
| 73 |
+
await ctx.newPage() :
|
| 74 |
+
ctx.page
|
| 75 |
await page.goto(
|
| 76 |
url,
|
| 77 |
{ waitUntil: 'networkidle0'}
|
| 78 |
)
|
| 79 |
+
|
| 80 |
const name = format(
|
| 81 |
'%s/%s.png',
|
| 82 |
tmpDir,
|
| 83 |
Math.random().toString(36).slice(2)
|
| 84 |
)
|
| 85 |
+
|
| 86 |
if (/^\d$/.test(delay) && delay > 0)
|
| 87 |
await setTimeout(+delay)
|
| 88 |
await page.screenshot({
|
|
|
|
| 90 |
path: name,
|
| 91 |
omitBackground: transparent
|
| 92 |
})
|
| 93 |
+
|
| 94 |
res.redirect(name)
|
| 95 |
} catch (e) {
|
| 96 |
console.error(e)
|
|
|
|
| 101 |
msg: format(e?.message || e)
|
| 102 |
})
|
| 103 |
} finally {
|
| 104 |
+
console.timeEnd(label)
|
| 105 |
await ctx.browser.close()
|
| 106 |
}
|
| 107 |
})
|