File size: 5,495 Bytes
f3a829e a5afca9 a4fe837 dbb8a67 6b60173 d25296b 6b60173 f3a829e 6b60173 a5afca9 6b60173 a5afca9 6b60173 a5afca9 6b60173 a5afca9 a4fe837 a5afca9 a4fe837 a5afca9 a4fe837 4d47368 a5afca9 4d47368 a4fe837 a5afca9 a4fe837 a5afca9 a4fe837 a5afca9 a4fe837 a5afca9 a4fe837 a5afca9 a4fe837 6b60173 a5afca9 a4fe837 a5afca9 f3a829e da66ac8 016c2c7 f3a829e a5afca9 016c2c7 a5afca9 a4fe837 6b60173 a5afca9 6b60173 a5afca9 6b60173 a5afca9 6b60173 a5afca9 6b60173 a5afca9 6b60173 a5afca9 6b60173 f3a829e 6b60173 a4fe837 f3a829e 6b60173 a4fe837 6b60173 a5afca9 6b60173 a5afca9 6b60173 44ed890 f3a829e 6b60173 a5afca9 6b60173 a5afca9 6b60173 a5afca9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
const express = require('express');
const { chromium } = require('playwright');
const QRCode = require('qrcode');
const Jimp = require('jimp');
const QrCodeReader = require('qrcode-reader');
//const fetch = require('node-fetch');
const app = express();
const port = 7860;
// Global variable to hold the browser instance and context
let browser;
let browserContext;
app.use(express.json());
app.get('/', (req, res) => {
console.log('request to /');
res.send('halo kontol');
});
app.post('/screenshot', async (req, res) => {
console.log('request to /screenshot');
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)');
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 });
});
app.get('/qrcode', async (req, res) => {
try {
const { mode, url } = req.query;
if (!mode || !url) {
return res.status(400).send('what?');
}
if (mode === 'create') {
QRCode.toDataURL(url, (err, src) => {
if (err) return res.send({ error: 'Error generating QR code' });
res.type('png').send(Buffer.from(src.split(",")[1], "base64"));
});
} else if (['read', 'scan'].includes(mode)) {
const rts = await fetch(url);
if (!rts.ok || rts.status !== 200) return res.send({ status: 400, message: 'Can\'t buffer url' });
if (!/image/.test(rts.headers.get('content-type'))) return res.send({ status: 400, message: 'Only image type' });
const buffer = Buffer.from(await rts.arrayBuffer());
const image = await Jimp.read(buffer);
image.grayscale().contrast(1).normalize();
const qrCodeReader = new QrCodeReader();
qrCodeReader.callback = (err, value) => {
if (err) return res.send({ error: 'Error reading QR code' });
res.json({ result: value.result });
};
const bitmap = image.bitmap;
qrCodeReader.decode({ width: bitmap.width, height: bitmap.height, data: bitmap.data });
} else {
QRCode.toDataURL(url, (err, src) => {
if (err) return res.send({ error: 'Error generating QR code' });
res.type('png').send(Buffer.from(src.split(",")[1], "base64"));
});
}
} catch (error) {
console.error(error);
res.send({ error: 'Internal Server Error' });
}
});
async 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 exist
if (!browser) {
browser = await chromium.launch({
headless: true,
args: ["--no-sandbox", "--disable-setuid-sandbox"]
});
}
// Reuse or create a new context
if (!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: Number(width) || 1280, height: Number(height) || 800 },
deviceScaleFactor: 2,
locale: language || 'id-ID',
extraHTTPHeaders: {
'Accept-Language': language || 'id-ID',
}
});
}
const page = await browserContext.newPage();
if (language) {
await page.emulateMedia({ colorScheme: "dark" });
}
await page.goto(url, { waitUntil: 'networkidle' });
// Handle JavaScript challenges
await page.waitForLoadState('networkidle');
let screenshotOptions = {
fullPage: fullpage == 1 ? true : false,
type: 'png',
omitBackground: false
};
if (type === 'desktop') {
await page.setViewportSize({
width: 1920,
height: 1080,
});
} else if (type === 'mobile') {
await page.setViewportSize({
width: 375,
height: 667,
});
} else if (type === 'tablet') {
await page.setViewportSize({
width: 768,
height: 1024,
});
} else if (type === 'iphone') {
await page.setViewportSize({
width: 375,
height: 667,
});
} 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,
height: Number(height) || 667,
});
}
await page.waitForTimeout(5000);
const screenshot = await page.screenshot(screenshotOptions);
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': screenshot.length,
});
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 bloat
if (browserContext.pages().length > 10) { // Arbitrary threshold
await browserContext.close();
browserContext = await browser.newContext();
}
// Trigger garbage collection
if (global.gc) {
global.gc();
}
} catch (e) {
console.error(e);
return res.json({
status: 400,
message: 'error when take a screenshot'
});
}
}
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
|