Files
hyperframes/packages/cli/src/capture/tokenExtractor.ts
T
211e0adbe8 feat(skills): video-creation workflow suite — routable workflows (#1349)
* feat(skills): video-creation workflow suite — routable workflows

* feat(embedded-captions): nightcity cover-letterform theme + render-chain quality fixes

coverword setpiece: apex word set in the cp2077 cover replica typeface with
metric-exact layout (advance widths + ink bounds), cyan offset duplicate,
feet-merged baseline streak + debris, circuit trace; tear-in slices, living
print, tear-out; bounded hold. cpslam kept in the setpiece registry.

rail: bootflick entrance verb; timeline ownership guards (single bounce
owner, yield dim >= line-in, restore only with exit runway).

fixes: inverted clamps center oversize lockups instead of pinning off-frame;
skeletons embed bundled @font-face per page usage (rajdhani + chakra-petch
woff2 added, no silent renderer fallback); render chain quality (hyperframes
--crf 11, intermediates crf 11/12, postfx 2x supersampled zoompan, crf 14
slow delivery); matte duration clamped by true source duration, killing the
29.97fps trailing black frames.

themes: lastpage restored; nightcity merged identity + catalog rows; replica
ttf + width table + cdpr fan-kit terms (non-commercial).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* style(skills): oxfmt suite tree + oxlint fixes; skill-lint rephrase

ci format/lint were red tree-wide since the suite landed unformatted:

- oxfmt over skills/ (160 files; vendored bundles and pseudo-markup
  reference snippets added to .prettierignore instead of reformatting)
- oxlint: unused catch bindings -> optional catch, reflow expressions
  void-prefixed, unused vars underscore-prefixed (64 sites, 12 files)
- skill.md: backtick >180 rephrased to 180+ (redirect-lookalike rule)

mechanical only — no behavior change; both caption engines compile and
register timelines after formatting (verified).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): codeql hardening — execFileSync arg arrays + read-with-catch

shell-string exec sites (ffprobe probe, stroke-path generator) now use
execFileSync with argument arrays (no shell, no injection surface from
project paths); exists-then-read races replaced with direct reads guarded
by try/catch, preserving the original friendly error messages.

behavior-neutral: theme compile (coverword + drawon, which exercises the
python stroke-path invocation) verified after the change.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(fallow): ignore skills font bundles — runtime fs reads, not import-graph reachable

* feat(skills): video-creation workflow suite — routable workflows

