Files
xbcr/scripts/verify_theme.js
T

86 lines
3.1 KiB
JavaScript

const { chromium } = require('playwright');
const fs = require('fs');
const path = require('path');
const edgePath = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe';
const outputDir = path.resolve(__dirname, '..', 'artifacts', 'screenshots');
const selectors = [
'.auth-btn',
'.locate-btn',
'.scan-btn',
'.bike-action-btn',
'.auth-submit',
'.profile-verify-btn',
'.radius-btn.active',
'.token-expired-btn.confirm',
'.code-send-btn',
'.return-confirm-btn.confirm',
'.unlock-confirm-btn.confirm',
'.lost-modal-btn.confirm',
'.share-req-row button',
'.share-mini-btn.primary',
];
function channel(value) {
const normalized = value / 255;
return normalized <= 0.04045
? normalized / 12.92
: ((normalized + 0.055) / 1.055) ** 2.4;
}
function luminance(rgb) {
const values = rgb.match(/[\d.]+/g).slice(0, 3).map(Number);
return 0.2126 * channel(values[0]) + 0.7152 * channel(values[1]) + 0.0722 * channel(values[2]);
}
function contrast(foreground, background) {
const light = Math.max(luminance(foreground), luminance(background));
const dark = Math.min(luminance(foreground), luminance(background));
return (light + 0.05) / (dark + 0.05);
}
(async () => {
const browser = await chromium.launch({ executablePath: edgePath, headless: true });
let failed = false;
for (const colorScheme of ['light', 'dark']) {
const context = await browser.newContext({ colorScheme, viewport: { width: 1280, height: 800 } });
const page = await context.newPage();
page.on('dialog', dialog => dialog.dismiss());
if (process.env.XBNIU_CAPTURE_TOKEN) {
await page.addInitScript(token => localStorage.setItem('auth_token', token), process.env.XBNIU_CAPTURE_TOKEN);
}
await page.goto('http://127.0.0.1:98/', { waitUntil: 'domcontentloaded' });
await page.waitForTimeout(500);
const results = await page.evaluate(items => items.map(selector => {
const element = document.querySelector(selector);
if (!element) return { selector, missing: true };
const style = getComputedStyle(element);
const themedBackground = getComputedStyle(document.body).getPropertyValue('--accent-color').trim();
const background = style.backgroundColor === 'rgba(0, 0, 0, 0)' && style.backgroundImage !== 'none'
? themedBackground
: style.backgroundColor;
return { selector, color: style.color, background };
}), selectors);
for (const result of results) {
if (result.missing) continue;
result.contrast = Number(contrast(result.color, result.background).toFixed(2));
if (result.contrast < 4.5) failed = true;
}
console.log(JSON.stringify({ colorScheme, results }, null, 2));
if (process.env.XBNIU_CAPTURE_TOKEN) {
fs.mkdirSync(outputDir, { recursive: true });
await page.addStyleTag({
content: '#token-expired-modal, .login-gate, .toast, #loading, #version-modal { display: none !important; }',
});
await page.screenshot({ path: path.join(outputDir, `map-authenticated-${colorScheme}.png`) });
}
await context.close();
}
await browser.close();
if (failed) process.exitCode = 1;
})();