(() => { const variables = new Set(); const rules = []; const inaccessible = []; const visited = new Set(); function scanSheet(sheet) { if (!sheet || visited.has(sheet)) return; visited.add(sheet); try { scanRules(sheet.cssRules); } catch { inaccessible.push( sheet.href?.split("?")[0] || "inline" ); } } function scanRules(list) { for (const rule of list) { if (rule.style && rule.selectorText) { const properties = {}; for (const name of Array.from(rule.style)) { const value = rule.style .getPropertyValue(name).trim(); if (name.startsWith("--")) { variables.add(name); } for (const match of value.matchAll( /var\(\s*(--[\w-]+)/g )) { variables.add(match[1]); } if ( name.startsWith("--") || /^(color|background|border|font|padding|margin|gap|box-shadow|line-height|letter-spacing)/.test(name) ) { if (!/url\s*\(/i.test(value)) { properties[name] = value; } } } if (Object.keys(properties).length) { rules.push({ selector: rule.selectorText, properties }); } } if (rule.styleSheet) { scanSheet(rule.styleSheet); } else if (rule.cssRules) { scanRules(rule.cssRules); } } } Array.from(document.styleSheets).forEach(scanSheet); function getVariables(element) { const style = getComputedStyle(element); return Object.fromEntries( [...variables].sort() .map(name => [ name, style.getPropertyValue(name).trim() ]) .filter(([, value]) => value && !/url\s*\(/i.test(value)) ); } const data = { rootVariables: getVariables(document.documentElement), bodyVariables: getVariables(document.body), classes: [...new Set( Array.from(document.querySelectorAll("[class]")) .flatMap(el => Array.from(el.classList)) )].sort(), ids: [...new Set( Array.from(document.querySelectorAll("[id]")) .map(el => el.id) )].sort(), rules, inaccessible }; const blob = new Blob( [JSON.stringify(data, null, 2)], { type: "application/json" } ); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = `${location.hostname}-css-audit.json`; link.click(); setTimeout(() => URL.revokeObjectURL(url), 1000); console.log("Audit finished:", { variables: variables.size, rules: rules.length, classes: data.classes.length, ids: data.ids.length }); })();