webcake-storefront-mcp 2.2.0 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/builder/clone.js +638 -0
- package/dist/builder/element-schema.js +2 -2
- package/dist/changelog.json +7 -7
- package/dist/tools/builder-extras.js +5 -2
- package/dist/tools/builder.js +9 -1
- package/dist/tools/clone.js +123 -0
- package/dist/tools/families.js +3 -0
- package/dist/tools/registry.js +2 -0
- package/dist/tools/result-cache.js +8 -0
- package/package.json +3 -2
|
@@ -0,0 +1,638 @@
|
|
|
1
|
+
// Clone a rendered web page into native BuilderX sections.
|
|
2
|
+
//
|
|
3
|
+
// 1. captureInPage() runs INSIDE the source page (Playwright page.evaluate, or the agent's own
|
|
4
|
+
// browser via the script from get_clone_script): computed styles + real boxes, reduced to a
|
|
5
|
+
// small tree — section → group(row|col) / box → text | image | button | form | video | …
|
|
6
|
+
// Layout is read from geometry (who sits beside whom), not from flex/grid/float CSS, so it
|
|
7
|
+
// works the same for any framework.
|
|
8
|
+
// 2. snapshotToSource() maps that tree onto the builder's own layout helpers (buildSection,
|
|
9
|
+
// rowChildren/stackChildren via buildFromSpec): a row → `layout:"row"` container with
|
|
10
|
+
// column widths from the measured boxes (collapses on tablet/mobile), a column → stack.
|
|
11
|
+
// Nothing becomes custom code; what can't be mapped is counted in `notes`.
|
|
12
|
+
import { buildSection } from "./page.js";
|
|
13
|
+
// ── 1. In-page capture (self-contained: it is serialized with Function#toString) ──────────
|
|
14
|
+
export async function captureInPage(opts = {}) {
|
|
15
|
+
const arr = (x) => Array.from(x);
|
|
16
|
+
const MAX = opts.maxNodes || 2500;
|
|
17
|
+
const W = window.innerWidth;
|
|
18
|
+
let count = 0;
|
|
19
|
+
const skipped = {};
|
|
20
|
+
const skip = (r) => {
|
|
21
|
+
skipped[r] = (skipped[r] || 0) + 1;
|
|
22
|
+
};
|
|
23
|
+
// Lazy content: walk the page once so images/sections that load on scroll exist.
|
|
24
|
+
if (opts.scroll !== false) {
|
|
25
|
+
const step = Math.max(400, window.innerHeight * 0.8);
|
|
26
|
+
for (let y = 0; y < document.body.scrollHeight && y < 40000; y += step) {
|
|
27
|
+
window.scrollTo(0, y);
|
|
28
|
+
await new Promise((r) => setTimeout(r, 120));
|
|
29
|
+
}
|
|
30
|
+
window.scrollTo(0, 0);
|
|
31
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
32
|
+
}
|
|
33
|
+
document.querySelectorAll("details").forEach((d) => (d.open = true));
|
|
34
|
+
const cs = (el) => getComputedStyle(el);
|
|
35
|
+
const box = (el) => {
|
|
36
|
+
const r = el.getBoundingClientRect();
|
|
37
|
+
return { x: Math.round(r.left + window.scrollX), y: Math.round(r.top + window.scrollY), w: Math.round(r.width), h: Math.round(r.height) };
|
|
38
|
+
};
|
|
39
|
+
const visible = (el) => {
|
|
40
|
+
const s = cs(el);
|
|
41
|
+
if (s.display === "none" || s.visibility === "hidden" || Number(s.opacity) === 0)
|
|
42
|
+
return false;
|
|
43
|
+
const r = el.getBoundingClientRect();
|
|
44
|
+
return r.width > 1 && r.height > 1;
|
|
45
|
+
};
|
|
46
|
+
const clear = (c) => !c || c === "transparent" || /rgba\([^)]*,\s*0\)$/.test(c);
|
|
47
|
+
const abs = (u) => {
|
|
48
|
+
try {
|
|
49
|
+
return new URL(u, location.href).href;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return u;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const px = (v) => Math.round(parseFloat(v) || 0);
|
|
56
|
+
const bgUrl = (s) => {
|
|
57
|
+
const m = /url\(["']?([^"')]+)["']?\)/.exec(s.backgroundImage || "");
|
|
58
|
+
return m ? abs(m[1]) : undefined;
|
|
59
|
+
};
|
|
60
|
+
const textStyle = (s) => {
|
|
61
|
+
const o = { fontSize: px(s.fontSize), fontWeight: s.fontWeight, color: s.color, textAlign: s.textAlign };
|
|
62
|
+
if (s.lineHeight !== "normal")
|
|
63
|
+
o.lineHeight = +(parseFloat(s.lineHeight) / parseFloat(s.fontSize)).toFixed(2);
|
|
64
|
+
if (s.letterSpacing !== "normal")
|
|
65
|
+
o.letterSpacing = s.letterSpacing;
|
|
66
|
+
if (s.textTransform !== "none")
|
|
67
|
+
o.textTransform = s.textTransform;
|
|
68
|
+
if (s.fontStyle !== "normal")
|
|
69
|
+
o.fontStyle = s.fontStyle;
|
|
70
|
+
o.fontFamily = s.fontFamily.split(",")[0].replace(/["']/g, "").trim();
|
|
71
|
+
return o;
|
|
72
|
+
};
|
|
73
|
+
const boxStyle = (s) => {
|
|
74
|
+
const o = {};
|
|
75
|
+
if (!clear(s.backgroundColor))
|
|
76
|
+
o.bg = s.backgroundColor;
|
|
77
|
+
const u = bgUrl(s);
|
|
78
|
+
if (u)
|
|
79
|
+
o.bgImage = u;
|
|
80
|
+
if (/gradient\(/.test(s.backgroundImage))
|
|
81
|
+
o.gradient = s.backgroundImage.replace(/,?\s*url\([^)]*\)/g, "");
|
|
82
|
+
if (px(s.borderTopWidth) > 0 && s.borderTopStyle !== "none" && !clear(s.borderTopColor))
|
|
83
|
+
o.border = `${px(s.borderTopWidth)}px ${s.borderTopStyle} ${s.borderTopColor}`;
|
|
84
|
+
if (px(s.borderTopLeftRadius) > 0)
|
|
85
|
+
o.radius = px(s.borderTopLeftRadius);
|
|
86
|
+
if (s.boxShadow && s.boxShadow !== "none")
|
|
87
|
+
o.shadow = s.boxShadow;
|
|
88
|
+
return o;
|
|
89
|
+
};
|
|
90
|
+
const painted = (b) => !!(b.bg || b.bgImage || b.gradient || b.border || b.shadow);
|
|
91
|
+
/** Pinned widgets (chat bubble, cookie bar, back-to-top) — not a pinned full-width header. */
|
|
92
|
+
const overlay = (s, r) => s.position === "fixed" && r.h < window.innerHeight * 0.9 && !(r.w >= W * 0.9 && r.y - window.scrollY <= 8);
|
|
93
|
+
const pads = (s) => ({ t: px(s.paddingTop), r: px(s.paddingRight), b: px(s.paddingBottom), l: px(s.paddingLeft) });
|
|
94
|
+
const INLINE = new Set(["A", "SPAN", "STRONG", "B", "EM", "I", "U", "SMALL", "BR", "SUP", "SUB", "MARK", "CODE", "S", "DEL", "LABEL", "ABBR", "TIME", "FONT", "Q", "CITE"]);
|
|
95
|
+
// Text-only subtree laid out inline. A flex/grid row of spans, or a painted inline-block
|
|
96
|
+
// (a pill button inside a line), is layout, not text.
|
|
97
|
+
const textLeaf = (el) => {
|
|
98
|
+
if (/flex|grid/.test(cs(el).display) && el.children.length > 1)
|
|
99
|
+
return false;
|
|
100
|
+
const links = arr(el.children).filter((c) => c.tagName === "A");
|
|
101
|
+
if (links.length >= 2 && links.length === el.children.length && !arr(el.childNodes).some((n) => n.nodeType === 3 && /[^\s|·•/-]/.test(n.textContent || "")))
|
|
102
|
+
return false;
|
|
103
|
+
for (const c of arr(el.children)) {
|
|
104
|
+
const d = cs(c).display;
|
|
105
|
+
if (!INLINE.has(c.tagName) || /block|flex|grid|table/.test(d) && (d !== "inline-block" || painted(boxStyle(cs(c)))) || !textLeaf(c))
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
return (el.innerText || "").trim().length > 0;
|
|
109
|
+
};
|
|
110
|
+
const esc = (t) => t.replace(/[<>&]/g, (c) => ({ "<": "<", ">": ">", "&": "&" }[c]));
|
|
111
|
+
const richText = (el) => {
|
|
112
|
+
const walk = (n) => {
|
|
113
|
+
if (n.nodeType === 3)
|
|
114
|
+
return esc(n.textContent || "");
|
|
115
|
+
if (n.nodeType !== 1)
|
|
116
|
+
return "";
|
|
117
|
+
const e = n;
|
|
118
|
+
const inner = arr(e.childNodes).map(walk).join("");
|
|
119
|
+
const t = e.tagName;
|
|
120
|
+
if (t === "BR")
|
|
121
|
+
return "<br>";
|
|
122
|
+
if (t === "B" || t === "STRONG" || (t === "SPAN" && +cs(e).fontWeight >= 600 && +cs(el).fontWeight < 600))
|
|
123
|
+
return `<b>${inner}</b>`;
|
|
124
|
+
if (t === "I" || t === "EM")
|
|
125
|
+
return `<i>${inner}</i>`;
|
|
126
|
+
if (t === "U")
|
|
127
|
+
return `<u>${inner}</u>`;
|
|
128
|
+
if (t === "A" && e.getAttribute("href"))
|
|
129
|
+
return `<a href="${abs(e.getAttribute("href")).replace(/"/g, """)}">${inner}</a>`;
|
|
130
|
+
return inner;
|
|
131
|
+
};
|
|
132
|
+
return arr(el.childNodes).map(walk).join("").replace(/\s+/g, " ").trim();
|
|
133
|
+
};
|
|
134
|
+
const headingOf = (el) => {
|
|
135
|
+
if (/^H[1-6]$/.test(el.tagName))
|
|
136
|
+
return el.tagName.toLowerCase();
|
|
137
|
+
const h = el.closest("h1,h2,h3,h4,h5,h6");
|
|
138
|
+
return h ? h.tagName.toLowerCase() : "p";
|
|
139
|
+
};
|
|
140
|
+
const isButton = (el, s) => {
|
|
141
|
+
const t = (el.innerText || el.value || "").trim();
|
|
142
|
+
if (!t || t.length > 60 || t.includes("\n"))
|
|
143
|
+
return false;
|
|
144
|
+
if (el.tagName === "BUTTON" || el.getAttribute("role") === "button")
|
|
145
|
+
return true;
|
|
146
|
+
const b = boxStyle(s);
|
|
147
|
+
return el.tagName === "A" && (!!b.bg || !!b.gradient || !!b.border) && px(s.paddingLeft) + px(s.paddingRight) >= 12;
|
|
148
|
+
};
|
|
149
|
+
const form = (el, r, s) => {
|
|
150
|
+
const fields = [];
|
|
151
|
+
let submit = "";
|
|
152
|
+
let submitStyle;
|
|
153
|
+
for (const f of arr(el.querySelectorAll("input,select,textarea,button"))) {
|
|
154
|
+
if (!visible(f))
|
|
155
|
+
continue;
|
|
156
|
+
const type = String(f.type || "").toLowerCase();
|
|
157
|
+
if (f.tagName === "BUTTON" || type === "submit") {
|
|
158
|
+
if (!submit) {
|
|
159
|
+
submit = (f.innerText || f.value || "").trim();
|
|
160
|
+
const fs = cs(f);
|
|
161
|
+
submitStyle = { ...boxStyle(fs), ...textStyle(fs), h: box(f).h };
|
|
162
|
+
}
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
if (type === "hidden")
|
|
166
|
+
continue;
|
|
167
|
+
if (type === "checkbox" || type === "radio") {
|
|
168
|
+
skip(`form ${type}`);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
const lab = (f.id && document.querySelector(`label[for="${CSS.escape(f.id)}"]`)?.innerText) || f.closest("label")?.innerText || f.getAttribute("aria-label") || "";
|
|
172
|
+
fields.push({
|
|
173
|
+
tag: f.tagName.toLowerCase(), type, name: f.name || "", label: lab.trim(), placeholder: f.placeholder || "", required: !!f.required, h: box(f).h,
|
|
174
|
+
style: { ...boxStyle(cs(f)), color: cs(f).color, fontSize: px(cs(f).fontSize) },
|
|
175
|
+
...(f.tagName === "SELECT" ? { options: arr(f.options).map((o) => o.text).slice(0, 30) } : {}),
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return { k: "form", ...r, fields, submit: submit || "Submit", submitStyle, style: boxStyle(s) };
|
|
179
|
+
};
|
|
180
|
+
// Lay children out the way they SIT: bands of vertically overlapping boxes → rows.
|
|
181
|
+
const arrange = (kids, r) => {
|
|
182
|
+
const sorted = [...kids].sort((a, b) => a.y - b.y || a.x - b.x);
|
|
183
|
+
const bands = [];
|
|
184
|
+
for (const k of sorted) {
|
|
185
|
+
const band = bands[bands.length - 1];
|
|
186
|
+
const bottom = band ? Math.max(...band.map((b) => b.y + b.h)) : -1;
|
|
187
|
+
if (band && k.y < bottom - 4 && !band.some((b) => k.x < b.x + b.w - 4 && b.x < k.x + k.w - 4))
|
|
188
|
+
band.push(k);
|
|
189
|
+
else
|
|
190
|
+
bands.push([k]);
|
|
191
|
+
}
|
|
192
|
+
const rows = bands.map((b) => {
|
|
193
|
+
if (b.length === 1)
|
|
194
|
+
return b[0];
|
|
195
|
+
b.sort((p, q) => p.x - q.x);
|
|
196
|
+
const gaps = b.slice(1).map((k, i) => k.x - (b[i].x + b[i].w));
|
|
197
|
+
const x = Math.min(...b.map((k) => k.x));
|
|
198
|
+
const y = Math.min(...b.map((k) => k.y));
|
|
199
|
+
return { k: "group", dir: "row", gap: Math.max(0, Math.round(gaps.reduce((n, g) => n + g, 0) / gaps.length)), x, y, w: Math.max(...b.map((k) => k.x + k.w)) - x, h: Math.max(...b.map((k) => k.y + k.h)) - y, kids: b };
|
|
200
|
+
});
|
|
201
|
+
if (rows.length === 1)
|
|
202
|
+
return rows[0].k === "group" ? { ...rows[0], ...r } : rows[0];
|
|
203
|
+
const vg = rows.slice(1).map((k, i) => k.y - (rows[i].y + rows[i].h));
|
|
204
|
+
return { k: "group", dir: "col", gap: Math.max(0, Math.round(vg.reduce((n, g) => n + g, 0) / vg.length)), ...r, kids: rows };
|
|
205
|
+
};
|
|
206
|
+
const node = (el) => {
|
|
207
|
+
if (++count > MAX) {
|
|
208
|
+
skip("node limit");
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
const t = el.tagName.toUpperCase();
|
|
212
|
+
if (/^(SCRIPT|STYLE|NOSCRIPT|TEMPLATE|LINK|META|HEAD)$/.test(t) || !visible(el))
|
|
213
|
+
return null;
|
|
214
|
+
const s = cs(el);
|
|
215
|
+
const r = box(el);
|
|
216
|
+
if (overlay(s, r)) {
|
|
217
|
+
skip("fixed overlay (chat widget, cookie bar, sticky button)");
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
const he = el;
|
|
221
|
+
if (t === "IMG" || t === "PICTURE") {
|
|
222
|
+
const img = t === "IMG" ? el : el.querySelector("img");
|
|
223
|
+
const src = img && (img.currentSrc || img.src);
|
|
224
|
+
if (!src)
|
|
225
|
+
return (skip("image without src"), null);
|
|
226
|
+
return { k: "image", ...r, src: abs(src), alt: img.alt || "", radius: px(s.borderTopLeftRadius) || undefined, fit: s.objectFit };
|
|
227
|
+
}
|
|
228
|
+
if (t === "SVG") {
|
|
229
|
+
const html = el.outerHTML;
|
|
230
|
+
if (html.length > 12000)
|
|
231
|
+
return (skip("large svg"), null);
|
|
232
|
+
return { k: "icon", ...r, svg: html, color: s.color };
|
|
233
|
+
}
|
|
234
|
+
if (t === "VIDEO") {
|
|
235
|
+
const v = el;
|
|
236
|
+
const src = v.currentSrc || el.querySelector("source")?.src;
|
|
237
|
+
return src ? { k: "video", ...r, src: abs(src), poster: v.poster || undefined } : (skip("video without src"), null);
|
|
238
|
+
}
|
|
239
|
+
if (t === "IFRAME") {
|
|
240
|
+
const src = el.src || "";
|
|
241
|
+
const yt = /youtube(?:-nocookie)?\.com\/embed\/([\w-]{6,})|youtu\.be\/([\w-]{6,})/.exec(src);
|
|
242
|
+
if (yt)
|
|
243
|
+
return { k: "video", ...r, provider: "youtube", id: yt[1] || yt[2] };
|
|
244
|
+
if (/google\.[^/]+\/maps|maps\.google\./.test(src))
|
|
245
|
+
return { k: "map", ...r, src };
|
|
246
|
+
skip("iframe (third-party widget)");
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
if (t === "HR")
|
|
250
|
+
return { k: "line", ...r, color: s.borderTopColor };
|
|
251
|
+
if (t === "FORM")
|
|
252
|
+
return form(el, r, s);
|
|
253
|
+
if (/^(INPUT|SELECT|TEXTAREA)$/.test(t)) {
|
|
254
|
+
if (/^(submit|button)$/.test(el.type))
|
|
255
|
+
return { k: "button", ...r, text: el.value, style: { ...boxStyle(s), ...textStyle(s) } };
|
|
256
|
+
skip("field outside a form");
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
if ((t === "A" || t === "BUTTON" || el.getAttribute("role") === "button") && isButton(he, s)) {
|
|
260
|
+
const href = el.getAttribute("href");
|
|
261
|
+
return { k: "button", ...r, text: he.innerText.trim(), href: href ? abs(href) : undefined, style: { ...boxStyle(s), ...textStyle(s), padX: px(s.paddingLeft) } };
|
|
262
|
+
}
|
|
263
|
+
const b = boxStyle(s);
|
|
264
|
+
if (textLeaf(el)) {
|
|
265
|
+
const href = t === "A" ? el.getAttribute("href") : null;
|
|
266
|
+
// A whole-text link (menu item, footer link) → plain text + a click event, so it keeps
|
|
267
|
+
// its own colour (rich-text <a> is forced blue/underlined by the storefront's .ql-editor a).
|
|
268
|
+
const n = { k: "text", ...r, tag: headingOf(el), html: richText(el), text: he.innerText.trim(), href: href ? abs(href) : undefined, style: textStyle(s) };
|
|
269
|
+
return painted(b) ? { k: "box", ...r, style: b, pad: pads(s), content: n } : n;
|
|
270
|
+
}
|
|
271
|
+
const kids = [];
|
|
272
|
+
for (const c of arr(el.children)) {
|
|
273
|
+
const n = node(c);
|
|
274
|
+
if (n)
|
|
275
|
+
kids.push(n);
|
|
276
|
+
}
|
|
277
|
+
const loose = arr(el.childNodes).filter((n) => n.nodeType === 3 && (n.textContent || "").trim()).map((n) => (n.textContent || "").trim()).join(" ");
|
|
278
|
+
if (loose)
|
|
279
|
+
kids.unshift({ k: "text", ...r, h: Math.max(16, px(s.lineHeight) || px(s.fontSize) * 1.4), tag: "p", html: esc(loose), text: loose, style: textStyle(s) });
|
|
280
|
+
if (!kids.length)
|
|
281
|
+
return painted(b) && r.h > 3 ? { k: "box", ...r, style: b, pad: pads(s) } : null;
|
|
282
|
+
const content = kids.length === 1 ? kids[0] : arrange(kids, r);
|
|
283
|
+
return painted(b) ? { k: "box", ...r, style: b, pad: pads(s), content } : content;
|
|
284
|
+
};
|
|
285
|
+
// Sections: split the page into its vertical bands. A tall block whose children sit one
|
|
286
|
+
// under the other (app root, <main>, a page wrapper) is replaced by those children; a
|
|
287
|
+
// painted wrapper hands its background down to the bands that have none.
|
|
288
|
+
const inherited = new Map();
|
|
289
|
+
const stacked = (kids) => {
|
|
290
|
+
const rs = kids.map((k) => k.getBoundingClientRect()).sort((a, b) => a.top - b.top);
|
|
291
|
+
return rs.every((r, i) => i === 0 || r.top >= rs[i - 1].bottom - 4);
|
|
292
|
+
};
|
|
293
|
+
// A zero-height wrapper (often around a fixed header) is see-through: take its children.
|
|
294
|
+
const expand = (k, d = 0) => {
|
|
295
|
+
const h = k.getBoundingClientRect().height;
|
|
296
|
+
if (h >= 8)
|
|
297
|
+
return visible(k) ? [k] : [];
|
|
298
|
+
return d < 4 && cs(k).display !== "none" ? arr(k.children).flatMap((c) => expand(c, d + 1)) : [];
|
|
299
|
+
};
|
|
300
|
+
let blocks = arr(document.body.children).flatMap((k) => expand(k));
|
|
301
|
+
for (let i = 0; i < 12; i++) {
|
|
302
|
+
let changed = false;
|
|
303
|
+
const next = [];
|
|
304
|
+
for (const b of blocks) {
|
|
305
|
+
const rb = b.getBoundingClientRect();
|
|
306
|
+
// Rails: empty decorative strips (gutter lines, borders) — not content, not layout.
|
|
307
|
+
const rail = (k) => !(k.innerText || "").trim() && !k.querySelector("img,svg,video,iframe,picture") && k.getBoundingClientRect().width < rb.width * 0.1;
|
|
308
|
+
const kids = arr(b.children).flatMap((k) => expand(k)).filter((k) => !rail(k));
|
|
309
|
+
const bs = boxStyle(cs(b));
|
|
310
|
+
const leaf = /^(IMG|PICTURE|SVG|VIDEO|IFRAME|FORM|HR)$/i.test(b.tagName) || textLeaf(b);
|
|
311
|
+
// Fixed bars (a pinned header) overlap the flow: they are their own band, not layout.
|
|
312
|
+
const flow = kids.filter((k) => cs(k).position !== "fixed");
|
|
313
|
+
const wrapper = !leaf && (kids.length === 1 ? !painted(bs) || rb.height > window.innerHeight * 1.5 : kids.length > 1 && rb.height > window.innerHeight * 1.2 && stacked(flow));
|
|
314
|
+
if (wrapper) {
|
|
315
|
+
const bg = painted(bs) ? bs : inherited.get(b);
|
|
316
|
+
for (const k of kids)
|
|
317
|
+
if (bg && !inherited.has(k))
|
|
318
|
+
inherited.set(k, bg);
|
|
319
|
+
next.push(...kids);
|
|
320
|
+
changed = true;
|
|
321
|
+
}
|
|
322
|
+
else
|
|
323
|
+
next.push(b);
|
|
324
|
+
}
|
|
325
|
+
blocks = next;
|
|
326
|
+
if (!changed)
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
const sections = [];
|
|
330
|
+
for (const b of blocks) {
|
|
331
|
+
const s = cs(b);
|
|
332
|
+
const r = box(b);
|
|
333
|
+
if (r.h < 8)
|
|
334
|
+
continue;
|
|
335
|
+
if (overlay(s, r)) {
|
|
336
|
+
skip("fixed overlay (chat widget, cookie bar, sticky button)");
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const kids = [];
|
|
340
|
+
const self = /^(IMG|PICTURE|SVG|VIDEO|IFRAME|FORM|HR)$/i.test(b.tagName) || textLeaf(b);
|
|
341
|
+
if (self)
|
|
342
|
+
kids.push(node(b));
|
|
343
|
+
else
|
|
344
|
+
for (const c of arr(b.children)) {
|
|
345
|
+
const n = node(c);
|
|
346
|
+
if (n)
|
|
347
|
+
kids.push(n);
|
|
348
|
+
}
|
|
349
|
+
const role = b.tagName === "HEADER" || b.querySelector(":scope > nav, header") || (s.position === "fixed" && r.y <= 8) ? "header" : b.tagName === "FOOTER" ? "footer" : b.tagName === "NAV" ? "nav" : undefined;
|
|
350
|
+
const content = kids.filter(Boolean);
|
|
351
|
+
if (!content.length && !painted(boxStyle(s)))
|
|
352
|
+
continue;
|
|
353
|
+
const own = self ? {} : boxStyle(s);
|
|
354
|
+
const style = own.bg || own.bgImage || own.gradient ? own : { ...(inherited.get(b) || {}), ...own };
|
|
355
|
+
sections.push({ k: "section", ...r, role, style, pad: pads(s), sticky: s.position === "sticky" || s.position === "fixed" || undefined, content: content.length === 1 ? content[0] : content.length ? arrange(content, r) : null });
|
|
356
|
+
}
|
|
357
|
+
// Thin bands with the same background as their neighbour (a section title above its grid)
|
|
358
|
+
// belong to one section.
|
|
359
|
+
const merged = [];
|
|
360
|
+
for (const s of sections) {
|
|
361
|
+
const prev = merged[merged.length - 1];
|
|
362
|
+
if (prev && !prev.role && !s.role && JSON.stringify(prev.style) === JSON.stringify(s.style) && (s.h < 240 || prev.h < 240) && s.content && prev.content) {
|
|
363
|
+
const kids = [...(prev.content.k === "group" && prev.content.dir === "col" ? prev.content.kids : [prev.content]), s.content];
|
|
364
|
+
const y = prev.y;
|
|
365
|
+
const h = s.y + s.h - prev.y;
|
|
366
|
+
Object.assign(prev, { h, w: Math.max(prev.w, s.w), content: { k: "group", dir: "col", gap: Math.max(0, s.y - (prev.y + prev.h)) || 16, x: Math.min(prev.x, s.x), y, w: Math.max(prev.w, s.w), h, kids } });
|
|
367
|
+
}
|
|
368
|
+
else
|
|
369
|
+
merged.push(s);
|
|
370
|
+
}
|
|
371
|
+
sections.length = 0;
|
|
372
|
+
sections.push(...merged);
|
|
373
|
+
const textTotal = (document.body.innerText || "").replace(/\s+/g, "").length;
|
|
374
|
+
// The page colour: body/html, else the colour most of the page's height is painted with
|
|
375
|
+
// (an app root wrapper), so translucent bars and unpainted bands sit on the right colour.
|
|
376
|
+
const weight = {};
|
|
377
|
+
for (const s of sections)
|
|
378
|
+
if (s.style?.bg && !/rgba\([^)]*,\s*0?\.\d+\)$/.test(s.style.bg))
|
|
379
|
+
weight[s.style.bg] = (weight[s.style.bg] || 0) + s.h;
|
|
380
|
+
const dominant = Object.entries(weight).sort((a, b) => b[1] - a[1])[0];
|
|
381
|
+
const pageBg = [document.body, document.documentElement].map((e) => cs(e).backgroundColor).find((c) => !clear(c))
|
|
382
|
+
|| (dominant && dominant[1] > sections.reduce((n, s) => n + s.h, 0) * 0.5 ? dominant[0] : "rgb(255, 255, 255)");
|
|
383
|
+
return { url: location.href, title: document.title, viewport: { w: W, h: window.innerHeight }, pageBg, sections, skipped, textTotal };
|
|
384
|
+
}
|
|
385
|
+
/** The capture as a script an agent can run in any browser tool (evaluate_script / javascript). */
|
|
386
|
+
export const CLONE_SCRIPT = `(${captureInPage.toString()})({})`;
|
|
387
|
+
const note = (c, k) => (c.notes[k] = (c.notes[k] || 0) + 1);
|
|
388
|
+
/** rgba(…, a<1) can't be shown over the storefront's white page: lay it over `base` instead. */
|
|
389
|
+
const over = (color, base) => (base && /rgba\([^)]*,\s*0?\.\d+\)$/.test(color) ? `linear-gradient(${color}, ${color}), ${base}` : color);
|
|
390
|
+
const bgStyle = (st = {}, base) => {
|
|
391
|
+
const o = {};
|
|
392
|
+
if (st.bgImage) {
|
|
393
|
+
// The colour stays under the image: if the image can't load the text keeps its contrast.
|
|
394
|
+
o.background = `${st.gradient ? `${st.gradient}, ` : ""}url(${st.bgImage})${st.bg ? ` ${st.bg}` : ""}`;
|
|
395
|
+
o["background-size"] = "cover";
|
|
396
|
+
o["background-position-x"] = "center";
|
|
397
|
+
o["background-position-y"] = "center";
|
|
398
|
+
o["background-repeat"] = "no-repeat";
|
|
399
|
+
}
|
|
400
|
+
else if (st.gradient)
|
|
401
|
+
o.background = base ? `${st.gradient}, ${base}` : st.gradient;
|
|
402
|
+
else if (st.bg)
|
|
403
|
+
o.background = over(st.bg, base);
|
|
404
|
+
// Split keys: element CSS reads borderWidth/borderStyle/borderColor (real pages use them).
|
|
405
|
+
const b = st.border && /^(\d+)px (\w+) (.+)$/.exec(st.border);
|
|
406
|
+
if (b)
|
|
407
|
+
Object.assign(o, { borderWidth: `${b[1]}px`, borderStyle: b[2], borderColor: b[3] });
|
|
408
|
+
if (st.radius)
|
|
409
|
+
o.borderRadius = `${st.radius}px`;
|
|
410
|
+
if (st.shadow)
|
|
411
|
+
o.boxShadow = st.shadow;
|
|
412
|
+
return o;
|
|
413
|
+
};
|
|
414
|
+
const fontStyle = (c, st = {}) => {
|
|
415
|
+
const o = {};
|
|
416
|
+
if (st.fontSize)
|
|
417
|
+
o.fontSize = `${st.fontSize}px`;
|
|
418
|
+
if (st.fontWeight && st.fontWeight !== "400")
|
|
419
|
+
o.fontWeight = String(st.fontWeight);
|
|
420
|
+
if (st.color)
|
|
421
|
+
o.color = st.color;
|
|
422
|
+
if (st.lineHeight)
|
|
423
|
+
o.lineHeight = String(st.lineHeight);
|
|
424
|
+
if (st.textAlign && st.textAlign !== "start")
|
|
425
|
+
o.textAlign = st.textAlign === "end" ? "right" : st.textAlign;
|
|
426
|
+
if (st.letterSpacing)
|
|
427
|
+
o.letterSpacing = st.letterSpacing;
|
|
428
|
+
if (st.textTransform)
|
|
429
|
+
o.textTransform = st.textTransform;
|
|
430
|
+
if (st.fontStyle)
|
|
431
|
+
o.fontStyle = st.fontStyle;
|
|
432
|
+
if (st.fontFamily)
|
|
433
|
+
c.fonts[st.fontFamily] = (c.fonts[st.fontFamily] || 0) + 1;
|
|
434
|
+
return o;
|
|
435
|
+
};
|
|
436
|
+
const painted = (st = {}) => !!(st.bg || st.bgImage || st.gradient || st.border || st.shadow);
|
|
437
|
+
const link = (href) => {
|
|
438
|
+
if (!href || /^javascript:/i.test(href))
|
|
439
|
+
return undefined;
|
|
440
|
+
if (/^tel:/i.test(href))
|
|
441
|
+
return [{ action: "phone_call", trigger: "click", phone_call_number: href.slice(4) }];
|
|
442
|
+
return [{ action: "open_link", trigger: "click", link_target: href, link_target_url: "_self" }];
|
|
443
|
+
};
|
|
444
|
+
const FIELD = { email: "email", tel: "phone-number", textarea: "text-area", select: "select", date: "input-date", number: "input-number" };
|
|
445
|
+
const inner = (n) => ({ x: n.x + (n.pad?.l || 0), w: Math.max(1, n.w - (n.pad?.l || 0) - (n.pad?.r || 0)) });
|
|
446
|
+
/** Where a box sits inside its area: fill | center | right | left. */
|
|
447
|
+
function alignIn(n, p) {
|
|
448
|
+
if (!p || !n.w)
|
|
449
|
+
return undefined;
|
|
450
|
+
if (n.w >= p.w * 0.92)
|
|
451
|
+
return "fill";
|
|
452
|
+
const off = n.x + n.w / 2 - (p.x + p.w / 2);
|
|
453
|
+
if (Math.abs(off) <= p.w * 0.06)
|
|
454
|
+
return "center";
|
|
455
|
+
if (n.x + n.w >= p.x + p.w * 0.94 && n.x - p.x > p.w * 0.2)
|
|
456
|
+
return "right";
|
|
457
|
+
return "left";
|
|
458
|
+
}
|
|
459
|
+
/** A narrower-than-its-area block keeps its share of the width. */
|
|
460
|
+
const share = (n, p) => (p && n.w && n.w < p.w * 0.92 ? { widthUnit: "%", relWidth: Math.max(5, Math.round((n.w / p.w) * 100)) } : undefined);
|
|
461
|
+
function spec(n, c, area) {
|
|
462
|
+
if (!n)
|
|
463
|
+
return null;
|
|
464
|
+
switch (n.k) {
|
|
465
|
+
case "text":
|
|
466
|
+
c.text += String(n.text || "").replace(/\s+/g, "").length;
|
|
467
|
+
const style = { ...fontStyle(c, n.style), height: n.h };
|
|
468
|
+
const at = area && n.w < area.w * 0.7 ? alignIn(n, area) : undefined;
|
|
469
|
+
// A short label on one line in the source stays on one line (fonts differ in width).
|
|
470
|
+
const oneLine = n.style?.fontSize && n.h <= n.style.fontSize * (n.style.lineHeight || 1.4) * 1.3;
|
|
471
|
+
if (oneLine && String(n.text || "").length <= 40)
|
|
472
|
+
style.whiteSpace = "nowrap";
|
|
473
|
+
if ((at === "right" || at === "center") && (!style.textAlign || style.textAlign === "left"))
|
|
474
|
+
style.textAlign = at;
|
|
475
|
+
if (/<a href/.test(n.html || ""))
|
|
476
|
+
note(c, "links inside a sentence keep the storefront's link style (blue, underlined)");
|
|
477
|
+
const events = link(n.href);
|
|
478
|
+
if (events)
|
|
479
|
+
style.cursor = "pointer";
|
|
480
|
+
return { type: "text", opts: { text: n.html || n.text, specials: { tag: n.tag || "p" }, style, ...(events ? { events } : {}) } };
|
|
481
|
+
case "image":
|
|
482
|
+
c.images.push(n.src);
|
|
483
|
+
return { type: "image", opts: { src: n.src, width: n.w, height: n.h, align: alignIn(n, area), config: share(n, area), specials: n.alt ? { image_alt: n.alt } : undefined, style: { ...(n.radius ? { borderRadius: `${n.radius}px` } : {}), ...(n.fit && n.fit !== "fill" ? { objectFit: n.fit } : {}) } } };
|
|
484
|
+
case "icon": {
|
|
485
|
+
note(c, "svg icon → image (data URI; rehost_images uploads it as PNG)");
|
|
486
|
+
const svg = /xmlns=/.test(n.svg) ? n.svg : n.svg.replace("<svg", '<svg xmlns="http://www.w3.org/2000/svg"');
|
|
487
|
+
const src = `data:image/svg+xml;base64,${Buffer.from(svg.replace(/currentColor/g, n.color || "#000")).toString("base64")}`;
|
|
488
|
+
c.images.push(src);
|
|
489
|
+
return { type: "image", opts: { src, width: n.w, height: n.h, align: alignIn(n, area) ?? "left", config: share(n, area) } };
|
|
490
|
+
}
|
|
491
|
+
case "button": {
|
|
492
|
+
c.text += String(n.text || "").replace(/\s+/g, "").length;
|
|
493
|
+
const st = n.style || {};
|
|
494
|
+
const events = link(n.href);
|
|
495
|
+
if (n.href && /#/.test(n.href) && n.href.split("#")[0] === "")
|
|
496
|
+
note(c, "anchor link kept as a URL — retarget it with scroll_to");
|
|
497
|
+
return {
|
|
498
|
+
type: "button",
|
|
499
|
+
opts: {
|
|
500
|
+
text: n.text,
|
|
501
|
+
style: { ...bgStyle(st, c.pageBg), ...fontStyle(c, st), height: n.h, ...(String(n.text || "").length <= 40 ? { whiteSpace: "nowrap" } : {}), ...(st.padX ? { paddingLeft: `${st.padX}px`, paddingRight: `${st.padX}px` } : {}) },
|
|
502
|
+
...(events ? { events } : {}),
|
|
503
|
+
align: alignIn(n, area) ?? "left",
|
|
504
|
+
},
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
case "line":
|
|
508
|
+
return { type: "line", opts: { style: { borderColor: n.color } } };
|
|
509
|
+
case "video":
|
|
510
|
+
const place = { align: alignIn(n, area), config: share(n, area), style: { height: n.h } };
|
|
511
|
+
if (n.provider === "youtube")
|
|
512
|
+
return { type: "video", opts: { ...place, specials: { type: "youtube", id: n.id, src: `https://www.youtube.com/watch?v=${n.id}` } } };
|
|
513
|
+
return { type: "video", opts: { ...place, src: n.src, thumbnail: n.poster } };
|
|
514
|
+
case "map":
|
|
515
|
+
note(c, "google map: embed iframe kept — set the address in the googlemap element if it differs");
|
|
516
|
+
return { type: "googlemap", opts: { iframe: `<iframe src="${n.src}" width="100%" height="${n.h}" style="border:0" loading="lazy"></iframe>`, width: n.w, height: n.h } };
|
|
517
|
+
case "form": {
|
|
518
|
+
note(c, "form rebuilt natively as a subscribe (lead) form — check its type and field names before going live");
|
|
519
|
+
const children = (n.fields || []).map((f) => {
|
|
520
|
+
const type = FIELD[f.tag === "input" ? f.type : f.tag] || "input";
|
|
521
|
+
const label = f.label || f.placeholder || f.name;
|
|
522
|
+
const fs = f.style || {};
|
|
523
|
+
return { type, opts: { specials: { field_name: f.name || label, label, placeholder: f.placeholder || label, required: !!f.required, show_label: !!f.label }, style: { height: f.h } } };
|
|
524
|
+
});
|
|
525
|
+
const sst = n.submitStyle || {};
|
|
526
|
+
children.push({ type: "submit-button", opts: { specials: { text: n.submit }, style: { ...bgStyle(sst), ...fontStyle(c, sst), ...(sst.h ? { height: sst.h } : {}) } } });
|
|
527
|
+
c.text += String(n.submit || "").length;
|
|
528
|
+
// A lead/contact/newsletter form = "subscribe" (customer_data is a logged-in profile form,
|
|
529
|
+
// hidden for visitors; form_order is a checkout).
|
|
530
|
+
// The renderer styles every field from the FORM's style (+ config.backgroundInput), so the
|
|
531
|
+
// first field's frame goes on the form.
|
|
532
|
+
const f0 = (n.fields || [])[0]?.style || {};
|
|
533
|
+
const { background, ...frame } = bgStyle(f0);
|
|
534
|
+
if (painted(n.style))
|
|
535
|
+
note(c, "form box background/border dropped (the form's style styles its fields)");
|
|
536
|
+
return {
|
|
537
|
+
type: "form",
|
|
538
|
+
opts: { specials: { type: "subscribe" }, style: { ...frame, ...(f0.color ? { color: f0.color } : {}), ...(f0.fontSize ? { fontSize: `${f0.fontSize}px` } : {}) }, ...(background ? { config: { backgroundInput: background } } : {}) },
|
|
539
|
+
children, rowGap: 12,
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
case "box": {
|
|
543
|
+
const inside = n.content ? spec(n.content, c, inner(n)) : null;
|
|
544
|
+
const pad = n.pad || {};
|
|
545
|
+
const style = { ...bgStyle(n.style), ...(pad.t || pad.l ? { padding: `${pad.t}px ${pad.r}px ${pad.b}px ${pad.l}px` } : {}), height: n.h };
|
|
546
|
+
if (!inside)
|
|
547
|
+
return { type: "rectangle", opts: { style: { ...bgStyle(n.style), height: n.h, width: n.w } } };
|
|
548
|
+
// A painted wrapper around one group → the group's own container gets the paint.
|
|
549
|
+
const own = { align: alignIn(n, area), config: share(n, area) };
|
|
550
|
+
if (inside.type === "container" && !inside.opts?.style?.background)
|
|
551
|
+
return { ...inside, opts: { ...(inside.opts || {}), ...own, style: { ...(inside.opts?.style || {}), ...style } } };
|
|
552
|
+
return { type: "container", opts: { style, ...own }, children: [inside] };
|
|
553
|
+
}
|
|
554
|
+
case "group": {
|
|
555
|
+
if (n.dir === "row") {
|
|
556
|
+
// Each column runs from its child's left edge to the next child's (the last one to the
|
|
557
|
+
// row's right edge), so space-between / offsets survive; the child is aligned inside it.
|
|
558
|
+
const ks = [...(n.kids || [])].sort((a, b) => a.x - b.x);
|
|
559
|
+
const gap = Math.max(0, Math.min(n.gap ?? 24, 120));
|
|
560
|
+
const left = ks[0].x;
|
|
561
|
+
const right = Math.max(...ks.map((k) => k.x + k.w));
|
|
562
|
+
const cols = ks.map((k, i) => {
|
|
563
|
+
const start = i === 0 ? left : k.x;
|
|
564
|
+
const end = i < ks.length - 1 ? ks[i + 1].x - gap : right;
|
|
565
|
+
return { x: start, w: Math.max(1, end - start) };
|
|
566
|
+
});
|
|
567
|
+
const kids = ks.map((k, i) => spec(k, c, cols[i]));
|
|
568
|
+
const keep = kids.map((k, i) => [k, cols[i]]).filter(([k]) => k);
|
|
569
|
+
if (!keep.length)
|
|
570
|
+
return null;
|
|
571
|
+
if (keep.length === 1)
|
|
572
|
+
return keep[0][0];
|
|
573
|
+
const total = keep.reduce((s, [, a]) => s + a.w, 0) || 1;
|
|
574
|
+
const colWidths = keep.map(([, a]) => ({ unit: "fr", value: Math.max(1, Math.round((a.w / total) * 24)) }));
|
|
575
|
+
const span = { x: left, w: right - left };
|
|
576
|
+
// A row of small pieces (menu links, icons, footer bits) stays a row on phones; real
|
|
577
|
+
// columns (cards, split hero) collapse 2 → 1 as usual.
|
|
578
|
+
const small = ks.every((k) => ["text", "button", "icon", "image"].includes(k.k) && k.w <= 240) && (span.w <= 900 || ks.length <= 4);
|
|
579
|
+
const n2 = keep.length;
|
|
580
|
+
return { type: "container", layout: "row", columnGap: gap, colWidths, ...(small ? { collapse: { bp3: n2, bp4: n2 } } : {}), children: keep.map(([k]) => k), opts: { align: alignIn(span, area), config: share(span, area) } };
|
|
581
|
+
}
|
|
582
|
+
const box = { x: n.x, w: n.w };
|
|
583
|
+
const kids = (n.kids || []).map((k) => spec(k, c, box)).filter(Boolean);
|
|
584
|
+
if (!kids.length)
|
|
585
|
+
return null;
|
|
586
|
+
if (kids.length === 1)
|
|
587
|
+
return kids[0];
|
|
588
|
+
return { type: "container", rowGap: Math.min(n.gap ?? 16, 160), children: kids };
|
|
589
|
+
}
|
|
590
|
+
default:
|
|
591
|
+
note(c, `unmapped ${n.k}`);
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
/** A captured page → { sections } built with the builder's own layout helpers. */
|
|
596
|
+
export function snapshotToSource(snap, opts = {}) {
|
|
597
|
+
const c = { pageBg: snap?.pageBg, notes: {}, text: 0, fonts: {}, images: [], W: snap?.viewport?.w || 1440 };
|
|
598
|
+
const white = (col) => !col || /^rgba?\(255, 255, 255(, 1)?\)$/.test(col);
|
|
599
|
+
const sections = [];
|
|
600
|
+
const roles = [];
|
|
601
|
+
for (const s of snap?.sections || []) {
|
|
602
|
+
if (opts.skipChrome && (s.role === "header" || s.role === "footer" || s.role === "nav")) {
|
|
603
|
+
note(c, `${s.role} skipped (the site's own global header/footer is used)`);
|
|
604
|
+
continue;
|
|
605
|
+
}
|
|
606
|
+
const content = s.content;
|
|
607
|
+
// The section's children: a column group stacks; anything else is one child.
|
|
608
|
+
const childNodes = content?.k === "group" && content.dir === "col" ? content.kids : content ? [content] : [];
|
|
609
|
+
const children = childNodes.map((n) => spec(n, c, inner(s))).filter(Boolean);
|
|
610
|
+
const pad = s.pad || {};
|
|
611
|
+
const firstY = childNodes.length ? Math.min(...childNodes.map((n) => n.y)) : s.y;
|
|
612
|
+
const lastY = childNodes.length ? Math.max(...childNodes.map((n) => n.y + n.h)) : s.y + s.h;
|
|
613
|
+
const padY = Math.max(0, Math.min(160, Math.round(((firstY - s.y) + (s.y + s.h - lastY)) / 2) || pad.t || 0));
|
|
614
|
+
// A section with no paint of its own shows the source page's background (dark sites).
|
|
615
|
+
const st = s.style?.bg || s.style?.bgImage || s.style?.gradient || white(c.pageBg) ? s.style : { ...s.style, bg: c.pageBg };
|
|
616
|
+
const section = buildSection(children, {
|
|
617
|
+
style: bgStyle(st, white(c.pageBg) ? undefined : c.pageBg),
|
|
618
|
+
padY,
|
|
619
|
+
rowGap: content?.k === "group" && content.dir === "col" ? Math.min(content.gap ?? 16, 160) : undefined,
|
|
620
|
+
});
|
|
621
|
+
if (s.sticky)
|
|
622
|
+
note(c, "sticky/fixed header captured as a normal section — set it sticky in the editor if needed");
|
|
623
|
+
sections.push(section);
|
|
624
|
+
roles.push(s.role || "content");
|
|
625
|
+
}
|
|
626
|
+
for (const [k, n] of Object.entries(snap?.skipped || {}))
|
|
627
|
+
c.notes[`skipped: ${k}`] = n;
|
|
628
|
+
const coverage = snap?.textTotal ? Math.min(100, Math.round((c.text / snap.textTotal) * 100)) : undefined;
|
|
629
|
+
const fonts = Object.entries(c.fonts).sort((a, b) => b[1] - a[1]).map(([f]) => f).slice(0, 4);
|
|
630
|
+
return {
|
|
631
|
+
source: { sections },
|
|
632
|
+
roles,
|
|
633
|
+
stats: { sections: sections.length, text_coverage_pct: coverage, images: c.images.length },
|
|
634
|
+
fonts,
|
|
635
|
+
images: c.images,
|
|
636
|
+
notes: Object.entries(c.notes).map(([k, n]) => `${k}${n > 1 ? ` ×${n}` : ""}`),
|
|
637
|
+
};
|
|
638
|
+
}
|
|
@@ -243,8 +243,8 @@ export function checkElementKeys(node) {
|
|
|
243
243
|
if (!obj || typeof obj !== "object" || Array.isArray(obj))
|
|
244
244
|
return;
|
|
245
245
|
for (const [k, v] of Object.entries(obj)) {
|
|
246
|
-
if (NOISE.has(k))
|
|
247
|
-
continue;
|
|
246
|
+
if (NOISE.has(k) || k.startsWith("__"))
|
|
247
|
+
continue; // __row/__cell: layout markers finalizeForRender consumes
|
|
248
248
|
const at = `${where}.${k}`;
|
|
249
249
|
if (k in specs) {
|
|
250
250
|
checkValue(specs[k], v, { ...base, at }, out);
|
package/dist/changelog.json
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
[
|
|
2
|
+
{
|
|
3
|
+
"v": "2.3.0",
|
|
4
|
+
"d": "19/11/2026",
|
|
5
|
+
"type": "Added",
|
|
6
|
+
"en": "New clone_page tool (family clone) turns a rendered web page into native BuilderX sections — text, image, button, form, video, and container rows —…",
|
|
7
|
+
"vi": "Tool mới clone_page (nhóm clone) chuyển một trang web đã render thành các section BuilderX native — text, image, button, form, video, và container…"
|
|
8
|
+
},
|
|
2
9
|
{
|
|
3
10
|
"v": "2.2.0",
|
|
4
11
|
"d": "19/11/2026",
|
|
@@ -33,12 +40,5 @@
|
|
|
33
40
|
"type": "Changed",
|
|
34
41
|
"en": "When a session token expires in stdio mode, multiple IDE windows/processes on the same machine now coordinate through a shared lock so only one…",
|
|
35
42
|
"vi": "Khi token phiên hết hạn ở chế độ stdio, nhiều cửa sổ/process IDE trên cùng một máy nay phối hợp qua một khóa dùng chung để chỉ một process mở trang…"
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
"v": "1.40.0",
|
|
39
|
-
"d": "19/11/2026",
|
|
40
|
-
"type": "Added",
|
|
41
|
-
"en": "The stdio server now reopens the browser Connect page automatically when the session token expires or the backend rejects it with a 401, then…",
|
|
42
|
-
"vi": "Server stdio nay tự mở lại trang Connect trên trình duyệt khi token phiên hết hạn hoặc bị backend từ chối với lỗi 401, rồi tự chạy lại lệnh gọi tool…"
|
|
43
43
|
}
|
|
44
44
|
]
|
|
@@ -158,6 +158,9 @@ async function toAllowedImage(buf, contentType) {
|
|
|
158
158
|
if (ALLOWED_IMG.test(contentType))
|
|
159
159
|
return { buf, contentType };
|
|
160
160
|
const sharp = (await import("sharp")).default;
|
|
161
|
+
// Vector icons keep their transparency (PNG at 2x); everything else → JPEG.
|
|
162
|
+
if (/svg/.test(contentType))
|
|
163
|
+
return { buf: await sharp(buf, { density: 192 }).png().toBuffer(), contentType: "image/png" };
|
|
161
164
|
const out = await sharp(buf).jpeg({ quality: 85 }).toBuffer();
|
|
162
165
|
return { buf: out, contentType: "image/jpeg" };
|
|
163
166
|
}
|
|
@@ -165,7 +168,7 @@ async function toAllowedImage(buf, contentType) {
|
|
|
165
168
|
* per-site cache so the same source is never uploaded twice. Returns the hosted URL.
|
|
166
169
|
* The storefront only renders whitelisted (WebCake CDN) image domains, so every image
|
|
167
170
|
* used on a page/product MUST go through this — external URLs (Pexels, etc.) won't show. */
|
|
168
|
-
async function cdnUpload(api, source) {
|
|
171
|
+
export async function cdnUpload(api, source) {
|
|
169
172
|
const cached = await getCachedUpload(api.siteId, source);
|
|
170
173
|
if (cached)
|
|
171
174
|
return cached;
|
|
@@ -461,7 +464,7 @@ RENDER_SCREENSHOT_BASE to avoid that).`, {
|
|
|
461
464
|
hint: "Rebuild this as BuilderX sections: map each heading group + its text/image/button into a new_section call. Generate fresh copy where useful; re-host external images with upload_images if you want them on the site CDN. This is a structural blueprint, not a 1:1 clone.",
|
|
462
465
|
};
|
|
463
466
|
}
|
|
464
|
-
server.tool("ingest_html", "
|
|
467
|
+
server.tool("ingest_html", "For a faithful copy of a live page use clone_page (native sections from the rendered layout). This one: parse reference HTML (e.g. a Google Stitch / Figma / Codex export) into a structural blueprint (title, headings, paragraphs, images, buttons, colour palette) you can rebuild as native BuilderX sections with new_section. Not a 1:1 clone. Rebuild every part native elements can do; use an embed only for a piece no element can (third-party widget, custom animation) — never paste the whole HTML into one (sandboxed iframe: no SEO, broken links, dead forms; rejected on save).", {
|
|
465
468
|
html: z.string().describe("Raw HTML to analyse"),
|
|
466
469
|
}, ({ html }) => handle(async () => blueprintFromHtml(html)));
|
|
467
470
|
server.tool("ingest_url", "Fetch a public URL and parse it into a structural blueprint (see ingest_html). Note: client-rendered (React/Vue) pages may return little content.", {
|
package/dist/tools/builder.js
CHANGED
|
@@ -5,6 +5,7 @@ import { PAGE_CODE_BUDGET, codeTargets, lintPageCode, mergePageCode } from "../b
|
|
|
5
5
|
import { elementSchema, suggestType, keyReference } from "../builder/element-schema.js";
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import { BUILD_GUIDE } from "../builder/guide.js";
|
|
8
|
+
import { getCached } from "./result-cache.js";
|
|
8
9
|
import { PAGE_SCHEMA } from "../builder/page-schema.js";
|
|
9
10
|
import { listElements, getElement, buildElement } from "../builder/catalog.js";
|
|
10
11
|
import { describeEventsCatalog } from "../builder/events.js";
|
|
@@ -21,9 +22,16 @@ const elementSpec = z.object({
|
|
|
21
22
|
colWidths: z.array(z.any()).optional().describe("layout:'row' only — explicit per-column unit objects (length = #children), e.g. [{unit:'fr',value:2},{unit:'fr',value:1}] for a 2:1 split. Default: equal columns."),
|
|
22
23
|
collapse: z.record(z.number()).optional().describe("layout:'row' only — columns to show per breakpoint, e.g. {bp3:2,bp4:1}. Default: tablet 2, mobile 1."),
|
|
23
24
|
});
|
|
25
|
+
/** A source object, its JSON, or "cache:<id>" — a source parked by clone_page. */
|
|
24
26
|
function parseSource(src) {
|
|
25
27
|
if (src == null)
|
|
26
28
|
return null;
|
|
29
|
+
if (typeof src === "string" && src.startsWith("cache:")) {
|
|
30
|
+
const hit = getCached(src.slice(6));
|
|
31
|
+
if (!hit)
|
|
32
|
+
throw new Error(`${src} expired or unknown — run clone_page again.`);
|
|
33
|
+
return JSON.parse(hit.text);
|
|
34
|
+
}
|
|
27
35
|
return typeof src === "string" ? JSON.parse(src) : src;
|
|
28
36
|
}
|
|
29
37
|
function newPageId(res) {
|
|
@@ -233,7 +241,7 @@ The source must be { sections: [...] } — build sections with new_section. Vali
|
|
|
233
241
|
ONE page per site for type main (homepage) / error / maintain, and slugs are unique per site: a duplicate is refused (dry_run reports blocked:true) and you get the existing page_id to edit instead. Only 'custom' pages can be created over and over.`, {
|
|
234
242
|
name: z.string().describe("Page name"),
|
|
235
243
|
slug: z.string().describe("URL slug WITHOUT a leading slash, e.g. 'about', 'collections', 'cart'. A leading '/' is stripped automatically (the storefront matches the bare path segment, so '/cart' would 404). Store pages MUST use the conventional slugs: category='collections', product detail='products', cart='cart', checkout='checkout', thank-you='complete'. The homepage needs no slug (pass is_homepage:true)."),
|
|
236
|
-
source: z.any().describe("Full page source { sections: [...] } (object or JSON string)"),
|
|
244
|
+
source: z.any().describe("Full page source { sections: [...] } (object or JSON string), or the source_ref \"cache:clone-N\" from clone_page"),
|
|
237
245
|
type: z
|
|
238
246
|
.enum(PAGE_KINDS)
|
|
239
247
|
.optional()
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// clone_page / get_clone_script: a rendered web page → native BuilderX sections (builder/clone.ts).
|
|
2
|
+
// The converted source is parked in the result cache and handed to build_page by reference
|
|
3
|
+
// (`source: "cache:clone-N"`), so every build_page guard (validation, slug, breakpoints) applies
|
|
4
|
+
// and a 300 KB source never has to travel through the agent's context.
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { CLONE_SCRIPT, captureInPage, snapshotToSource } from "../builder/clone.js";
|
|
7
|
+
import { validatePage, walk } from "../builder/page.js";
|
|
8
|
+
import { codeOf } from "../builder/element-schema.js";
|
|
9
|
+
import { cachePut } from "./result-cache.js";
|
|
10
|
+
import { cdnUpload } from "./builder-extras.js";
|
|
11
|
+
const RUN_HINT = "Open the page in your browser tool at ~1440px wide, let it load, run `script` there (chrome-devtools evaluate_script / claude-in-chrome javascript_tool / playwright evaluate — it is an async expression returning JSON), then call clone_page({ snapshot: <the result> }).";
|
|
12
|
+
/** Headless capture with a local Chrome via playwright-core (optional dependency). */
|
|
13
|
+
async function captureWithChrome(url, width) {
|
|
14
|
+
const mod = "playwright-core";
|
|
15
|
+
let chromium;
|
|
16
|
+
try {
|
|
17
|
+
({ chromium } = await import(mod));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
throw new Error("NO_BROWSER: playwright-core is not installed here.");
|
|
21
|
+
}
|
|
22
|
+
let browser;
|
|
23
|
+
try {
|
|
24
|
+
browser = await chromium.launch(process.env.CHROME_PATH ? { executablePath: process.env.CHROME_PATH, headless: true } : { channel: "chrome", headless: true });
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
throw new Error(`NO_BROWSER: no local Chrome (${String(e?.message || e).split("\n")[0]}). Set CHROME_PATH or use get_clone_script.`);
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const page = await browser.newPage({ viewport: { width, height: 900 } });
|
|
31
|
+
await page.goto(url, { waitUntil: "networkidle", timeout: 45000 }).catch(() => undefined);
|
|
32
|
+
return await page.evaluate(captureInPage, {});
|
|
33
|
+
}
|
|
34
|
+
finally {
|
|
35
|
+
await browser.close();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** One line per section: what it holds, so the agent can judge the clone without the source. */
|
|
39
|
+
function outline(sections, roles) {
|
|
40
|
+
const label = (n, d) => {
|
|
41
|
+
const kids = n.children || [];
|
|
42
|
+
const row = n.runtime?.config?.__row;
|
|
43
|
+
const txt = n.specials?.text ? ` "${String(n.specials.text).replace(/<[^>]+>/g, "").slice(0, 28)}"` : "";
|
|
44
|
+
const head = `${n.type}${txt}`;
|
|
45
|
+
if (!kids.length || d > 2)
|
|
46
|
+
return head;
|
|
47
|
+
return `${head}${row ? " row" : ""}[${kids.slice(0, 6).map((k) => label(k, d + 1)).join(", ")}${kids.length > 6 ? `, …+${kids.length - 6}` : ""}]`;
|
|
48
|
+
};
|
|
49
|
+
return sections.map((s, i) => `#${i + 1} ${roles[i]}: ${(s.children || []).map((k) => label(k, 1)).join(" | ")}`.slice(0, 400));
|
|
50
|
+
}
|
|
51
|
+
async function rehost(api, source, images) {
|
|
52
|
+
const map = new Map();
|
|
53
|
+
const failed = [];
|
|
54
|
+
const queue = [...new Set(images)];
|
|
55
|
+
const worker = async () => {
|
|
56
|
+
for (let src = queue.shift(); src; src = queue.shift()) {
|
|
57
|
+
try {
|
|
58
|
+
map.set(src, await cdnUpload(api, src));
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
failed.push(`${src.slice(0, 80)}: ${String(e?.message || e).slice(0, 80)}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
await Promise.all(Array.from({ length: 4 }, worker));
|
|
66
|
+
walk(source, (n) => {
|
|
67
|
+
const cfg = n.runtime?.config;
|
|
68
|
+
if (cfg?.src && map.has(cfg.src))
|
|
69
|
+
cfg.src = map.get(cfg.src);
|
|
70
|
+
if (n.specials?.src && map.has(n.specials.src))
|
|
71
|
+
n.specials.src = map.get(n.specials.src);
|
|
72
|
+
const bg = n.runtime?.style?.background;
|
|
73
|
+
if (typeof bg === "string")
|
|
74
|
+
n.runtime.style.background = bg.replace(/url\(([^)]+)\)/g, (m, u) => (map.has(u) ? `url(${map.get(u)})` : m));
|
|
75
|
+
return n;
|
|
76
|
+
});
|
|
77
|
+
return { hosted: map.size, failed };
|
|
78
|
+
}
|
|
79
|
+
export function registerCloneTools(server, api, handle) {
|
|
80
|
+
server.tool("get_clone_script", "Get the capture script for clone_page: run it inside the source page with YOUR browser tool when clone_page can't open the page itself (remote server, no local Chrome, a page behind login).", {}, () => handle(async () => ({ script: CLONE_SCRIPT, run: RUN_HINT })));
|
|
81
|
+
server.tool("clone_page", `Clone a web page into NATIVE sections (text/image/button/form/video/container rows — no embed, no custom code). Pass url (captured with a local Chrome when available) or snapshot (from get_clone_script run in your browser). Layout comes from the real boxes: side-by-side → a row with measured column widths (collapses on tablet/mobile), stacked → a column. Returns an outline, text coverage, fonts, notes on everything approximated or skipped, the validation result and source_ref. Then: build_page({ name, slug, source: source_ref, dry_run:true }) → dry_run:false. rehost_images:true uploads every image to the WebCake CDN first (the storefront shows CDN images only) — do it for a page you will keep. Clone only pages the user owns or may copy; rewrite copy/branding to theirs.`, {
|
|
82
|
+
url: z.string().url().optional().describe("Page to clone (public URL)"),
|
|
83
|
+
snapshot: z.any().optional().describe("Result of the get_clone_script script (object or JSON string), instead of url"),
|
|
84
|
+
width: z.number().int().min(320).max(1920).optional().describe("Capture width in px (default 1440)"),
|
|
85
|
+
skip_chrome: z.boolean().optional().describe("Leave out the source's header/footer/nav (use the site's global ones). Default false"),
|
|
86
|
+
rehost_images: z.boolean().optional().describe("Upload images (and svg icons as PNG) to the WebCake CDN (default false)"),
|
|
87
|
+
}, ({ url, snapshot, width, skip_chrome, rehost_images }) => handle(async () => {
|
|
88
|
+
let snap = typeof snapshot === "string" ? JSON.parse(snapshot) : snapshot;
|
|
89
|
+
if (!snap) {
|
|
90
|
+
if (!url)
|
|
91
|
+
throw new Error("Pass url or snapshot.");
|
|
92
|
+
try {
|
|
93
|
+
snap = await captureWithChrome(url, width || 1440);
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
if (!String(e?.message).startsWith("NO_BROWSER"))
|
|
97
|
+
throw e;
|
|
98
|
+
return { needs_browser: true, reason: e.message, next: `Call get_clone_script, then: ${RUN_HINT}` };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!snap || !Array.isArray(snap.sections))
|
|
102
|
+
throw new Error("snapshot has no sections — run the get_clone_script script on the page.");
|
|
103
|
+
const out = snapshotToSource(snap, { skipChrome: !!skip_chrome });
|
|
104
|
+
const images = rehost_images ? await rehost(api, out.source, out.images) : undefined;
|
|
105
|
+
const v = validatePage(out.source);
|
|
106
|
+
const byCode = {};
|
|
107
|
+
for (const w of v.warnings || [])
|
|
108
|
+
byCode[codeOf(w)] = (byCode[codeOf(w)] || 0) + 1;
|
|
109
|
+
const ref = cachePut(`clone ${snap.url || url || ""}`, JSON.stringify(out.source));
|
|
110
|
+
return {
|
|
111
|
+
url: snap.url || url,
|
|
112
|
+
title: snap.title,
|
|
113
|
+
stats: out.stats,
|
|
114
|
+
outline: outline(out.source.sections, out.roles),
|
|
115
|
+
fonts: out.fonts,
|
|
116
|
+
...(out.notes.length ? { notes: out.notes } : {}),
|
|
117
|
+
...(images ? { images: { hosted: images.hosted, ...(images.failed.length ? { failed: images.failed.slice(0, 10) } : {}) } } : { images: { external: out.images.length, note: out.images.length ? "Not on the CDN yet — the storefront won't show them. Re-run with rehost_images:true for a page you keep." : undefined } }),
|
|
118
|
+
validation: { valid: v.valid, errors: (v.errors || []).slice(0, 8), warnings_by_code: byCode },
|
|
119
|
+
source_ref: `cache:${ref}`,
|
|
120
|
+
next: `build_page({ name, slug, source: "cache:${ref}", dry_run: true }) then dry_run:false. Set the site fonts to ${out.fonts.slice(0, 2).join(" / ") || "the source's"} if they differ; rewrite the copy for the user's brand. The ref expires in 10 min (read it with read_cached_result).`,
|
|
121
|
+
};
|
|
122
|
+
}));
|
|
123
|
+
}
|
package/dist/tools/families.js
CHANGED
|
@@ -19,6 +19,9 @@ export const FAMILIES = {
|
|
|
19
19
|
builder_ref: ["Reference for composing pages: elements, attributes, events, bindings, schema, guides, section templates", [
|
|
20
20
|
"get_build_guide", "get_intake_guide", "list_elements", "get_element", "list_events", "list_bindings", "get_page_schema", "list_section_templates",
|
|
21
21
|
]],
|
|
22
|
+
clone: ["Copy an existing web page as native sections (clone_page), or read a page's text/images as a blueprint (ingest_*)", [
|
|
23
|
+
"clone_page", "get_clone_script", "ingest_url", "ingest_html",
|
|
24
|
+
]],
|
|
22
25
|
compose: ["Create element nodes from the factory (never hand-write nodes)", [
|
|
23
26
|
"new_element", "new_section", "new_row", "new_page_skeleton",
|
|
24
27
|
]],
|
package/dist/tools/registry.js
CHANGED
|
@@ -31,6 +31,7 @@ import { registerPageDraftTools } from "./page-draft.js";
|
|
|
31
31
|
import { registerSitePlanTools } from "./site-plans.js";
|
|
32
32
|
import { registerApiGatewayTools } from "./api-gateway.js";
|
|
33
33
|
import { registerPageCodeTools } from "./page-code.js";
|
|
34
|
+
import { registerCloneTools } from "./clone.js";
|
|
34
35
|
import { readFileSync } from "node:fs";
|
|
35
36
|
import { FAMILIES, familyTool } from "./families.js";
|
|
36
37
|
/** Tool names folded into api_get / api_call → their route (invoke_tool points callers there). */
|
|
@@ -90,6 +91,7 @@ const MODULES = [
|
|
|
90
91
|
["site_config", registerSitePlanTools],
|
|
91
92
|
["session", registerApiGatewayTools],
|
|
92
93
|
["web", registerPageCodeTools],
|
|
94
|
+
["web", registerCloneTools],
|
|
93
95
|
];
|
|
94
96
|
/** Every collected tool of a server, by name — members of a family included (tests, gateway). */
|
|
95
97
|
const ENTRIES = new WeakMap();
|
|
@@ -31,6 +31,14 @@ export function cacheLarge(label, data, threshold = DEFAULT_THRESHOLD) {
|
|
|
31
31
|
hint: `Large result cached (${text.length} chars). Read it in chunks with read_cached_result(cache_id="${id}", offset:0, length:${DEFAULT_CHUNK}). Expires in ${TTL_MS / 60000} min.`,
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
|
+
/** Store a payload under a fresh id (e.g. a cloned page source handed to build_page as "cache:<id>"). */
|
|
35
|
+
export function cachePut(label, text, prefix = "clone") {
|
|
36
|
+
const now = Date.now();
|
|
37
|
+
prune(now);
|
|
38
|
+
const id = `${prefix}-${++_seq}`;
|
|
39
|
+
_cache.set(id, { text, label, created: now });
|
|
40
|
+
return id;
|
|
41
|
+
}
|
|
34
42
|
/** Direct accessor for other modules (e.g. to peek a cached entry). */
|
|
35
43
|
export function getCached(id) {
|
|
36
44
|
return _cache.get(id);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webcake-storefront-mcp",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "MCP server for the WebCake/StoreCake storefront builder — page CRUD, page authoring, products, orders, and more",
|
|
5
5
|
"mcpName": "io.github.vuluu2k/webcake-storefront-mcp",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
},
|
|
52
52
|
"optionalDependencies": {
|
|
53
53
|
"ioredis": "^5.11.1",
|
|
54
|
-
"pg": "^8.21.0"
|
|
54
|
+
"pg": "^8.21.0",
|
|
55
|
+
"playwright-core": "^1.65.1"
|
|
55
56
|
}
|
|
56
57
|
}
|