Project Browsers 1.0
This commit is contained in:
33
lib/screenshotter.js
Normal file
33
lib/screenshotter.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const { execFile } = require('child_process');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
|
||||
const SCREENSHOTS_DIR = path.join(__dirname, '../public/screenshots');
|
||||
|
||||
function takeScreenshot(slug, port, useLocalhost = false) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const outPath = path.join(SCREENSHOTS_DIR, `${slug}.png`);
|
||||
const host = useLocalhost ? 'localhost' : '127.0.0.1';
|
||||
const url = `http://${host}:${port}`;
|
||||
|
||||
const args = [
|
||||
'screenshot',
|
||||
'--viewport-size', '1280,800',
|
||||
'--wait-for-timeout', '3000',
|
||||
url,
|
||||
outPath,
|
||||
];
|
||||
|
||||
execFile('/usr/bin/playwright', args, { timeout: 30000 }, (err, stdout, stderr) => {
|
||||
if (err) {
|
||||
reject(new Error(`Screenshot failed: ${err.message}\n${stderr}`));
|
||||
} else if (fs.existsSync(outPath)) {
|
||||
resolve(`/screenshots/${slug}.png`);
|
||||
} else {
|
||||
reject(new Error('Screenshot file not created'));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { takeScreenshot };
|
||||
Reference in New Issue
Block a user