Files
nexus/scripts/ui-pruefung/kontrast.mjs

65 lines
4.8 KiB
JavaScript

import { start } from './lib.mjs';
const BASE = process.env.NEXUS_BASE || 'http://127.0.0.1:18112';
const pw = process.argv[2]; const tag = process.argv[3] || 'vorher';
const b = await start();
const mess = `(() => {
const parse = c => { const m = c.match(/rgba?\\(([^)]+)\\)/); if (!m) return null; const p = m[1].split(/[ ,\\/]+/).filter(Boolean).map(Number); return { r: p[0], g: p[1], b: p[2], a: p.length > 3 ? p[3] : 1 }; };
const lin = v => { v /= 255; return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); };
const lum = c => 0.2126 * lin(c.r) + 0.7152 * lin(c.g) + 0.0722 * lin(c.b);
const mix = (top, bot) => ({ r: top.r * top.a + bot.r * (1 - top.a), g: top.g * top.a + bot.g * (1 - top.a), b: top.b * top.a + bot.b * (1 - top.a), a: 1 });
const ratio = (f, bg) => { const F = mix(f, bg); const l1 = lum(F), l2 = lum(bg); return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05); };
const seiten = getComputedStyle(document.body).backgroundColor;
function hintergruende(e) { // Liste möglicher Hintergrundfarben (Verlaufsstopps zählen einzeln)
let ketten = [{ r: 246, g: 243, b: 239, a: 1 }];
const alle = []; for (let x = e; x; x = x.parentElement) alle.push(x);
let basis = parse(getComputedStyle(document.documentElement).backgroundColor); if (!basis || basis.a === 0) basis = null;
const bodyBg = parse(getComputedStyle(document.body).backgroundColor); if (bodyBg && bodyBg.a > 0) basis = bodyBg;
let stack = [basis || { r: 255, g: 255, b: 255, a: 1 }];
for (const x of alle.reverse()) {
const cs = getComputedStyle(x); const bc = parse(cs.backgroundColor); const bi = cs.backgroundImage;
const neu = [];
const stops = bi && bi !== 'none' ? (bi.match(/rgba?\\([^)]*\\)/g) || []).map(parse).filter(Boolean) : [];
for (const s of stack) {
let g = s; if (bc && bc.a > 0) g = mix(bc, s);
if (stops.length) { for (const st of stops) neu.push(mix(st, g)); } else { neu.push(g); }
}
stack = neu.length > 8 ? neu.slice(0, 8) : neu;
}
return stack;
}
const sicht = e => { const r = e.getBoundingClientRect(); const s = getComputedStyle(e); return r.width > 0 && r.height > 0 && s.visibility !== 'hidden' && s.display !== 'none' && parseFloat(s.opacity) > 0.05; };
const treffer = {}; const gesamt = {};
document.querySelectorAll('body *').forEach(e => {
if (!sicht(e)) return;
const direkt = Array.from(e.childNodes).filter(n => n.nodeType === 3 && n.textContent.trim().length > 1).map(n => n.textContent.trim()).join(' ');
if (!direkt) return;
if (e.closest('svg,script,style')) return;
const cs = getComputedStyle(e); const f = parse(cs.color); if (!f) return;
const gross = parseFloat(cs.fontSize) >= 24 || (parseFloat(cs.fontSize) >= 18.66 && parseInt(cs.fontWeight, 10) >= 700);
const grenze = gross ? 3 : 4.5;
let min = 99; let bgMin = null;
for (const bg of hintergruende(e)) { const q = ratio(f, bg); if (q < min) { min = q; bgMin = bg; } }
const schl = e.tagName.toLowerCase() + (e.className && typeof e.className === 'string' ? '.' + e.className.trim().split(/\\s+/).slice(0, 2).join('.') : '') + (e.closest('a') && e.tagName !== 'A' ? '(in a)' : '') ;
gesamt[schl] = (gesamt[schl] || 0) + 1;
if (min < grenze) { const k = schl; if (!treffer[k] || treffer[k].q > min) treffer[k] = { q: Math.round(min * 100) / 100, grenze, text: direkt.slice(0, 22), fg: cs.color, bg: 'rgb(' + Math.round(bgMin.r) + ',' + Math.round(bgMin.g) + ',' + Math.round(bgMin.b) + ')', n: 0 }; treffer[k].n = (treffer[k].n || 0) + 1; }
});
return JSON.stringify({ elemente: Object.values(gesamt).reduce((a, c) => a + c, 0), unter: Object.entries(treffer).sort((a, c) => a[1].q - c[1].q).map(([k, v]) => k + ' ' + v.q + ' (min ' + v.grenze + ') "' + v.text + '" ' + v.fg + ' auf ' + v.bg + ' x' + v.n) });
})()`;
const seiten = ['/ui', '/ui/modelle', '/ui/modelle?art=sprache', '/ui/engines', '/ui/chat', '/ui/personas', '/ui/keys', '/ui/warteschlange', '/ui/anfragen', '/ui/audit', '/ui/benutzer', '/ui/konto'];
try {
await b.login(BASE, 'claude', pw);
await b.viewport(1200, 900, { dsf: 1, mobile: false });
for (const theme of ['light', 'dark']) {
await b.send('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: theme }] });
const alle = {};
for (const p of seiten) {
await b.goto(BASE + p);
const r = JSON.parse(await b.eval(mess));
r.unter.forEach(u => { const k = u.replace(/ x\d+$/, ''); alle[k] = (alle[k] || []).concat(p); });
if (p === '/ui/modelle') await b.shot(`${process.argv[4] || '/tmp'}/kontrast-${tag}-${theme}.png`);
}
console.log('=== ' + theme + ' (' + Object.keys(alle).length + ' Befunde)');
Object.entries(alle).slice(0, 40).forEach(([k, v]) => console.log(k, ' ← ' + [...new Set(v)].join(', ')));
}
} finally { b.close(); }