42 lines
2.0 KiB
JavaScript
42 lines
2.0 KiB
JavaScript
// Screenshot helper: takes each .slide at 1920x1080 into an output folder.
|
|
// Usage: node shot.cjs <html-path> <out-dir>
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const { chromium } = require('/home/marcel/.npm/_npx/e41f203b7505f1fb/node_modules/playwright');
|
|
|
|
(async () => {
|
|
const htmlPath = path.resolve(process.argv[2]);
|
|
const outDir = path.resolve(process.argv[3] || 'shots');
|
|
fs.mkdirSync(outDir, { recursive: true });
|
|
const browser = await chromium.launch({ args: ['--no-sandbox'] });
|
|
const page = await browser.newPage({ viewport: { width: 1920, height: 1080 } });
|
|
const errors = [];
|
|
page.on('console', m => { if (m.type() === 'error') errors.push('console: ' + m.text()); });
|
|
page.on('pageerror', e => errors.push('pageerror: ' + e.message));
|
|
page.on('requestfailed', r => errors.push('requestfailed: ' + r.url() + ' ' + (r.failure() && r.failure().errorText)));
|
|
await page.goto('file://' + htmlPath, { waitUntil: 'load' });
|
|
await page.evaluate(async () => { if (document.fonts && document.fonts.ready) await document.fonts.ready; });
|
|
await page.waitForTimeout(1200);
|
|
await page.mouse.move(960, 540);
|
|
const n = await page.$$eval('.slide', els => els.length);
|
|
const broken = await page.evaluate(() => Array.from(document.images)
|
|
.filter(i => !i.complete || i.naturalWidth === 0)
|
|
.map(i => i.getAttribute('src')));
|
|
for (let i = 0; i < n; i++) {
|
|
await page.evaluate(k => {
|
|
const slides = document.querySelectorAll('.slide');
|
|
slides.forEach((s, j) => {
|
|
s.classList.toggle('active', j === k);
|
|
s.classList.toggle('visible', j === k);
|
|
});
|
|
}, i);
|
|
await page.waitForTimeout(900);
|
|
const file = path.join(outDir, String(i + 1).padStart(2, '0') + '.png');
|
|
await page.screenshot({ path: file, clip: { x: 0, y: 0, width: 1920, height: 1080 } });
|
|
}
|
|
console.log('slides:', n);
|
|
console.log('broken images:', JSON.stringify(broken, null, 2));
|
|
console.log('errors:', JSON.stringify(errors, null, 2));
|
|
await browser.close();
|
|
})();
|