Skip to content

Clone any website into a pixel-accurate Next.js app with AI — Playwright-measured extraction (no eyeballing), spec-gated parallel builder agents, and scored pixel-diff QA at phone/tablet/desktop. Works with Claude Code, Cursor, Copilot + 9 more.

feat(extract): scrape real CSS rules, not just computed styles

Mahanaicoachaefb21b8e988
diff --git a/docs/cover.png b/docs/cover.png
new file mode 100644
index 0000000..4cb98fc
Binary files /dev/null and b/docs/cover.png differ
diff --git a/examples/forty/comparison.png b/examples/forty/comparison.png
new file mode 100644
index 0000000..21ca828
Binary files /dev/null and b/examples/forty/comparison.png differ
diff --git a/scripts/extract/css.mjs b/scripts/extract/css.mjs
new file mode 100644
index 0000000..88a7326
--- /dev/null
+++ b/scripts/extract/css.mjs
@@ -0,0 +1,196 @@
+#!/usr/bin/env node
+// Scrape the target's ACTUAL CSS rules — not just computed styles.
+//
+// Computed styles only tell you how an element looks right now. The stylesheet
+// tells you every state it can be in: :hover, :focus, :active, media queries,
+// keyframes. Reading the rules catches interactions you'd never think to probe.
+//
+// Sources handled:
+// - inline <style> blocks
+// - same-origin stylesheets (via cssRules)
+// - cross-origin stylesheets (cssRules throws → fetched directly by URL)
+//
+// Usage:
+// node scripts/extract/css.mjs <url> # whole-page rule dump
+// node scripts/extract/css.mjs <url> --selector "nav a" # + exact matched rules
+// for that element (via CDP,
+// same data as the DevTools
+// Styles panel, incl. :hover)
+// Output: docs/research/<host>/css.json (+ a summary to stdout)
+import { launchPage, gotoAndSettle, autoScroll, hostOf, writeJson, parseArgs } from "../lib.mjs";
+
+const args = parseArgs(process.argv.slice(2));
+const url = args._[0];
+if (!url) {
+ console.error('Usage: node scripts/extract/css.mjs <url> [--selector "css"]');
+ process.exit(1);
+}
+
+const { browser, page } = await launchPage();
+await gotoAndSettle(page, url);
+await autoScroll(page);
+
+// ── 1. Collect stylesheet text from inside the page ───────────────────
+const collected = await page.evaluate(() => {
+ const sheets = [];
+ for (const sheet of document.styleSheets) {
+ let rules = null;
+ try {
+ rules = sheet.cssRules;
+ } catch {
+ // cross-origin: record the href so we can fetch it outside the page
+ sheets.push({ href: sheet.href, blocked: true, text: null });
+ continue;
+ }
+ const text = [...rules].map((r) => r.cssText).join("\n");
+ sheets.push({ href: sheet.href || "(inline <style>)", blocked: false, text });
+ }
+ return sheets;
+});
+
+// ── 2. Fetch anything the browser refused to expose ───────────────────
+for (const s of collected) {
+ if (!s.blocked || !s.href) continue;
+ try {
+ const res = await fetch(s.href, { signal: AbortSignal.timeout(20000), headers: { "User-Agent": "Mozilla/5.0" } });
+ if (res.ok) {
+ s.text = await res.text();
+ s.blocked = false;
+ s.fetched = true;
+ }
+ } catch {
+ /* leave blocked — reported in the summary */
+ }
+}
+
+const allCss = collected.filter((s) => s.text).map((s) => s.text).join("\n");
+
+// ── 3. Parse out the parts that matter for cloning ────────────────────
+// A light brace-matching parser: CSS is regular enough at this level, and this
+// avoids taking a dependency just to find hover rules.
+function splitRules(css) {
+ const out = [];
+ let depth = 0, buf = "", atRule = null, atBuf = "";
+ for (let i = 0; i < css.length; i++) {
+ const ch = css[i];
+ if (ch === "{") {
+ depth++;
+ if (depth === 1) {
+ const head = buf.trim();
+ buf = "";
+ if (head.startsWith("@media") || head.startsWith("@supports") || head.startsWith("@layer") || head.startsWith("@container")) {
+ atRule = head;
+ atBuf = "";
+ continue;
+ }
+ out.push({ selector: head, body: "", _open: true });
+ continue;
+ }
+ } else if (ch === "}") {
+ depth--;
+ if (depth === 0) {
+ if (atRule) {
+ // recurse into the group, tagging each inner rule with its condition
+ for (const inner of splitRules(atBuf)) out.push({ ...inner, media: atRule });
+ atRule = null;
+ atBuf = "";
+ } else {
+ const last = out[out.length - 1];
+ if (last?._open) {
+ last.body = buf.trim();
+ delete last._open;
+ }
+ buf = "";
+ }
+ continue;
+ }
+ }
+ if (atRule && depth >= 1) atBuf += ch;
+ else buf += ch;
+ }
+ return out.filter((r) => r.selector && !r.selector.startsWith("@"));
+}
+
+const rules = splitRules(allCss);
+
+const STATE_RE = /:(hover|focus|focus-visible|focus-within|active|checked|disabled|target|open)\b/;
+const interactive = rules
+ .filter((r) => STATE_RE.test(r.selector))
+ .map((r) => ({ selector: r.selector.trim(), declarations: r.body, media: r.media || null }));
+
+const byState = {};
+for (const r of interactive) {
+ const m = r.selector.match(STATE_RE);
+ const k = m ? m[1] : "other";
+ (byState[k] ||= []).push(r);
+}
+
+// Real breakpoint values, straight from the source
+const breakpoints = [...new Set(
+ (allCss.match(/@media[^{]+/g) || [])
+ .flatMap((m) => m.match(/\(\s*(min|max)-width\s*:\s*[^)]+\)/g) || [])
+ .map((s) => s.replace(/\s+/g, " ").trim())
+)].sort();
+
+const keyframeNames = [...new Set((allCss.match(/@keyframes\s+([\w-]+)/g) || []).map((s) => s.replace(/@keyframes\s+/, "")))];
+
+const host = hostOf(url);
+
+// ── 4. Optional: exact matched rules for one element, via CDP ─────────
+let matched = null;
+if (args.selector) {
+ const cdp = await page.context().newCDPSession(page);
+ await cdp.send("DOM.enable");
+ await cdp.send("CSS.enable");
+ const { root } = await cdp.send("DOM.getDocument", { depth: -1 });
+ const { nodeId } = await cdp.send("DOM.querySelector", { nodeId: root.nodeId, selector: args.selector });
+ if (!nodeId) {
+ console.error(`Element not found: ${args.selector}`);
+ } else {
+ const styles = await cdp.send("CSS.getMatchedStylesForNode", { nodeId });
+ matched = {
+ selector: args.selector,
+ // every rule that applies, in cascade order — same list DevTools shows
+ matchedRules: (styles.matchedCSSRules || []).map((m) => ({
+ selector: m.rule.selectorList?.text,
+ origin: m.rule.origin,
+ media: m.rule.media?.map((x) => x.text) || null,
+ declarations: (m.rule.style?.cssProperties || [])
+ .filter((p) => !p.disabled && p.value)
+ .map((p) => `${p.name}: ${p.value}`),
+ })),
+ // :hover / :focus / etc. rules Chrome knows about for this element
+ pseudoRules: (styles.pseudoElements || []).map((p) => ({
+ pseudoType: p.pseudoType,
+ rules: p.matches?.map((m) => ({
+ selector: m.rule.selectorList?.text,
+ declarations: (m.rule.style?.cssProperties || []).filter((x) => !x.disabled && x.value).map((x) => `${x.name}: ${x.value}`),
+ })),
+ })),
+ inherited: (styles.inherited || []).length,
+ };
+ }
+}
+
+await browser.close();
+
+writeJson(`docs/research/${host}/css.json`, {
+ url,
+ generatedAt: new Date().toISOString(),
+ stylesheets: collected.map((s) => ({ href: s.href, blocked: s.blocked, fetched: !!s.fetched, bytes: s.text?.length || 0 })),
+ totalRules: rules.length,
+ breakpoints,
+ keyframeNames,
+ interactiveStates: byState,
+ matched,
+});
+
+const blocked = collected.filter((s) => s.blocked);
+console.log(
+ `Stylesheets: ${collected.length} (${blocked.length} unreadable) · rules: ${rules.length} · interactive-state rules: ${interactive.length}`
+);
+console.log(`States found: ${Object.entries(byState).map(([k, v]) => `${k}=${v.length}`).join(" · ") || "none"}`);
+console.log(`Breakpoints: ${breakpoints.join(", ") || "none"}`);
+if (keyframeNames.length) console.log(`@keyframes: ${keyframeNames.join(", ")}`);
+if (blocked.length) console.log(`NOTE: ${blocked.length} stylesheet(s) could not be read or fetched — hover rules from those are missing.`);
+if (matched) console.log(`Matched rules for "${args.selector}": ${matched.matchedRules.length}`);
diff --git a/scripts/extract/tokens.mjs b/scripts/extract/tokens.mjs
index a1253f2..1a9fec0 100644
--- a/scripts/extract/tokens.mjs
+++ b/scripts/extract/tokens.mjs
@@ -55,22 +55,50 @@ const tokens = await page.evaluate(() => {
};
}
- // :root custom properties (same-origin stylesheets only)
+ // :root custom properties + @keyframes bodies (same-origin stylesheets only).
+ // Capturing keyframes matters: computed styles tell you `animation: slide-up-fade 1s`
+ // but not what slide-up-fade DOES. Without the rule body a builder can only guess.
const cssVars = {};
- for (const sheet of document.styleSheets) {
- let rules;
- try {
- rules = sheet.cssRules;
- } catch {
- continue; // cross-origin sheet
- }
+ const keyframes = {};
+ const walkRules = (rules) => {
for (const rule of rules) {
+ // CSSKeyframesRule
+ if (rule.type === 7 || rule.constructor?.name === "CSSKeyframesRule") {
+ const steps = {};
+ for (const kf of rule.cssRules) steps[kf.keyText] = kf.style.cssText;
+ keyframes[rule.name] = steps;
+ continue;
+ }
+ // nested groups: @media, @supports, @layer
+ if (rule.cssRules) {
+ try {
+ walkRules(rule.cssRules);
+ } catch { /* ignore */ }
+ continue;
+ }
if (rule.selectorText === ":root" || rule.selectorText === "html") {
for (const prop of rule.style) {
if (prop.startsWith("--")) cssVars[prop] = rule.style.getPropertyValue(prop).trim();
}
}
}
+ };
+ for (const sheet of document.styleSheets) {
+ try {
+ walkRules(sheet.cssRules);
+ } catch {
+ continue; // cross-origin sheet
+ }
+ }
+
+ // Which animations are actually used on the page, and on how many elements
+ const animationsInUse = {};
+ for (const el of els) {
+ const cs = getComputedStyle(el);
+ if (cs.animationName && cs.animationName !== "none") {
+ const key = `${cs.animationName} | ${cs.animationDuration} | ${cs.animationTimingFunction} | ${cs.animationIterationCount} | delay ${cs.animationDelay}`;
+ animationsInUse[key] = (animationsInUse[key] || 0) + 1;
+ }
}
// Font sources
@@ -101,6 +129,8 @@ const tokens = await page.evaluate(() => {
borderRadii: top(radii, 10),
boxShadows: top(shadows, 10),
cssVariables: cssVars,
+ keyframes,
+ animationsInUse,
fontLinks,
globalBehaviors,
};
@@ -108,4 +138,4 @@ const tokens = await page.evaluate(() => {
await browser.close();
writeJson(`docs/research/${hostOf(url)}/tokens.json`, { url, generatedAt: new Date().toISOString(), ...tokens });
-console.log(`Colors: ${tokens.colors.length} · Fonts: ${tokens.fontFamilies.length} · CSS vars: ${Object.keys(tokens.cssVariables).length}`);
+console.log(`Colors: ${tokens.colors.length} · Fonts: ${tokens.fontFamilies.length} · CSS vars: ${Object.keys(tokens.cssVariables).length} · @keyframes: ${Object.keys(tokens.keyframes).length} · animations in use: ${Object.keys(tokens.animationsInUse).length}`);