// Programa usado en la prueba de https://lyntev.com/es/guias/prompt-para-crear-una-pagina-web. Guárdelo como medir.mjs. // Mide páginas HTML sueltas: tamaño, peticiones, desplazamiento lateral, encabezados, contraste, botón principal y datos por confirmar. // Uso: node medir.mjs pagina1.html pagina2.html (necesita Node y Playwright con Chromium: npm i playwright && npx playwright install chromium) import fs from "node:fs"; import http from "node:http"; import { createRequire } from "node:module"; const { chromium } = createRequire(process.env.PLAYWRIGHT_DESDE ?? import.meta.url)("playwright"); const nav = await chromium.launch(); try { for (const f of process.argv.slice(2)) { const html = fs.readFileSync(f, "utf8"); const srv = http.createServer((q, r) => { if (q.url !== "/") { r.writeHead(404); return r.end(); } r.writeHead(200, { "content-type": "text/html; charset=utf-8" }); r.end(html); }).listen(0); await new Promise((r) => srv.once("listening", r)); const out = { archivo: f.split(/[\\/]/).pop(), bytes: Buffer.byteLength(html), peticiones: 0, desborde: {}, porConfirmar: (html.match(/\[DATO POR CONFIRMAR\]/g) || []).length }; for (const width of [320, 360, 390, 1280]) { const ctx = await nav.newContext({ viewport: { width, height: 800 } }), p = await ctx.newPage(); if (width === 360) p.on("request", (q) => { if (!/favicon/.test(q.url())) out.peticiones++; }); await p.goto(`http://127.0.0.1:${srv.address().port}/`); out.desborde[width] = await p.evaluate(() => document.documentElement.scrollWidth - document.documentElement.clientWidth); if (width === 360) Object.assign(out, await p.evaluate(() => { for (const d of document.querySelectorAll("details")) d.open = true; const rgb = (s) => s.match(/[\d.]+/g).slice(0, 3).map(Number), l = (c) => { const v = c.map((x) => x / 255).map((x) => (x <= 0.04045 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4)); return 0.2126 * v[0] + 0.7152 * v[1] + 0.0722 * v[2]; }; const fondo = (e) => { for (let x = e; x; x = x.parentElement) { const b = getComputedStyle(x).backgroundColor; if (!/rgba\(0, 0, 0, 0\)|transparent/.test(b)) return rgb(b); } return [255, 255, 255]; }; const ct = (e) => { const a = l(rgb(getComputedStyle(e).color)), b = l(fondo(e)); return +((Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05)).toFixed(2); }; const tx = [...document.querySelectorAll("h1,h2,h3,p,li,a,summary,span,strong,div.hueco")].filter((e) => e.offsetParent !== null && e.textContent.trim()); const b = document.querySelector("a.boton"), r = b?.getBoundingClientRect(), hs = [...document.querySelectorAll("h1,h2,h3,h4")].map((h) => +h.tagName[1]); return { titulo: document.title.length, descripcion: document.querySelector("meta[name=description]")?.content.length, h1: hs.filter((x) => x === 1).length, orden: hs.every((x, i) => i === 0 ? x === 1 : x - hs[i - 1] <= 1), encabezados: hs.length, contrasteMinimo: Math.min(...tx.map(ct)), boton: r && { alto: Math.round(r.height), visibleAlAbrir: r.bottom + scrollY <= innerHeight }, resumenesMin: Math.min(...[...document.querySelectorAll("summary")].map((x) => Math.round(x.getBoundingClientRect().height)), 999), idioma: document.documentElement.lang, scripts: document.scripts.length }; })); await ctx.close(); } console.log(JSON.stringify(out)); srv.close(); } } finally { await nav.close(); }