87 lines
3.1 KiB
JavaScript
87 lines
3.1 KiB
JavaScript
import { mkdir } from 'node:fs/promises';
|
|
import { chromium } from '@playwright/test';
|
|
|
|
const baseURL = process.env.CHRNX_REVIEW_URL || 'http://localhost:8888';
|
|
const host = new URL(baseURL).hostname;
|
|
if (!['localhost', '127.0.0.1'].includes(host)) {
|
|
throw new Error('Visual review capture is restricted to local WordPress.');
|
|
}
|
|
|
|
const output = new URL('../tmp/imagegen/', import.meta.url);
|
|
await mkdir(output, { recursive: true });
|
|
|
|
async function capture(target, variant) {
|
|
const { path, selector } = target;
|
|
const { theme, viewport } = variant;
|
|
const browser = await chromium.launch();
|
|
try {
|
|
const context = await browser.newContext({
|
|
viewport,
|
|
colorScheme: theme,
|
|
reducedMotion: 'reduce',
|
|
});
|
|
await context.addInitScript(
|
|
(preference) => localStorage.setItem('chrnx-theme', preference),
|
|
theme,
|
|
);
|
|
const page = await context.newPage();
|
|
const response = await page.goto(new URL(path, baseURL).href, { waitUntil: 'networkidle' });
|
|
if (!response?.ok()) {
|
|
throw new Error(`Capture target ${path} returned HTTP ${response?.status() ?? 'unknown'}.`);
|
|
}
|
|
await page.addStyleTag({
|
|
content: '*,*::before,*::after{animation:none!important;transition:none!important}',
|
|
});
|
|
await page.evaluate(async () => {
|
|
await document.fonts.ready;
|
|
for (let y = 0; y <= document.documentElement.scrollHeight; y += window.innerHeight) {
|
|
window.scrollTo(0, y);
|
|
await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)));
|
|
}
|
|
await Promise.race([
|
|
Promise.all(
|
|
[...document.images].map((image) =>
|
|
image.complete ? Promise.resolve() : image.decode().catch(() => {}),
|
|
),
|
|
),
|
|
new Promise((resolve) => setTimeout(resolve, 3000)),
|
|
]);
|
|
window.scrollTo(0, 0);
|
|
});
|
|
const outputName = `editorial-polish-${target.name}-${variant.name}.png`;
|
|
const outputPath = new URL(outputName, output).pathname;
|
|
if (selector) {
|
|
await page.locator(selector).screenshot({ path: outputPath });
|
|
} else {
|
|
await page.screenshot({ path: outputPath, fullPage: true });
|
|
}
|
|
console.log(
|
|
`${outputName} ${await page.evaluate(
|
|
() => `${document.documentElement.scrollWidth}x${document.documentElement.scrollHeight}`,
|
|
)}`,
|
|
);
|
|
await context.close();
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
}
|
|
|
|
const reviewTargets = [
|
|
{ name: 'home', path: '/' },
|
|
{ name: 'music', path: '/music/' },
|
|
{ name: 'post', path: '/una-cuerda-rota-tambien-puede-decidir-el-arreglo/' },
|
|
{ name: 'footer', path: '/', selector: 'footer.wp-block-template-part' },
|
|
];
|
|
const reviewVariants = [
|
|
{ name: 'desktop-dark', theme: 'dark', viewport: { width: 1440, height: 900 } },
|
|
{ name: 'desktop-light', theme: 'light', viewport: { width: 1440, height: 900 } },
|
|
{ name: 'mobile-dark', theme: 'dark', viewport: { width: 375, height: 812 } },
|
|
{ name: 'mobile-light', theme: 'light', viewport: { width: 375, height: 812 } },
|
|
];
|
|
|
|
for (const target of reviewTargets) {
|
|
for (const variant of reviewVariants) {
|
|
await capture(target, variant);
|
|
}
|
|
}
|