const express = require('express'); const puppeteer = require('puppeteer-extra'); const StealthPlugin = require('puppeteer-extra-plugin-stealth'); const AnonymizeUA = require('puppeteer-extra-plugin-anonymize-ua'); puppeteer.use(StealthPlugin()); puppeteer.use(AnonymizeUA()); const app = express(); const port = 7860; app.use(express.json()); app.get('/', async (req, res) => { console.log('request to /') console.log('IP => ' + req.headers['x-real-ip']) console.log('User Agent => ' + req.headers['user-agent']) res.send('halo kontol') }) app.post('/screenshot', async (req, res) => { console.log('request to /screenshot') console.log('IP => ' + req.headers['x-real-ip']) console.log('User Agent => ' + req.headers['user-agent']) const { ua, url, type, width, height, language, fullpage } = req.body; if (!url) return res.send('please input url') await processScreenshot(req, res, { ua, url, type, width, height, language, fullpage }); }); app.get('/screenshot', async (req, res) => { console.log('request to /screenshot (get)') console.log('IP => ' + req.headers['x-real-ip']) console.log('User Agent => ' + req.headers['user-agent']) const { ua, url, type, width, height, language, fullpage } = req.query; if (!url) return res.send('please input url') await processScreenshot(req, res, { ua, url, type, width, height, language, fullpage }); }); async function processScreenshot(req, res, { ua, url, type, width, height, language, fullpage }) { console.log('process screenshot') const browser = await puppeteer.launch({ headless: "new", args: ["--aggressive-tab-discard", "--disable-accelerated-2d-canvas", "--disable-application-cache", "--disable-cache", "--disable-dev-shm-usage", "--disable-gpu", "--disable-offline-load-stale-cache", "--disable-setuid-sandbox", "--disable-setuid-sandbox", "--disk-cache-size=0", "--ignore-certificate-errors", "--no-first-run", "--no-sandbox", "--no-zygote" ] }); const page = await browser.newPage(); // Set language and user agent await page.setExtraHTTPHeaders({ 'Accept-Language': language || 'id-ID', 'User-Agent': ua || 'Mozilla/5.0 (Linux; Android 13; Pixel Build/RPB1.210523.001) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36', }); try { await page.goto(url, { waitUntil: 'networkidle2', }); let full = fullpage == 1 ? true : false let screenshotOptions = { fullPage: true }; if (type === 'desktop') { screenshotOptions = { fullPage: full, width: 1280, height: 800 }; await page.setViewport({ width: 1280, height: 800, }); } else if (type === 'mobile') { screenshotOptions = { fullPage: full, width: 375, height: 667 }; await page.setViewport({ width: 375, height: 667, }); } else if (type === 'tablet') { screenshotOptions = { fullPage: full, width: 768, height: 1024 }; await page.setViewport({ width: 768, height: 1024, }); } else if (type === 'iphone') { screenshotOptions = { fullPage: full, width: 375, height: 667 }; await page.setViewport({ width: 375, height: 667, }); } else if (type === 'custom') { if (width && height > 4096) { await browser.close(); return res.json({ "status": 400, "message": "Width and height values should not exceed 4096 pixels." }); } if (width && height < 90) { await browser.close(); return res.json({ "status": 400, "message": "Width and height values should not be less than 90 pixels." }); } screenshotOptions = { fullPage: full, width: width || 375, height: height || 667 }; await page.setViewport({ width: Number(width) || 375, height: Number(height) || 667, }); } // Wait for a few seconds to mimic more natural behavior await page.waitForTimeout(3000); const screenshot = await page.screenshot(screenshotOptions); res.writeHead(200, { 'Content-Type': 'image/png', 'Content-Length': screenshot.length, }); res.end(screenshot); await browser.close(); } catch (e) { console.log(e) await browser.close(); return res.json({ status: 400, message: 'error when take a screenshot' }) } } app.listen(port, () => { console.log(`Server is running on port ${port}`); });