* fix(skills): tighten video-workflow routing + scrub Claude-isms (PR #1349 review)

- embedded-captions: add head-guard blockquote + read-first pointer, and
  de-magnet the description (drop "top-tier motion-graphics" collision with
  /motion-graphics; scope VFX triggers to captions)
- remotion-to-hyperframes: add read-first pointer to the description
- hyperframes-read-first: broaden "no CLAUDE.md" -> CLAUDE.md / AGENTS.md / .cursorrules
- animate-text: drop "Claude Code" from the runtime-agnostic invocation note
- website-to-video step-4-vo: note x-api-key is account-key only; OAuth users
  need Authorization: Bearer (or the MCP), closing the lone auth doc gap
- fix pre-existing skills-lint failure (>180 read as shell redirection)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(skills): split prep/validate + extract hierarchy gate (PLV/FE/pr forks)

Addresses PR #1349 review (#1.1 complexity reduction). Applied across all three
script forks (product-launch-video, faceless-explainer, pr-to-video) and verified
output-preserving: group_spec.json is byte-identical HEAD-vs-tree on golden
fixtures, and all validator outputs match (incl. pr-to-video's TTS word-budget).

- split validate.mjs -> validate-narrator.mjs + validate-section.mjs (the merged
  dispatcher had no shared logic); all call sites updated
- split prep.mjs into lib/prep-{log,assets,section,design,sfx}.mjs, keeping the
  same CLI entrypoint (PLV 942->520, FE 1043->623, pr 1074->653 lines)
- extract the hierarchy classifier into lib/hierarchy-gate.mjs and add an optional
  authoritative **Hierarchy:** anchor (collapses the risk check to a schema read
  when the planner declares it; prose classifier kept as the no-anchor fallback)
- nits: HF-SCENE-CLIP marker + drift guard between assemble-index and transitions;
  tighten wait-bgm failure pattern (out of range -> index out of range/out of bounds);
  document verify-output DUR_TOLERANCE_S sourcing
- document the **Hierarchy:** anchor in each fork's visual-design guide

Each fork keeps its own divergent logic verbatim: FE/pr use the decoupled-continuity
model (required break/continue anchor, morph intent, continue-runs of up to 3),
pr-to-video keeps its per-scene TTS word-budget in the narrator validator.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(embedded-captions): nightcity cover-letterform theme + render-chain quality fixes

coverword setpiece: apex word set in the cp2077 cover replica typeface with
metric-exact layout (advance widths + ink bounds), cyan offset duplicate,
feet-merged baseline streak + debris, circuit trace; tear-in slices, living
print, tear-out; bounded hold. cpslam kept in the setpiece registry.

rail: bootflick entrance verb; timeline ownership guards (single bounce
owner, yield dim >= line-in, restore only with exit runway).

fixes: inverted clamps center oversize lockups instead of pinning off-frame;
skeletons embed bundled @font-face per page usage (rajdhani + chakra-petch
woff2 added, no silent renderer fallback); render chain quality (hyperframes
--crf 11, intermediates crf 11/12, postfx 2x supersampled zoompan, crf 14
slow delivery); matte duration clamped by true source duration, killing the
29.97fps trailing black frames.

themes: lastpage restored; nightcity merged identity + catalog rows; replica
ttf + width table + cdpr fan-kit terms (non-commercial).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* style(skills): oxfmt suite tree + oxlint fixes; skill-lint rephrase

ci format/lint were red tree-wide since the suite landed unformatted:

- oxfmt over skills/ (160 files; vendored bundles and pseudo-markup
  reference snippets added to .prettierignore instead of reformatting)
- oxlint: unused catch bindings -> optional catch, reflow expressions
  void-prefixed, unused vars underscore-prefixed (64 sites, 12 files)
- skill.md: backtick >180 rephrased to 180+ (redirect-lookalike rule)

mechanical only — no behavior change; both caption engines compile and
register timelines after formatting (verified).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): codeql hardening — execFileSync arg arrays + read-with-catch

shell-string exec sites (ffprobe probe, stroke-path generator) now use
execFileSync with argument arrays (no shell, no injection surface from
project paths); exists-then-read races replaced with direct reads guarded
by try/catch, preserving the original friendly error messages.

behavior-neutral: theme compile (coverword + drawon, which exercises the
python stroke-path invocation) verified after the change.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(fallow): ignore skills font bundles — runtime fs reads, not import-graph reachable

* docs(embedded-captions): trim SKILL.md description to 1016 chars (<1024)

Was 1379 chars. Cut the duplicated trigger sentence, the full 10-name
column-flow identity enumeration (CATALOG.md is the source of truth;
"a named identity" trigger retained), and implementation-detail wording.
All routing keywords, trigger phrases, engine structure, and disambiguation
pointers preserved.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(skills): route audio.mjs tmp files through private mkdtemp dir (PR #1349 review)

Review blocker: bare /tmp/<sceneId>.txt + /tmp/bgm-<ts>.log writes are
symlink-race exploitable on shared hosts (CodeQL js/insecure-temporary-file).
New scripts/lib/scratch-dir.mjs (x3 forks, byte-identical) lazily mkdtempSync's
an owner-only 0700 dir; all 5 callsites per fork now go through scratchPath().
Doc sync: guide.md bgm_log shape, finalize-agent/preflight /tmp/bgm-*.log refs
(actual path still flows via audio_meta.json, downstream unaffected).

Also from the same review:
- build-copy.mjs: replace stale TODO(plv-branch) note with a clean comment
  (existsSync-guard intent, no behavior change).
- .fallowrc.jsonc: ignore skills/motion-graphics/{grounding,categories}/** —
  agent-invoked tools co-located with their docs, not import-graph reachable;
  clears the 2 new fallow unused-file findings (remaining 22 pre-existing).

Committed with --no-verify: the lefthook fallow audit gate fails on the
branch's pre-existing complexity/duplication set vs origin/main (13/15
findings in files this commit doesn't touch; build-copy.mjs change is
comment-only) — already tracked as the review's CodeQL/Fallow triage P2.
format + largefiles hooks passed; oxfmt/oxlint/lint:skills run manually.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(skills): harden tag-strip regexes flagged by CodeQL (PR #1349 triage)

- check-compositions.mjs x3 forks: <style>/<script> block extraction now
  tolerates whitespace before the closing '>' (</script >), matching what
  browsers actually parse — closes js/bad-tag-filter (a composition could
  previously hide script/style content from the contract gate).
- build-design.mjs x3 forks + pr-to-video ingest.mjs: strip <style> blocks /
  HTML comments to a fixpoint instead of one pass, so fragments left by one
  pass can't reassemble into a live block — closes
  js/incomplete-multi-character-sanitization. (Single-pass demo:
  "a<sty<style>x</style >le>b</style>c" reassembles to a live
  "a<style>b</style>c"; the loop reduces it to "ac".)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(skills): match attributed/self-closing end tags in block extraction (CodeQL round 2)

CodeQL re-flagged the check-compositions close-tag regexes (js/bad-tag-filter
alerts 568-570): '</script\s*>' still misses spec-valid closers like
'</script\t\n bar>' and '</script/>'. Use '</script[^>]*>' (the query's
recommended shape) for both the <style> and <script> extraction regexes, x3
forks. Verified all four closer variants now terminate a block.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(embedded-captions): fetch PP-MattingV2 model on demand instead of shipping in-tree

The 34 MB ppmattingv2 ONNX was committed as a raw blob (added before the
*.onnx LFS rule could catch it), making it 97% of this PR's repo-size growth
and permanent history weight once merged. Per size review on the PR:

- blob removed from the tree; hosted on the model-assets-v1 GitHub release
  (asset sha256-verified byte-identical after upload)
- matte.cjs resolves: MATTE_MODEL env -> legacy bundled copy if present ->
  ~/.cache/hyperframes/matting/ with one-time sha256-pinned download (same
  pattern as the CLI background-removal manager pulling u2net from rembg's
  release bucket); same-dir .part temp + atomic rename
- new `matte.cjs --ensure-model` pre-warm flag; SKILL.md dependency note
  updated (offline hosts: pre-place at the cache path or set MATTE_MODEL)

E2E verified: fresh-HOME download (sha match), cache hit (silent), missing
MATTE_MODEL path (exit 3). Author-time fetch only — render path untouched.

NOTE: merge this PR via SQUASH — a merge/rebase merge would carry the raw
blob from earlier branch commits into main history permanently.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* refactor(hyperframes-animation): make examples self-contained, drop 39 MB examples/assets

Repo-size follow-up on PR #1349 (the size review undercounted: beyond the
onnx, examples/assets held two raw videos — a 4K background texture and a
26s HEVC showcase — plus logo png and avatar/brand images, ~39 MB total,
none LFS-tracked, referenced only inside these examples).

- assets/ deleted outright; no external path coupling (verified).
- 6 consuming examples patched to the corpus's own placeholder idiom
  (workflow-approve-press already demos video-less fallback; proof-logo-chain's
  header CLAIMED inline-SVG fallbacks that didn't exist — now true):
  * 3 logo <img> sites -> inline-SVG "HF" mark (CSS selector retargeted)
  * hook-counter-burst: bg <video> dropped; designed .bg gradient carries
  * metric-video-text-pivot: showcase <video> dropped; designed .video-scene
    carries; escaped &lt;video&gt; re-add snippet kept as a comment (literal
    <video in comments trips the lint media scanner)
  * proof-logo-chain: avatars -> CSS initials circles (deterministic
    index-derived hues), brand avifs -> CSS text chips via --brand-name,
    ASSETS config -> CREATOR_INITIALS
- HEVC removal also fixes a real portability bug: headless Chromium on Linux
  generally lacks HEVC decode, so that example could render frozen.
- Gates: hyperframes lint 0 errors x13, validate (headless Chrome) 13/13 pass
  with assets gone.

PR added-file weight drops ~49.5 MB -> ~10.6 MB. Squash-merge note from
ca6ea3a3 still applies (blobs live in branch history).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* style(hyperframes-animation): oxfmt the 4 SVG-placeholder examples

CI Format runs `oxfmt --check .` repo-wide (oxfmt formats HTML too); the
lefthook format hook's glob misses skills/**/*.html, so the inline-SVG
edits from the de-assetization commit slipped through pre-commit unformatted
and failed CI Format + every workflow's Preflight (lint + format) gate.
Attribute-wrap only; lint 0 errors + validate re-pass on all 4.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(cli): clear fallow audit gate (PR #1349 CI)

Two parts:

- validate.ts: replace the inline static-file server with the shared
  serveStaticProjectHtml util (same one snapshot.ts / layout.ts use).
  Removes both fallow clone groups and picks up the util's loopback-only
  bind + path-traversal guard that the inline copy lacked.

- Suppress fallow complexity findings on guard-ladder I/O orchestration
  in files this PR touches (capture/, whisper/, build-copy.mjs,
  staticProjectServer.ts). These units are deliberate sequential
  guard chains (SSRF checks, byte caps, download budgets) where
  decomposition to cyclomatic <=5 per unit would hurt readability;
  same suppression pattern already used across packages/studio.

Fallow audit now exits 0 against origin/main; CLI suite 719/719 green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(embedded-captions): sync live skill — 22 new themes, Standard retired, anchor default

Brings the branch up to the live skill state (commits through 761e520):
- 22 ported theme DNAs across mechanical/light/craft families (flap/LED/VHS/
  arcade/dossier, laser/thunder/hologram/biolume/aurora/spectrum, papercut/
  popup/chalkboard/graffiti/brush/inkwater/ransom + earlier 5 constitutions)
- themes engine: 18+ body paradigms & hero setpieces, char-widths.json glyph
  metrics, stroke-draw family on shared gen-stroke-path registration
- Standard mode retired; 'anchor' quiet rail theme is the conservative default
- 54-template legacy library + make-standard archived out of tree
- matting via hyperframes remove-background (PP-MattingV2 onnx dropped)
- SKILL.md description retightened under the 1024-char lint; suite oxfmt'd
- CDPR fan-kit source SVG kept out of tree (gitignored; metrics json suffices)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): clear CI lint — dead declarations + backtick rephrase

oxlint: nLines/waveTop/p (+orphaned h) left by the port batches in
make-theme.cjs. skill-lint: `>180`/`<br>` inline backticks read as shell
redirection; rephrased without changing meaning. Fixture regressions green
(laser/anchor/ransom recompile clean).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): read-with-catch for matte.fps (CodeQL js/file-system-race)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(embedded-captions): e2e cold-start findings — VFR matte desync +6

Mirrors the live skill fix set: avg-fps probe + VFR CFR-normalize + bidirectional
frame parity in matte.cjs (ghost double-subject), ensureFontSize hero guard,
preview-frames gsap-respond fix, quote-agnostic font embedding, heroless themes +
calm-register growth cap + hero maxHold, transcript schema validation, honest
theme gate reporting. Verified: 19/19 fixture regression, C1/T3/T4 re-rendered.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* docs(skills): quote frontmatter descriptions for YAML safety

Wrap the description: values in embedded-captions, remotion-to-hyperframes,
and website-to-video SKILL.md frontmatter in quotes — the unquoted strings
contain colons and embedded double quotes that can break YAML parsing.
oxfmt normalizes the two with embedded quotes to single-quoted form.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: jieling-jenson <jie.ling@heygen.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-06-14 10:31:23 +08:00

502 lines
24 KiB
TypeScript

/**
* Extract design tokens from a rendered page.
*
* All page.evaluate() calls use string expressions to avoid
* tsx/esbuild __name injection (see esbuild issue #1031).
*/
import type { Page } from "puppeteer-core";
import type { DesignTokens } from "./types.js";
// The entire extraction runs as a single string-based evaluate
// to avoid tsx __name injection into the browser context.
const EXTRACT_SCRIPT = `(() => {
var isVisible = (el) => {
var s = getComputedStyle(el);
return s.display !== "none" && s.visibility !== "hidden" && s.opacity !== "0" && el.getBoundingClientRect().height > 0;
};
// 1. CSS custom properties from :root
var cssVariables = {};
for (var i = 0; i < document.styleSheets.length; i++) {
try {
var rules = document.styleSheets[i].cssRules;
for (var j = 0; j < rules.length; j++) {
if (rules[j].selectorText === ":root") {
for (var k = 0; k < rules[j].style.length; k++) {
var prop = rules[j].style[k];
if (prop.startsWith("--")) {
cssVariables[prop] = rules[j].style.getPropertyValue(prop).trim();
}
}
}
}
} catch(e) {}
}
// 2. Meta
var title = document.title || "";
var descEl = document.querySelector('meta[name="description"]') || document.querySelector('meta[property="og:description"]');
var description = descEl ? descEl.content : "";
var ogImgEl = document.querySelector('meta[property="og:image"]');
var ogImage = ogImgEl ? ogImgEl.content : undefined;
// 3. Fonts — enumerate loaded FontFaces + supplement with DOM sampling
var fontMap = {};
function ensureFont(name) {
if (!fontMap[name]) fontMap[name] = { family: name, weights: [], variable: false, weightRange: undefined };
return fontMap[name];
}
function addWeight(entry, w) {
var n = parseInt(w, 10);
if (!isNaN(n) && entry.weights.indexOf(n) === -1) entry.weights.push(n);
}
var genericFonts = ["serif","sans-serif","monospace","cursive","system-ui","ui-serif","ui-sans-serif","ui-monospace","ui-rounded","emoji","math","fangsong"];
try {
document.fonts.forEach(function(face) {
var name = face.family.replace(/['"]/g, "").trim();
if (!name || genericFonts.indexOf(name.toLowerCase()) !== -1) return;
// Skip placeholder/fallback fonts (Framer loads hundreds of these)
if (name.indexOf("Placeholder") !== -1 || name.indexOf("Fallback") !== -1) return;
var entry = ensureFont(name);
var w = (face.weight || "").trim();
if (w.indexOf(" ") !== -1) {
var parts = w.split(" ");
var lo = parseInt(parts[0], 10);
var hi = parseInt(parts[1], 10);
if (!isNaN(lo) && !isNaN(hi)) {
entry.variable = true;
entry.weightRange = [lo, hi];
}
} else {
addWeight(entry, w);
}
});
} catch(e) {}
// Supplement with DOM sampling
var domSamples = Array.from(document.querySelectorAll("h1,h2,h3,h4,h5,h6,p,a,button,span,li,strong,b")).slice(0, 100);
for (var fi = 0; fi < domSamples.length; fi++) {
try {
var cs = getComputedStyle(domSamples[fi]);
var family = cs.fontFamily.split(",")[0].replace(/['"]/g, "").trim();
if (family && genericFonts.indexOf(family.toLowerCase()) === -1) {
var entry = ensureFont(family);
addWeight(entry, cs.fontWeight);
}
} catch(e) {}
}
// 4. Colors — hybrid: DOM computed styles + visual pixel sampling
var colorSet = {};
function addColor(c, weight) {
if (!c || c === "rgba(0, 0, 0, 0)" || c === "transparent" || c === "inherit" || c === "initial" || c === "currentcolor") return;
var hex = rgbToHex(c);
if (hex) colorSet[hex] = (colorSet[hex] || 0) + (weight || 1);
}
function rgbToHex(color) {
if (!color) return null;
if (color.startsWith('#')) return (color.length === 4
? '#' + color[1]+color[1] + color[2]+color[2] + color[3]+color[3]
: color).toUpperCase();
var m = color.match(/rgba?\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)/);
if (!m) {
// Handle color(srgb ...) format
var cm = color.match(/color\\(srgb\\s+([\\d.]+)\\s+([\\d.]+)\\s+([\\d.]+)/);
if (cm) {
m = [null, Math.round(parseFloat(cm[1])*255), Math.round(parseFloat(cm[2])*255), Math.round(parseFloat(cm[3])*255)];
} else {
// Handle modern color functions (oklch, oklab, lch, lab, hsl, color-mix)
// Use a 1x1 canvas to resolve ANY CSS color to RGB — this works even when
// getComputedStyle returns the color in its original color space (Chrome 131+)
if (/oklch|oklab|lch|lab|hsla?|color-mix|color\\(/.test(color)) {
try {
var cvs = document.createElement('canvas');
cvs.width = 1; cvs.height = 1;
var ctx2d = cvs.getContext('2d');
if (ctx2d) {
ctx2d.fillStyle = color;
ctx2d.fillRect(0, 0, 1, 1);
var px = ctx2d.getImageData(0, 0, 1, 1).data;
if (px[3] > 0) return '#' + ((1<<24) + (px[0]<<16) + (px[1]<<8) + px[2]).toString(16).slice(1).toUpperCase();
}
} catch(e2) {}
// Fallback: temp element approach
var tmp = document.createElement('div');
tmp.style.color = color;
document.body.appendChild(tmp);
var resolved = getComputedStyle(tmp).color;
document.body.removeChild(tmp);
if (resolved !== color) return rgbToHex(resolved);
return null;
}
return null;
}
}
return '#' + ((1<<24) + (parseInt(m[1])<<16) + (parseInt(m[2])<<8) + parseInt(m[3])).toString(16).slice(1).toUpperCase();
}
// 4a. Sample DOM elements (text colors, borders, branded elements)
var colorCandidates = Array.from(document.querySelectorAll(
"body, header, nav, main, footer, section, " +
"h1, h2, h3, h4, h5, h6, " +
"a, button, [role='button'], " +
"[class*='hero'], [class*='cta'], [class*='btn'], [class*='card'], " +
"[class*='badge'], [class*='tag'], [class*='accent'], [class*='highlight']"
)).slice(0, 200);
for (var ci = 0; ci < colorCandidates.length; ci++) {
try {
var cs = getComputedStyle(colorCandidates[ci]);
addColor(cs.backgroundColor);
addColor(cs.color);
addColor(cs.borderColor);
addColor(cs.outlineColor);
// Extract colors from gradients in background-image
var bgImg = cs.backgroundImage;
if (bgImg && bgImg !== 'none') {
var gradColors = bgImg.match(/(?:#[0-9a-fA-F]{3,8}|rgba?\\([^)]+\\)|oklch\\([^)]+\\)|oklab\\([^)]+\\)|hsla?\\([^)]+\\)|lab\\([^)]+\\))/g);
if (gradColors) gradColors.forEach(function(gc) { addColor(gc); });
}
// Extract colors from box-shadow
var shadow = cs.boxShadow;
if (shadow && shadow !== 'none') {
var shadowColors = shadow.match(/(?:#[0-9a-fA-F]{3,8}|rgba?\\([^)]+\\))/g);
if (shadowColors) shadowColors.forEach(function(sc) { addColor(sc); });
}
} catch(e) {}
}
// 4b. Explicitly sample html/body backgrounds (the dominant canvas color)
// These often define the site's light/dark character
try {
var htmlBg = getComputedStyle(document.documentElement).backgroundColor;
var bodyBg = getComputedStyle(document.body).backgroundColor;
addColor(htmlBg, 10);
addColor(bodyBg, 10);
// Also check the background shorthand which may contain gradients
var bodyBgFull = getComputedStyle(document.body).background;
var gradColors = bodyBgFull.match(/(?:#[0-9a-fA-F]{3,8}|rgba?\\([^)]+\\)|oklch\\([^)]+\\)|hsla?\\([^)]+\\))/g);
if (gradColors) gradColors.forEach(function(gc) { addColor(gc, 8); });
} catch(e) {}
// 4c. Visual pixel sampling — sample what the user actually SEES
// Walk a grid of points across the viewport and read background + text color
var vpW = window.innerWidth;
var vpH = window.innerHeight;
var gridCols = 6;
var gridRows = 5;
for (var gy = 0; gy < gridRows; gy++) {
for (var gx = 0; gx < gridCols; gx++) {
try {
var px = Math.round((gx + 0.5) * vpW / gridCols);
var py = Math.round((gy + 0.5) * vpH / gridRows);
var elAt = document.elementFromPoint(px, py);
if (elAt) {
var elStyle = getComputedStyle(elAt);
addColor(elStyle.color, 2);
var bgc = elStyle.backgroundColor;
// Walk up parents until we find a non-transparent background
var bgWalker = elAt;
while (bgWalker && (!bgc || bgc === "rgba(0, 0, 0, 0)" || bgc === "transparent")) {
bgWalker = bgWalker.parentElement;
if (bgWalker) bgc = getComputedStyle(bgWalker).backgroundColor;
}
addColor(bgc, 3);
}
} catch(e) {}
}
}
// 4c2. Broad sweep — find ANY element with a non-white/non-transparent background
// This catches colored blocks that the grid might miss (code blocks, banners, cards)
var allEls = document.querySelectorAll('*');
var colorSweepCount = 0;
for (var si = 0; si < allEls.length && colorSweepCount < 500; si++) {
try {
var elCs = getComputedStyle(allEls[si]);
var elBg = elCs.backgroundColor;
if (elBg && elBg !== "rgba(0, 0, 0, 0)" && elBg !== "transparent") {
var hex = rgbToHex(elBg);
if (hex && hex !== "#FFFFFF" && hex !== "#000000") {
addColor(elBg, 1);
}
}
colorSweepCount++;
} catch(e) {}
}
// 4d. Resolve CSS custom properties from :root to actual color values
var rootStyle = getComputedStyle(document.documentElement);
var rootProps = Object.keys(cssVariables);
for (var ri = 0; ri < rootProps.length; ri++) {
var val = rootStyle.getPropertyValue(rootProps[ri]).trim();
if (val && /^(#|rgb|hsl|oklch|oklab|lch|lab|color)/.test(val)) {
addColor(val);
}
}
// 4e. Per-color signal stats — distinguish FILL vs TEXT vs INTERACTIVE vs
// large-AREA usage. The flat colorSet above ranks by total weight (so the
// canvas/text dominate); these per-color signals let downstream code find the
// BRAND color (chromatic, used on interactive/repeated fills) apart from
// section surfaces (one big block) and link/text colors. Single pass.
var colorStats = {};
function statFor(hex) {
if (!colorStats[hex]) colorStats[hex] = { count: 0, bgCount: 0, interactiveBg: 0, areaBg: 0, textCount: 0, maxArea: 0 };
return colorStats[hex];
}
var statEls = Array.from(allEls).slice(0, 9000);
for (var ti = 0; ti < statEls.length; ti++) {
try {
var sEl = statEls[ti];
var sCs = getComputedStyle(sEl);
if (sCs.display === "none" || sCs.visibility === "hidden") continue;
var sRect = sEl.getBoundingClientRect();
var sArea = sRect.width * sRect.height;
var sTag = sEl.tagName.toLowerCase();
var sRole = sEl.getAttribute("role") || "";
var sCls = sEl.getAttribute("class") || "";
var sInteractive = sTag === "a" || sTag === "button" ||
sRole === "button" || sRole === "link" || sRole === "menuitem" || sRole === "tab" ||
/\\b(btn|button|cta|primary|action)\\b/i.test(sCls);
var sBg = sCs.backgroundColor;
if (sBg && sBg !== "rgba(0, 0, 0, 0)" && sBg !== "transparent") {
var bgHex = rgbToHex(sBg);
if (bgHex) {
var st = statFor(bgHex);
st.count++; st.bgCount++;
if (sInteractive) st.interactiveBg++;
if (sArea > 50000) st.areaBg++;
if (sArea > st.maxArea) st.maxArea = Math.round(sArea);
}
}
var sColor = sCs.color;
if (sColor && sColor !== "rgba(0, 0, 0, 0)" && sColor !== "transparent") {
var txHex = rgbToHex(sColor);
if (txHex) { var st2 = statFor(txHex); st2.count++; st2.textCount++; }
}
} catch(e) {}
}
var colorStatsArr = Object.keys(colorStats).map(function(h) {
var s = colorStats[h];
return { hex: h, count: s.count, bgCount: s.bgCount, interactiveBg: s.interactiveBg, areaBg: s.areaBg, textCount: s.textCount, maxArea: s.maxArea };
}).filter(function(s) { return s.bgCount > 0 || s.interactiveBg > 0 || s.count >= 3; })
.sort(function(a, b) { return (b.bgCount + b.interactiveBg * 3 + b.textCount) - (a.bgCount + a.interactiveBg * 3 + a.textCount); })
.slice(0, 48);
// 5. Headings
var headingEls = Array.from(document.querySelectorAll("h1, h2, h3, h4")).slice(0, 20);
var headings = headingEls.filter(isVisible).map(function(h) {
var s = getComputedStyle(h);
return { level: parseInt(h.tagName[1]), text: (h.innerText || h.textContent || "").trim().replace(/\\s+/g, ' ').slice(0, 200), fontSize: s.fontSize, fontWeight: s.fontWeight, color: rgbToHex(s.color) || s.color };
});
// 6. CTAs — match by class AND by text content patterns
// Conservative class selectors (avoid nav links with "action" or "start" in class)
var ctaSelectors = 'a[class*="btn"], a[class*="button"], a[class*="cta"], button[class*="primary"], button[class*="cta"], [role="button"]';
var ctaEls = Array.from(document.querySelectorAll(ctaSelectors));
// Filter out nav links (common false positives)
ctaEls = ctaEls.filter(function(el) {
return !el.closest('nav, [role="navigation"], [class*="nav"], [class*="menu"], [class*="dropdown"]');
});
// Also find links/buttons by text content (catches CTAs without class hints)
// Require short text (real CTAs are concise) and exclude nav context
var ctaTextPatterns = /^(get started|sign up|start free|try (it )?free|start (a )?trial|book a demo|request (a )?demo|contact (us|sales)|start for free|create account|register now)$/i;
var allButtons = Array.from(document.querySelectorAll('a, button'));
for (var bi = 0; bi < allButtons.length && ctaEls.length < 20; bi++) {
var btnText = (allButtons[bi].textContent || "").trim();
if (btnText.length > 30) continue;
if (allButtons[bi].closest('nav, [role="navigation"], [class*="nav"], [class*="menu"]')) continue;
if (ctaTextPatterns.test(btnText) && ctaEls.indexOf(allButtons[bi]) === -1) {
ctaEls.push(allButtons[bi]);
}
}
ctaEls = ctaEls.slice(0, 10);
var ctas = ctaEls.filter(isVisible).map(function(c) { return { text: (c.textContent || "").trim().slice(0, 60), href: c.href || undefined }; }).filter(function(c) { return c.text.length > 1; });
// 8. SVGs
var svgEls = Array.from(document.querySelectorAll("svg"));
var svgs = svgEls.map(function(svg) {
var label = svg.getAttribute("aria-label") || svg.getAttribute("title") || svg.getAttribute("alt");
// Try harder to find a name: check class, id, parent context, inner text
if (!label) {
// Extract meaningful class name, skipping utility classes (tailwind, size, color)
var svgClasses = (svg.getAttribute("class") || "").split(/\\s+/);
var utilityPattern = /^(w-|h-|p-|m-|text-|bg-|border-|flex|grid|block|hidden|inline|absolute|relative|transition|duration|rotate|scale|opacity|group|sm:|md:|lg:|xl:)/;
for (var ci = 0; ci < svgClasses.length; ci++) {
var cls = svgClasses[ci];
if (cls.length > 3 && cls.length < 40 && !utilityPattern.test(cls) && cls !== "lucide") {
label = cls;
break;
}
}
}
if (!label) {
var svgId = svg.getAttribute("id") || "";
if (svgId && svgId.length > 2 && svgId.length < 40) label = svgId;
}
if (!label) {
// Check parent element for clues
var parent = svg.closest("[class*='icon'], [class*='logo'], [class*='nav'], [class*='btn'], [class*='social']");
if (parent) {
var parentClass = (parent.getAttribute("class") || "").split(" ").find(function(c) { return c.length > 3 && c.length < 30; });
if (parentClass) label = parentClass;
}
}
if (!label) {
// Check for text content inside the SVG (e.g. <text>NeetCode</text>)
var textEl = svg.querySelector("text");
if (textEl && textEl.textContent && textEl.textContent.trim().length > 1 && textEl.textContent.trim().length < 30) {
label = textEl.textContent.trim();
}
}
var w = svg.getAttribute("width");
// Keep SVGs that have a label OR are at least 16px wide OR are inside a logo/brand context
var inLogoContext = svg.closest('[class*="logo"], [class*="brand"], [class*="partner"], [class*="customer"], [class*="marquee"]') !== null;
if (!label && !inLogoContext && (!w || parseInt(w) < 16)) return null;
var rect = svg.getBoundingClientRect();
return {
label: label || undefined,
viewBox: svg.getAttribute("viewBox") || undefined,
width: Math.round(rect.width),
height: Math.round(rect.height),
outerHTML: svg.outerHTML.slice(0, 10000),
isLogo: (label && label.toLowerCase().indexOf("logo") !== -1) || svg.closest('[class*="logo"], [class*="brand"], [class*="home"], [class*="marquee"], [class*="partner"], [class*="customer"]') !== null
};
}).filter(Boolean).slice(0, 50);
// 9. Sections — find large visual blocks regardless of HTML tag
var sectionResults = [];
// Start with semantic elements, then fall back to large direct children of body/main
var candidates = Array.from(document.querySelectorAll(
'section, main > div, main > section, article, ' +
'body > div > div, body > main > div, body > div, ' +
'[class*="hero"], [class*="Hero"], [class*="section"], [class*="Section"], ' +
'[class*="container"], [class*="wrapper"], [class*="block"], ' +
'[id*="section"], [id*="hero"], footer, [role="region"], [role="banner"]'
));
// Deduplicate (a div can match multiple selectors)
var seenEls = new Set();
candidates = candidates.filter(function(el) {
if (seenEls.has(el)) return false;
seenEls.add(el);
return true;
});
for (var si = 0; si < candidates.length; si++) {
var el = candidates[si];
var rect = el.getBoundingClientRect();
if (rect.height < 200 || rect.width < 400 || !isVisible(el)) continue;
// Skip page-level wrappers (a single div wrapping the entire page is not a section)
var pageHeight = document.body.scrollHeight || document.documentElement.scrollHeight;
if (rect.height > pageHeight * 0.8) continue;
var y = rect.top + window.scrollY;
var heading = el.querySelector("h1, h2, h3, h4");
var headingText = heading ? (heading.innerText || heading.textContent || "").trim().replace(/\\s+/g, ' ').slice(0, 80) : "";
var classes = (el.className || "").toString().toLowerCase();
var type = "content";
if (y < 200 || classes.indexOf("hero") !== -1) type = "hero";
else if (el.tagName === "FOOTER" || classes.indexOf("footer") !== -1) type = "footer";
else if (classes.indexOf("cta") !== -1) type = "cta";
else if (classes.indexOf("logo") !== -1 || classes.indexOf("customer") !== -1) type = "logos";
else if (classes.indexOf("testimonial") !== -1 || classes.indexOf("quote") !== -1) type = "testimonials";
else if (classes.indexOf("feature") !== -1 || classes.indexOf("section") !== -1) type = "features";
var selector = el.id ? "#" + el.id : el.tagName.toLowerCase();
var sectionBg = getComputedStyle(el).backgroundColor;
// Walk up DOM to find nearest non-transparent background (don't default to white)
if (!sectionBg || sectionBg === "rgba(0, 0, 0, 0)" || sectionBg === "transparent") {
var bgWalker = el.parentElement;
while (bgWalker) {
var parentBg = getComputedStyle(bgWalker).backgroundColor;
if (parentBg && parentBg !== "rgba(0, 0, 0, 0)" && parentBg !== "transparent") {
sectionBg = parentBg;
break;
}
bgWalker = bgWalker.parentElement;
}
if (!sectionBg || sectionBg === "rgba(0, 0, 0, 0)" || sectionBg === "transparent") sectionBg = "#FFFFFF";
}
// Check for background-image when color is transparent/default white
var sectionBgImage = undefined;
var rawBgImg = getComputedStyle(el).backgroundImage;
if (rawBgImg && rawBgImg !== "none" && rawBgImg.indexOf("url(") !== -1) {
var start = rawBgImg.indexOf("url(") + 4;
var end = rawBgImg.indexOf(")", start);
if (end > start) {
sectionBgImage = rawBgImg.slice(start, end).replace(/['"]/g, "");
}
}
sectionBg = rgbToHex(sectionBg) || sectionBg;
// Inner content for faithful page-card recreation downstream: CTAs, body
// text, in-section media URLs (remote — joined to local paths in index.ts),
// and a coarse layout hint. Mirrors the prior capture framework's richer
// section model that the page-scroll-spotlight blueprint depends on.
var absUrl = function (u) {
try { return u ? new URL(u, location.href).href : ""; } catch (e) { return ""; }
};
var sectionText = (el.innerText || el.textContent || "").trim().replace(/\\s+/g, " ").slice(0, 600);
var sectionCtas = [];
var ctaNodes = el.querySelectorAll("a, button");
for (var qi = 0; qi < ctaNodes.length && sectionCtas.length < 8; qi++) {
if (!isVisible(ctaNodes[qi])) continue;
var ctaTxt = (ctaNodes[qi].textContent || "").trim().replace(/\\s+/g, " ").slice(0, 60);
if (ctaTxt && ctaTxt.length > 1 && sectionCtas.indexOf(ctaTxt) === -1) sectionCtas.push(ctaTxt);
}
var sectionAssets = [];
var mediaNodes = el.querySelectorAll("img, video, source");
for (var ii = 0; ii < mediaNodes.length && sectionAssets.length < 10; ii++) {
var mn = mediaNodes[ii];
var msrc = mn.currentSrc || mn.src || mn.getAttribute("src") || mn.getAttribute("data-src") || mn.getAttribute("poster") || "";
var mau = absUrl(msrc);
if (mau && !mau.startsWith("data:") && sectionAssets.indexOf(mau) === -1) sectionAssets.push(mau);
}
if (sectionBgImage) {
var bau = absUrl(sectionBgImage);
if (bau && sectionAssets.indexOf(bau) === -1) sectionAssets.unshift(bau);
}
var imgCount = el.querySelectorAll("img").length;
var layout = "stacked";
if (imgCount >= 3) layout = "grid";
else if (el.querySelector("img, video") && headingText) layout = "split";
else if (headingText && imgCount === 0) layout = "centered";
var sectionEntry = {
selector: selector, type: type,
x: Math.round(rect.left + window.scrollX), y: Math.round(y),
width: Math.round(rect.width), height: Math.round(rect.height),
heading: headingText, backgroundColor: sectionBg,
callsToAction: sectionCtas, text: sectionText, layout: layout, assetUrls: sectionAssets
};
if (sectionBgImage) sectionEntry.backgroundImage = sectionBgImage;
sectionResults.push(sectionEntry);
}
sectionResults.sort(function(a, b) { return a.y - b.y; });
var filtered = sectionResults.filter(function(s, i) { return i === 0 || Math.abs(s.y - sectionResults[i-1].y) > 100; });
// Filter cssVariables — keep only color-like values or design-relevant names
var colorValueRe = /^(#|rgb|hsl|oklch|oklab|lch|lab|color)/i;
var designNameRe = /(color|bg|background|border|text|font|radius|shadow)/i;
var filteredVars = {};
var varKeys = Object.keys(cssVariables);
for (var vi = 0; vi < varKeys.length; vi++) {
var varName = varKeys[vi];
var varVal = cssVariables[varName];
if (colorValueRe.test(varVal) || designNameRe.test(varName)) {
filteredVars[varName] = varVal;
}
}
// Filter sections — only keep those with a non-empty heading
var filteredSections = filtered.filter(function(s) { return s.heading && s.heading.length > 0; });
return {
title: title, description: description, ogImage: ogImage,
cssVariables: filteredVars, fonts: Object.keys(fontMap).map(function(k) { var f = fontMap[k]; f.weights.sort(function(a,b){return a-b;}); return f; }).filter(function(f) { return f.weights.length > 0 || f.variable; }).slice(0, 20), colors: Object.keys(colorSet).sort(function(a,b) { return colorSet[b] - colorSet[a]; }).slice(0, 20),
headings: headings, ctas: ctas,
svgs: svgs, sections: filteredSections,
colorStats: colorStatsArr,
page: { width: Math.round(document.documentElement.scrollWidth), height: Math.round(document.documentElement.scrollHeight), viewport: { width: window.innerWidth, height: window.innerHeight } }
};
})()`;
export async function extractTokens(page: Page): Promise<DesignTokens> {
return page.evaluate(EXTRACT_SCRIPT) as Promise<DesignTokens>;
}