Update index.js
Browse files12343 unmodified lines891011121361 unmodified lines757677787980818283848586878889903 unmodified lines94959697989910010110210310410523 unmodified lines12913013113213313413513613713813914014114214314414514611 unmodified lines158159160161162163164const express = require('express');const { chromium } = require('playwright');const QRCode = require('qrcode');const Jimp = require('jimp');3 unmodified linesconst app = express();const port = 7860;app.use(express.json());app.get('/', (req, res) => {61 unmodified linesasync function processScreenshot(req, res, { ua, url, type, width, height, language, fullpage }) {try {console.log('process screenshot');const browser = await chromium.launch({headless: true,args: ["--no-sandbox", "--disable-setuid-sandbox"]});const context = await browser.newContext({userAgent: ua || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',viewport: { width: width || 1280, height: height || 800 },deviceScaleFactor: 2});const page = await context.newPage();if (language) {await page.emulateMedia({ colorScheme: language });3 unmodified lines// Handle JavaScript challengesawait page.waitForLoadState('networkidle');// Example to handle JavaScript challenge// This is a placeholder, replace with actual challenge handling if needed// await page.evaluate(() => {// // Solve any inline JavaScript challenges// });let screenshotOptions = {fullPage: fullpage == 1 ? true : false,23 unmodified lines});} else if (type === 'custom') {if (width && height > 4096) {return res.json({"status": 400,"message": "Width and height values should not exceed 4096 pixels."});await browser.close();}if (width && height < 400) {return res.json({"status": 400,"message": "Width and height values should not be less than 400 pixels."});await browser.close();}await page.setViewportSize({width: Number(width) || 375,11 unmodified lines});res.end(screenshot);console.log('process done');await browser.close();} catch (e) {console.error(e);return res.json({12343 unmodified lines89101112131415161761 unmodified lines7980818283848586878889909192939495969798991001011021033 unmodified lines10710810911011111223 unmodified lines13613713813914014114214314414514614714814915015115215311 unmodified lines165166167168169170171172173174175176177178179180181182const express = require('express');const { chromium } = require('playwright');const QRCode = require('qrcode');const Jimp = require('jimp');3 unmodified linesconst app = express();const port = 7860;// Global variable to hold the browser instance and contextlet browser;let browserContext;app.use(express.json());app.get('/', (req, res) => {61 unmodified linesasync function processScreenshot(req, res, { ua, url, type, width, height, language, fullpage }) {try {console.log('process screenshot');// Launch browser and create a context if it doesn't existif (!browser) {browser = await chromium.launch({headless: true,args: ["--no-sandbox", "--disable-setuid-sandbox"]});}// Reuse or create a new contextif (!browserContext) {browserContext = await browser.newContext({userAgent: ua || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',viewport: { width: width || 1280, height: height || 800 },deviceScaleFactor: 2});}const page = await browserContext.newPage();if (language) {await page.emulateMedia({ colorScheme: language });3 unmodified lines// Handle JavaScript challengesawait page.waitForLoadState('networkidle');let screenshotOptions = {fullPage: fullpage == 1 ? true : false,23 unmodified lines});} else if (type === 'custom') {if (width && height > 4096) {await page.close();return res.json({"status": 400,"message": "Width and height values should not exceed 4096 pixels."});}if (width && height < 400) {await page.close();return res.json({"status": 400,"message": "Width and height values should not be less than 400 pixels."});}await page.setViewportSize({width: Number(width) || 375,11 unmodified lines});res.end(screenshot);console.log('process done');await page.close(); // Close the tab instead of the browser// Periodically close and reopen the browser to avoid memory bloatif (browserContext.pages().length > 10) { // Arbitrary thresholdawait browserContext.close();browserContext = await browser.newContext();}// Trigger garbage collectionif (global.gc) {global.gc();}} catch (e) {console.error(e);return res.json({
index.js+38-20