@mushi-mushi/web 1.37.0 → 1.37.3
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/README.md +1 -1
- package/dist/chunks/chunk-ONXNS7MQ.js +35 -0
- package/dist/chunks/chunk-ONXNS7MQ.js.map +1 -0
- package/dist/chunks/screenshot-dom-K3N2HPNN.js +653 -0
- package/dist/chunks/screenshot-dom-K3N2HPNN.js.map +1 -0
- package/dist/headless.cjs +744 -271
- package/dist/headless.cjs.map +1 -1
- package/dist/headless.d.cts +1 -1
- package/dist/headless.d.ts +4 -0
- package/dist/headless.js +744 -271
- package/dist/headless.js.map +1 -1
- package/dist/index.cjs +739 -271
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.js +43 -275
- package/dist/index.js.map +1 -1
- package/dist/mushi.loader.global.js +20 -19
- package/dist/test-utils.cjs +595 -0
- package/dist/test-utils.cjs.map +1 -1
- package/dist/test-utils.js +595 -0
- package/dist/test-utils.js.map +1 -1
- package/dist/{timeline-B8nMyZyM.d.cts → timeline-BUrJ4n9r.d.cts} +4 -0
- package/package.json +8 -2
package/dist/headless.js
CHANGED
|
@@ -1,7 +1,710 @@
|
|
|
1
1
|
import { scrubUrl, DEFAULT_API_ENDPOINT, MUSHI_COLORS_LIGHT, MUSHI_INTERNAL_INIT_MARKER, MUSHI_INTERNAL_HEADER } from '@mushi-mushi/core';
|
|
2
2
|
export { DEFAULT_API_ENDPOINT } from '@mushi-mushi/core';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __esm = (fn, res) => function __init() {
|
|
7
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
8
|
+
};
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// src/capture/screenshot-dom.ts
|
|
15
|
+
var screenshot_dom_exports = {};
|
|
16
|
+
__export(screenshot_dom_exports, {
|
|
17
|
+
captureDomScreenshot: () => captureDomScreenshot
|
|
18
|
+
});
|
|
19
|
+
async function captureDomScreenshot(privacy, fail) {
|
|
20
|
+
const canvas = document.createElement("canvas");
|
|
21
|
+
const ctx = canvas.getContext("2d", { willReadFrequently: true });
|
|
22
|
+
if (!ctx) {
|
|
23
|
+
fail("unsupported");
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const width = window.innerWidth;
|
|
27
|
+
const height = window.innerHeight;
|
|
28
|
+
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
29
|
+
canvas.width = width * dpr;
|
|
30
|
+
canvas.height = height * dpr;
|
|
31
|
+
ctx.scale(dpr, dpr);
|
|
32
|
+
const safeDocument = await buildPrivacySafeDocument(privacy);
|
|
33
|
+
const markup = new XMLSerializer().serializeToString(safeDocument).replace(XML_INVALID_CHARS, "");
|
|
34
|
+
const svgData = `
|
|
35
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">
|
|
36
|
+
<foreignObject width="100%" height="100%">
|
|
37
|
+
<div xmlns="http://www.w3.org/1999/xhtml">
|
|
38
|
+
${markup}
|
|
39
|
+
</div>
|
|
40
|
+
</foreignObject>
|
|
41
|
+
</svg>
|
|
42
|
+
`;
|
|
43
|
+
const img = new Image();
|
|
44
|
+
const url = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgData)}`;
|
|
45
|
+
let cspBlocked = false;
|
|
46
|
+
const onViolation = (e) => {
|
|
47
|
+
const v = e;
|
|
48
|
+
if (/^img-src|^default-src/.test(v.violatedDirective ?? "") && /^data/.test(v.blockedURI ?? "")) cspBlocked = true;
|
|
49
|
+
};
|
|
50
|
+
document.addEventListener("securitypolicyviolation", onViolation);
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
let settled = false;
|
|
53
|
+
const settle = (value, reason) => {
|
|
54
|
+
if (settled) return;
|
|
55
|
+
settled = true;
|
|
56
|
+
clearTimeout(timer);
|
|
57
|
+
document.removeEventListener("securitypolicyviolation", onViolation);
|
|
58
|
+
if (reason) fail(reason);
|
|
59
|
+
resolve(value);
|
|
60
|
+
};
|
|
61
|
+
const timer = setTimeout(() => settle(null, "timeout"), 5e3);
|
|
62
|
+
img.onload = () => {
|
|
63
|
+
try {
|
|
64
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
65
|
+
if (isCanvasBlank(ctx, width, height)) {
|
|
66
|
+
settle(null, "unsupported");
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
settle(canvas.toDataURL("image/jpeg", 0.7));
|
|
70
|
+
} catch {
|
|
71
|
+
settle(null, "taint");
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
img.onerror = () => setTimeout(() => settle(null, cspBlocked ? "csp" : "error"), 0);
|
|
75
|
+
img.src = url;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
async function buildPrivacySafeDocument(privacy) {
|
|
79
|
+
const live = document.documentElement;
|
|
80
|
+
const clone = live.cloneNode(true);
|
|
81
|
+
const liveLinks = live.querySelectorAll("link");
|
|
82
|
+
const cloneLinks = clone.querySelectorAll("link");
|
|
83
|
+
const linkSlots = /* @__PURE__ */ new Map();
|
|
84
|
+
if (liveLinks.length === cloneLinks.length) liveLinks.forEach((l, i) => linkSlots.set(l, cloneLinks[i]));
|
|
85
|
+
const { offscreen, pins, backgrounds } = replayLiveState(live, clone);
|
|
86
|
+
fillScriptedStyleElements(live, clone);
|
|
87
|
+
const hidden = [
|
|
88
|
+
ALWAYS_REDACT_SELECTORS,
|
|
89
|
+
...privacy?.redactSelectors ?? DEFAULT_REDACT_SELECTORS,
|
|
90
|
+
...privacy?.maskSelectors ?? [],
|
|
91
|
+
...privacy?.blockSelectors ?? []
|
|
92
|
+
];
|
|
93
|
+
inlineMedia(
|
|
94
|
+
Array.from(live.querySelectorAll(MEDIA_SELECTOR)),
|
|
95
|
+
Array.from(clone.querySelectorAll(MEDIA_SELECTOR)),
|
|
96
|
+
(el) => offscreen.has(el) || hidden.some((selector) => matchesOrInside(el, selector))
|
|
97
|
+
);
|
|
98
|
+
pinOffscreen(pins);
|
|
99
|
+
await inlineBackgrounds(backgrounds.filter((bg) => !hidden.some((selector) => matchesOrInside(bg.live, selector))));
|
|
100
|
+
clone.querySelector("#mushi-mushi-widget")?.remove();
|
|
101
|
+
await inlineDocumentStyles(clone, linkSlots);
|
|
102
|
+
stripTaintSources(clone);
|
|
103
|
+
makeXmlSafe(clone);
|
|
104
|
+
freezeMotion(clone);
|
|
105
|
+
fillViewport(clone);
|
|
106
|
+
const redactSelectors = [
|
|
107
|
+
ALWAYS_REDACT_SELECTORS,
|
|
108
|
+
...privacy?.redactSelectors ?? DEFAULT_REDACT_SELECTORS
|
|
109
|
+
];
|
|
110
|
+
for (const selector of redactSelectors) {
|
|
111
|
+
for (const el of safeQueryAll(clone, selector)) {
|
|
112
|
+
redactElement(el);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
for (const selector of privacy?.blockSelectors ?? []) {
|
|
116
|
+
for (const el of safeQueryAll(clone, selector)) {
|
|
117
|
+
el.remove();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (const selector of privacy?.maskSelectors ?? []) {
|
|
121
|
+
for (const el of safeQueryAll(clone, selector)) {
|
|
122
|
+
maskElement(el);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return clone;
|
|
126
|
+
}
|
|
127
|
+
function replayLiveState(live, clone) {
|
|
128
|
+
const offscreen = /* @__PURE__ */ new Set();
|
|
129
|
+
const pins = [];
|
|
130
|
+
const backgrounds = [];
|
|
131
|
+
const liveAll = [live, ...Array.from(live.querySelectorAll("*"))];
|
|
132
|
+
const cloneAll = [clone, ...Array.from(clone.querySelectorAll("*"))];
|
|
133
|
+
if (liveAll.length !== cloneAll.length) return { offscreen, pins, backgrounds };
|
|
134
|
+
const animated = animationTargets();
|
|
135
|
+
const styles = [];
|
|
136
|
+
const scroller = document.scrollingElement;
|
|
137
|
+
const pending = /* @__PURE__ */ new Map();
|
|
138
|
+
const set = (el, prop, value) => {
|
|
139
|
+
let decls = pending.get(el);
|
|
140
|
+
if (!decls) pending.set(el, decls = /* @__PURE__ */ new Map());
|
|
141
|
+
decls.set(prop, value);
|
|
142
|
+
};
|
|
143
|
+
const scrolled = [];
|
|
144
|
+
const withBackground = [];
|
|
145
|
+
let inBody = false;
|
|
146
|
+
for (let i = 0; i < liveAll.length; i++) {
|
|
147
|
+
const liveEl = liveAll[i];
|
|
148
|
+
const cloneEl = cloneAll[i];
|
|
149
|
+
const computed = getComputedStyle(liveEl);
|
|
150
|
+
styles.push(computed);
|
|
151
|
+
if (liveEl === document.body) inBody = true;
|
|
152
|
+
if (inBody) {
|
|
153
|
+
if (computed.display === "none") set(cloneEl, "display", "none");
|
|
154
|
+
else {
|
|
155
|
+
const parent = liveEl.parentElement;
|
|
156
|
+
if (computed.visibility !== "visible" && parent && getComputedStyle(parent).visibility === "visible") {
|
|
157
|
+
set(cloneEl, "visibility", computed.visibility);
|
|
158
|
+
}
|
|
159
|
+
if (computed.opacity === "0") set(cloneEl, "opacity", "0");
|
|
160
|
+
if (computed.backgroundImage.includes("url(")) withBackground.push(i);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (animated.has(liveEl)) {
|
|
164
|
+
for (const p of ANIMATED_PROPERTIES) set(cloneEl, p, computed.getPropertyValue(p));
|
|
165
|
+
}
|
|
166
|
+
if (computed.position === "sticky" && liveEl instanceof HTMLElement) {
|
|
167
|
+
const offset = stickyOffset(liveEl);
|
|
168
|
+
if (offset) {
|
|
169
|
+
set(cloneEl, "position", "relative");
|
|
170
|
+
set(cloneEl, "top", `${offset.top}px`);
|
|
171
|
+
set(cloneEl, "left", `${offset.left}px`);
|
|
172
|
+
set(cloneEl, "bottom", "auto");
|
|
173
|
+
set(cloneEl, "right", "auto");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (liveEl !== scroller && (liveEl.scrollTop > 0 || liveEl.scrollLeft > 0)) scrolled.push(i);
|
|
177
|
+
}
|
|
178
|
+
for (const i of scrolled) {
|
|
179
|
+
const liveEl = liveAll[i];
|
|
180
|
+
const liveKids = liveEl.children;
|
|
181
|
+
const cloneKids = cloneAll[i].children;
|
|
182
|
+
for (let j = 0; j < liveKids.length && j < cloneKids.length; j++) {
|
|
183
|
+
const kid = getComputedStyle(liveKids[j]);
|
|
184
|
+
if (kid.position === "fixed") continue;
|
|
185
|
+
const own = pending.get(cloneKids[j])?.get("transform") ?? kid.transform;
|
|
186
|
+
const shift = `translate(${-liveEl.scrollLeft}px, ${-liveEl.scrollTop}px)`;
|
|
187
|
+
set(cloneKids[j], "transform", own && own !== "none" ? `${shift} ${own}` : shift);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (window.scrollX || window.scrollY) {
|
|
191
|
+
set(clone, "position", "relative");
|
|
192
|
+
set(clone, "top", `${-window.scrollY}px`);
|
|
193
|
+
set(clone, "left", `${-window.scrollX}px`);
|
|
194
|
+
}
|
|
195
|
+
for (const [el, decls] of pending) {
|
|
196
|
+
const held = Array.from(decls, ([p, v]) => `${p}:${v}!important`).join(";");
|
|
197
|
+
el.setAttribute("style", `${el.getAttribute("style") ?? ""};${held}`);
|
|
198
|
+
}
|
|
199
|
+
findOffscreen(liveAll, cloneAll, styles, offscreen, pins);
|
|
200
|
+
for (const i of withBackground) {
|
|
201
|
+
if (!offscreen.has(liveAll[i])) {
|
|
202
|
+
backgrounds.push({ live: liveAll[i], clone: cloneAll[i], value: styles[i].backgroundImage });
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return { offscreen, pins, backgrounds };
|
|
206
|
+
}
|
|
207
|
+
async function inlineBackgrounds(backgrounds) {
|
|
208
|
+
const urls = /* @__PURE__ */ new Map();
|
|
209
|
+
let bytes = 0;
|
|
210
|
+
const load = (url) => {
|
|
211
|
+
let pending = urls.get(url);
|
|
212
|
+
if (!pending) {
|
|
213
|
+
pending = urls.size >= MAX_BACKGROUND_URLS ? Promise.resolve(null) : readAsDataUrl(url).then((data) => {
|
|
214
|
+
if (!data || bytes + data.length > MAX_BACKGROUND_BYTES) return null;
|
|
215
|
+
bytes += data.length;
|
|
216
|
+
return data;
|
|
217
|
+
});
|
|
218
|
+
urls.set(url, pending);
|
|
219
|
+
}
|
|
220
|
+
return pending;
|
|
221
|
+
};
|
|
222
|
+
await Promise.all(
|
|
223
|
+
backgrounds.map(async (bg) => {
|
|
224
|
+
const refs = Array.from(bg.value.matchAll(CSS_URL), (m) => m[2].trim()).filter((u) => !u.startsWith("data:"));
|
|
225
|
+
if (refs.length === 0) return;
|
|
226
|
+
const data = await Promise.all(refs.map(load));
|
|
227
|
+
let value = bg.value;
|
|
228
|
+
refs.forEach((ref, k) => {
|
|
229
|
+
if (data[k]) value = value.split(ref).join(data[k]);
|
|
230
|
+
});
|
|
231
|
+
if (value !== bg.value) {
|
|
232
|
+
bg.clone.setAttribute("style", `${bg.clone.getAttribute("style") ?? ""};background-image:${value}!important`);
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
async function readAsDataUrl(url) {
|
|
238
|
+
if (typeof fetch !== "function") return null;
|
|
239
|
+
const abort = typeof AbortController === "function" ? new AbortController() : null;
|
|
240
|
+
const timer = setTimeout(() => abort?.abort(), SHEET_FETCH_TIMEOUT_MS);
|
|
241
|
+
try {
|
|
242
|
+
const res = await fetch(url, { cache: "force-cache", signal: abort?.signal });
|
|
243
|
+
if (!res.ok) return null;
|
|
244
|
+
const blob = await res.blob();
|
|
245
|
+
if (blob.size > MAX_BACKGROUND_IMAGE_BYTES || !blob.type.startsWith("image/")) return null;
|
|
246
|
+
const bytes = new Uint8Array(await blob.arrayBuffer());
|
|
247
|
+
let binary = "";
|
|
248
|
+
for (let i = 0; i < bytes.length; i += 32768) {
|
|
249
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + 32768));
|
|
250
|
+
}
|
|
251
|
+
return `data:${blob.type};base64,${btoa(binary)}`;
|
|
252
|
+
} catch {
|
|
253
|
+
return null;
|
|
254
|
+
} finally {
|
|
255
|
+
clearTimeout(timer);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
function findOffscreen(liveAll, cloneAll, styles, offscreen, pins) {
|
|
259
|
+
const bodyIndex = document.body ? liveAll.indexOf(document.body) : -1;
|
|
260
|
+
if (bodyIndex < 0) return;
|
|
261
|
+
const vw = window.innerWidth;
|
|
262
|
+
const vh = window.innerHeight;
|
|
263
|
+
const index = /* @__PURE__ */ new Map();
|
|
264
|
+
const visible = new Array(liveAll.length).fill(false);
|
|
265
|
+
const outside = new Array(liveAll.length).fill(false);
|
|
266
|
+
for (let i = bodyIndex; i < liveAll.length; i++) index.set(liveAll[i], i);
|
|
267
|
+
visible[bodyIndex] = true;
|
|
268
|
+
for (let i = liveAll.length - 1; i > bodyIndex; i--) {
|
|
269
|
+
const el = liveAll[i];
|
|
270
|
+
const r = el.getBoundingClientRect();
|
|
271
|
+
if (r.width > 0 || r.height > 0) {
|
|
272
|
+
if (r.bottom > 0 && r.top < vh && r.right > 0 && r.left < vw) visible[i] = true;
|
|
273
|
+
else outside[i] = true;
|
|
274
|
+
}
|
|
275
|
+
if (styles[i].position === "fixed") visible[i] = true;
|
|
276
|
+
if (visible[i]) {
|
|
277
|
+
outside[i] = false;
|
|
278
|
+
const parent = el.parentElement ? index.get(el.parentElement) : void 0;
|
|
279
|
+
if (parent !== void 0) visible[parent] = true;
|
|
280
|
+
} else if (outside[i]) {
|
|
281
|
+
offscreen.add(el);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const covered = new Array(liveAll.length).fill(false);
|
|
285
|
+
for (let i = bodyIndex + 1; i < liveAll.length; i++) {
|
|
286
|
+
const el = liveAll[i];
|
|
287
|
+
const parentIndex = el.parentElement ? index.get(el.parentElement) : void 0;
|
|
288
|
+
if (parentIndex === void 0) continue;
|
|
289
|
+
if (covered[parentIndex]) {
|
|
290
|
+
covered[i] = true;
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
if (!outside[i] || !(el instanceof HTMLElement)) continue;
|
|
294
|
+
const s = styles[i];
|
|
295
|
+
if (!PINNABLE_DISPLAY.test(s.display)) continue;
|
|
296
|
+
covered[i] = true;
|
|
297
|
+
const inFlow = BLOCK_FLOW.test(styles[parentIndex].display) && s.float === "none" && (s.position === "static" || s.position === "relative");
|
|
298
|
+
pins.push({
|
|
299
|
+
clone: cloneAll[i],
|
|
300
|
+
width: el.offsetWidth,
|
|
301
|
+
height: el.offsetHeight,
|
|
302
|
+
marginTop: inFlow ? gapAbove(el) : null,
|
|
303
|
+
marginBottom: inFlow ? gapBelow(el) : null
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
function gapAbove(el) {
|
|
308
|
+
const prev = el.previousElementSibling;
|
|
309
|
+
if (!(prev instanceof HTMLElement) || prev.offsetParent !== el.offsetParent) return null;
|
|
310
|
+
return Math.max(0, el.offsetTop - (prev.offsetTop + prev.offsetHeight));
|
|
311
|
+
}
|
|
312
|
+
function gapBelow(el) {
|
|
313
|
+
const next = el.nextElementSibling;
|
|
314
|
+
if (!(next instanceof HTMLElement) || next.offsetParent !== el.offsetParent) return null;
|
|
315
|
+
return Math.max(0, next.offsetTop - (el.offsetTop + el.offsetHeight));
|
|
316
|
+
}
|
|
317
|
+
function pinOffscreen(pins) {
|
|
318
|
+
for (const pin of pins) {
|
|
319
|
+
pin.clone.replaceChildren();
|
|
320
|
+
let pinned = `width:${pin.width}px!important;height:${pin.height}px!important;box-sizing:border-box!important;min-width:0!important;min-height:0!important;max-width:none!important;max-height:none!important;content-visibility:visible!important;contain:none!important;`;
|
|
321
|
+
if (pin.marginTop !== null) pinned += `margin-top:${pin.marginTop}px!important;`;
|
|
322
|
+
if (pin.marginBottom !== null) pinned += `margin-bottom:${pin.marginBottom}px!important;`;
|
|
323
|
+
pin.clone.setAttribute("style", `${pin.clone.getAttribute("style") ?? ""};${pinned}`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function animationTargets() {
|
|
327
|
+
try {
|
|
328
|
+
return new Set(
|
|
329
|
+
document.getAnimations().map((a) => a.effect?.target).filter((t) => t instanceof Element && !t.pseudoElement)
|
|
330
|
+
);
|
|
331
|
+
} catch {
|
|
332
|
+
return /* @__PURE__ */ new Set();
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
function stickyOffset(el) {
|
|
336
|
+
const stuck = el.getBoundingClientRect();
|
|
337
|
+
const value = el.style.getPropertyValue("position");
|
|
338
|
+
const priority = el.style.getPropertyPriority("position");
|
|
339
|
+
el.style.setProperty("position", "static", "important");
|
|
340
|
+
const flow = el.getBoundingClientRect();
|
|
341
|
+
el.style.removeProperty("position");
|
|
342
|
+
if (value) el.style.setProperty("position", value, priority);
|
|
343
|
+
const top = stuck.top - flow.top;
|
|
344
|
+
const left = stuck.left - flow.left;
|
|
345
|
+
return top || left ? { top, left } : null;
|
|
346
|
+
}
|
|
347
|
+
function inlineMedia(liveMedia, clonedMedia, isHidden) {
|
|
348
|
+
let count = 0;
|
|
349
|
+
let bytes = 0;
|
|
350
|
+
for (let i = 0; i < clonedMedia.length; i++) {
|
|
351
|
+
const cloned = clonedMedia[i];
|
|
352
|
+
const liveEl = liveMedia[i];
|
|
353
|
+
const rect = liveEl?.getBoundingClientRect();
|
|
354
|
+
const layout = liveEl instanceof HTMLElement && liveEl.offsetWidth > 0 ? { width: liveEl.offsetWidth, height: liveEl.offsetHeight } : rect && { width: rect.width, height: rect.height };
|
|
355
|
+
let pixels = null;
|
|
356
|
+
if (liveEl && layout && layout.width > 0 && layout.height > 0 && count < MAX_INLINED_MEDIA && bytes < MAX_INLINED_BYTES && !isHidden(liveEl)) {
|
|
357
|
+
pixels = readPixels(liveEl);
|
|
358
|
+
if (pixels) {
|
|
359
|
+
count++;
|
|
360
|
+
bytes += pixels.url.length;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
replaceMedia(cloned, pixels, layout);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function readPixels(el) {
|
|
367
|
+
try {
|
|
368
|
+
let source = null;
|
|
369
|
+
let width = 0;
|
|
370
|
+
let height = 0;
|
|
371
|
+
if (el instanceof HTMLCanvasElement) {
|
|
372
|
+
[source, width, height] = [el, el.width, el.height];
|
|
373
|
+
} else if (el instanceof HTMLImageElement && el.complete && el.naturalWidth > 0) {
|
|
374
|
+
[source, width, height] = [el, el.naturalWidth, el.naturalHeight];
|
|
375
|
+
} else if (el instanceof HTMLVideoElement && el.readyState >= 2) {
|
|
376
|
+
[source, width, height] = [el, el.videoWidth, el.videoHeight];
|
|
377
|
+
}
|
|
378
|
+
if (!source || width <= 0 || height <= 0) return null;
|
|
379
|
+
const scale = Math.min(1, MAX_IMAGE_SIDE / Math.max(width, height));
|
|
380
|
+
const canvas = document.createElement("canvas");
|
|
381
|
+
canvas.width = Math.max(1, Math.round(width * scale));
|
|
382
|
+
canvas.height = Math.max(1, Math.round(height * scale));
|
|
383
|
+
const ctx = canvas.getContext("2d");
|
|
384
|
+
if (!ctx) return null;
|
|
385
|
+
ctx.drawImage(source, 0, 0, canvas.width, canvas.height);
|
|
386
|
+
const url = canvas.toDataURL("image/webp", 0.85);
|
|
387
|
+
return url === "data:," ? null : { url, width, height };
|
|
388
|
+
} catch {
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
function matchesOrInside(el, selector) {
|
|
393
|
+
try {
|
|
394
|
+
return el.closest(selector) !== null;
|
|
395
|
+
} catch {
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
function replaceMedia(cloned, pixels, layout) {
|
|
400
|
+
const box = cloned.ownerDocument.createElement(pixels ? "img" : "div");
|
|
401
|
+
for (const name of cloned.getAttributeNames()) {
|
|
402
|
+
if (!SOURCE_ATTRIBUTES.has(name)) box.setAttribute(name, cloned.getAttribute(name) ?? "");
|
|
403
|
+
}
|
|
404
|
+
const size = layout ? `width:${layout.width}px;height:${layout.height}px;` : "";
|
|
405
|
+
if (pixels) {
|
|
406
|
+
box.setAttribute("src", pixels.url);
|
|
407
|
+
box.setAttribute("alt", "");
|
|
408
|
+
if (!box.hasAttribute("width")) box.setAttribute("width", String(pixels.width));
|
|
409
|
+
if (!box.hasAttribute("height")) box.setAttribute("height", String(pixels.height));
|
|
410
|
+
box.setAttribute("style", `${box.getAttribute("style") ?? ""};${size}`);
|
|
411
|
+
} else {
|
|
412
|
+
box.setAttribute(
|
|
413
|
+
"style",
|
|
414
|
+
`${box.getAttribute("style") ?? ""};${size}background:#d4d4d4!important;display:${cloned.tagName === "IMG" || cloned.tagName === "CANVAS" ? "inline-block" : "block"};`
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
const parent = cloned.parentElement;
|
|
418
|
+
const target = parent && parent.tagName === "PICTURE" ? parent : cloned;
|
|
419
|
+
target.replaceWith(box);
|
|
420
|
+
}
|
|
421
|
+
function makeXmlSafe(root) {
|
|
422
|
+
const doc = root.ownerDocument;
|
|
423
|
+
const walker = doc.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
|
|
424
|
+
const comments = [];
|
|
425
|
+
for (let n = walker.nextNode(); n; n = walker.nextNode()) comments.push(n);
|
|
426
|
+
for (const c of comments) c.parentNode?.removeChild(c);
|
|
427
|
+
for (const el of [root, ...Array.from(root.querySelectorAll("*"))]) {
|
|
428
|
+
for (const name of el.getAttributeNames()) {
|
|
429
|
+
if (!XML_ATTRIBUTE_NAME.test(name)) el.removeAttribute(name);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
function safeQueryAll(root, selector) {
|
|
434
|
+
try {
|
|
435
|
+
return Array.from(root.querySelectorAll(selector));
|
|
436
|
+
} catch {
|
|
437
|
+
return [];
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
function redactElement(el) {
|
|
441
|
+
if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
|
|
442
|
+
el.value = "";
|
|
443
|
+
el.setAttribute("value", "");
|
|
444
|
+
}
|
|
445
|
+
el.textContent = "";
|
|
446
|
+
el.setAttribute(
|
|
447
|
+
"style",
|
|
448
|
+
`${el.getAttribute("style") ?? ""};background:#000!important;color:#000!important;text-shadow:none!important;border-color:#000!important;`
|
|
449
|
+
);
|
|
450
|
+
el.setAttribute("data-mushi-redacted", "true");
|
|
451
|
+
while (el.firstChild) el.removeChild(el.firstChild);
|
|
452
|
+
}
|
|
453
|
+
function stripTaintSources(root) {
|
|
454
|
+
const pageOrigin = typeof location !== "undefined" ? location.origin : "";
|
|
455
|
+
for (const link of root.querySelectorAll('link[rel="stylesheet"], link[as="style"]')) {
|
|
456
|
+
link.remove();
|
|
457
|
+
}
|
|
458
|
+
for (const styleEl of root.querySelectorAll("style")) {
|
|
459
|
+
const cleaned = stripCssUrlRefs(styleEl.textContent ?? "", pageOrigin);
|
|
460
|
+
if (cleaned !== styleEl.textContent) styleEl.textContent = cleaned;
|
|
461
|
+
}
|
|
462
|
+
for (const el of root.querySelectorAll("[style]")) {
|
|
463
|
+
const s = el.getAttribute("style");
|
|
464
|
+
if (!s) continue;
|
|
465
|
+
const cleaned = stripCssUrlRefs(s, pageOrigin);
|
|
466
|
+
if (cleaned !== s) el.setAttribute("style", cleaned);
|
|
467
|
+
}
|
|
468
|
+
for (const script of root.querySelectorAll("script, noscript, template")) {
|
|
469
|
+
script.remove();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function stripCssUrlRefs(css, pageOrigin) {
|
|
473
|
+
return css.replace(/url\(\s*(['"]?)([^)'"]+)\1\s*\)/gi, (_match, _q, href) => {
|
|
474
|
+
const trimmed = href.trim();
|
|
475
|
+
if (!trimmed || trimmed.startsWith("data:") || trimmed.startsWith("#")) return _match;
|
|
476
|
+
if (isCrossOriginUrl(trimmed, pageOrigin)) return "none";
|
|
477
|
+
return _match;
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
async function inlineDocumentStyles(clone, linkSlots) {
|
|
481
|
+
const sheets = [
|
|
482
|
+
...Array.from(document.styleSheets),
|
|
483
|
+
...document.adoptedStyleSheets ?? []
|
|
484
|
+
];
|
|
485
|
+
const texts = await Promise.all(
|
|
486
|
+
sheets.map(async (sheet) => {
|
|
487
|
+
const owner = sheet.ownerNode;
|
|
488
|
+
if (sheet.disabled || owner instanceof Element && owner.tagName === "STYLE") return "";
|
|
489
|
+
return withMedia(await sheetSource(sheet) ?? serializeSheet(sheet), sheet);
|
|
490
|
+
})
|
|
491
|
+
);
|
|
492
|
+
const head = clone.querySelector("head") ?? clone;
|
|
493
|
+
sheets.forEach((sheet, i) => {
|
|
494
|
+
if (!texts[i]) return;
|
|
495
|
+
const styleEl = document.createElement("style");
|
|
496
|
+
styleEl.textContent = texts[i];
|
|
497
|
+
const slot = sheet.ownerNode ? linkSlots.get(sheet.ownerNode) : void 0;
|
|
498
|
+
if (slot && clone.contains(slot)) slot.replaceWith(styleEl);
|
|
499
|
+
else head.appendChild(styleEl);
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
async function sheetSource(sheet, depth = 0) {
|
|
503
|
+
const href = sheet.href;
|
|
504
|
+
if (!href || depth > 3 || typeof fetch !== "function") return null;
|
|
505
|
+
const abort = typeof AbortController === "function" ? new AbortController() : null;
|
|
506
|
+
const timer = setTimeout(() => abort?.abort(), SHEET_FETCH_TIMEOUT_MS);
|
|
507
|
+
try {
|
|
508
|
+
const res = await fetch(href, { cache: "force-cache", signal: abort?.signal });
|
|
509
|
+
if (!res.ok) return null;
|
|
510
|
+
let imported = "";
|
|
511
|
+
for (const rule of readableRules(sheet)) {
|
|
512
|
+
if (!(rule instanceof CSSImportRule) || !rule.styleSheet) continue;
|
|
513
|
+
const text = await sheetSource(rule.styleSheet, depth + 1) ?? serializeSheet(rule.styleSheet);
|
|
514
|
+
imported += wrapImport(text, rule);
|
|
515
|
+
}
|
|
516
|
+
const own = absolutizeCssUrls((await res.text()).replace(/@import\s[^;]*;/gi, ""), href);
|
|
517
|
+
return `${imported}
|
|
518
|
+
${own}`;
|
|
519
|
+
} catch {
|
|
520
|
+
return null;
|
|
521
|
+
} finally {
|
|
522
|
+
clearTimeout(timer);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
function readableRules(sheet) {
|
|
526
|
+
try {
|
|
527
|
+
return Array.from(sheet.cssRules);
|
|
528
|
+
} catch {
|
|
529
|
+
return [];
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
function wrapImport(text, rule) {
|
|
533
|
+
const layer = rule.layerName;
|
|
534
|
+
let out = layer == null ? text : `@layer ${layer}{${text}}`;
|
|
535
|
+
const media = rule.media?.mediaText;
|
|
536
|
+
if (media && media !== "all") out = `@media ${media}{${out}}`;
|
|
537
|
+
return `${out}
|
|
538
|
+
`;
|
|
539
|
+
}
|
|
540
|
+
function withMedia(text, sheet) {
|
|
541
|
+
const media = sheet.media?.mediaText;
|
|
542
|
+
return text && media && media !== "all" ? `@media ${media}{${text}}` : text;
|
|
543
|
+
}
|
|
544
|
+
function absolutizeCssUrls(css, base) {
|
|
545
|
+
return css.replace(/url\(\s*(['"]?)([^)'"]+)\1\s*\)/gi, (match, quote, ref) => {
|
|
546
|
+
const trimmed = ref.trim();
|
|
547
|
+
if (!trimmed || /^(data:|#|[a-z][a-z\d+.-]*:)/i.test(trimmed)) return match;
|
|
548
|
+
try {
|
|
549
|
+
return `url(${quote}${new URL(trimmed, base).href}${quote})`;
|
|
550
|
+
} catch {
|
|
551
|
+
return match;
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
function fillScriptedStyleElements(live, clone) {
|
|
556
|
+
const liveStyles = live.querySelectorAll("style");
|
|
557
|
+
const cloneStyles = clone.querySelectorAll("style");
|
|
558
|
+
if (liveStyles.length !== cloneStyles.length) return;
|
|
559
|
+
liveStyles.forEach((el, i) => {
|
|
560
|
+
if ((el.textContent ?? "").trim() || !el.sheet) return;
|
|
561
|
+
const text = serializeSheet(el.sheet);
|
|
562
|
+
if (text) cloneStyles[i].textContent = text;
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
function fillViewport(clone) {
|
|
566
|
+
const clear = (c) => !c || c === "transparent" || /^rgba\(.*,\s*0\)$/.test(c);
|
|
567
|
+
let bg = getComputedStyle(document.documentElement).backgroundColor;
|
|
568
|
+
if (clear(bg) && document.body) bg = getComputedStyle(document.body).backgroundColor;
|
|
569
|
+
const root = clone;
|
|
570
|
+
root.style.setProperty("background-color", clear(bg) ? "Canvas" : bg);
|
|
571
|
+
root.style.setProperty("min-height", `${window.innerHeight}px`);
|
|
572
|
+
}
|
|
573
|
+
function freezeMotion(clone) {
|
|
574
|
+
const styleEl = document.createElement("style");
|
|
575
|
+
styleEl.textContent = FREEZE_MOTION_CSS;
|
|
576
|
+
(clone.querySelector("head") ?? clone).appendChild(styleEl);
|
|
577
|
+
}
|
|
578
|
+
function serializeSheet(sheet) {
|
|
579
|
+
let rules;
|
|
580
|
+
try {
|
|
581
|
+
rules = sheet.cssRules;
|
|
582
|
+
} catch {
|
|
583
|
+
return "";
|
|
584
|
+
}
|
|
585
|
+
let text = "";
|
|
586
|
+
for (const rule of Array.from(rules)) {
|
|
587
|
+
if (rule instanceof CSSImportRule) {
|
|
588
|
+
if (rule.styleSheet) text += serializeSheet(rule.styleSheet);
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
text += `${rule.cssText}
|
|
592
|
+
`;
|
|
593
|
+
}
|
|
594
|
+
return text;
|
|
595
|
+
}
|
|
596
|
+
function isCanvasBlank(ctx, width, height) {
|
|
597
|
+
try {
|
|
598
|
+
const stepX = Math.max(1, Math.floor(width / 8));
|
|
599
|
+
const stepY = Math.max(1, Math.floor(height / 8));
|
|
600
|
+
for (let y = 0; y < height; y += stepY) {
|
|
601
|
+
for (let x = 0; x < width; x += stepX) {
|
|
602
|
+
if (ctx.getImageData(x, y, 1, 1).data[3] !== 0) return false;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
return true;
|
|
606
|
+
} catch {
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
function isCrossOriginUrl(raw, pageOrigin) {
|
|
611
|
+
if (!raw || raw.startsWith("data:") || raw.startsWith("blob:")) return false;
|
|
612
|
+
try {
|
|
613
|
+
const resolved = new URL(raw, typeof location !== "undefined" ? location.href : "http://localhost/");
|
|
614
|
+
return Boolean(pageOrigin) && resolved.origin !== pageOrigin;
|
|
615
|
+
} catch {
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
function maskElement(el) {
|
|
620
|
+
if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
|
|
621
|
+
el.value = "";
|
|
622
|
+
el.setAttribute("value", "");
|
|
623
|
+
el.setAttribute("placeholder", "\u2022\u2022\u2022\u2022");
|
|
624
|
+
}
|
|
625
|
+
el.textContent = el.children.length === 0 ? "\u2022\u2022\u2022\u2022" : el.textContent;
|
|
626
|
+
el.setAttribute(
|
|
627
|
+
"style",
|
|
628
|
+
`${el.getAttribute("style") ?? ""};background:#8f8f8f!important;color:transparent!important;text-shadow:none!important;`
|
|
629
|
+
);
|
|
630
|
+
el.setAttribute("data-mushi-masked", "true");
|
|
631
|
+
}
|
|
632
|
+
var XML_INVALID_CHARS, XML_ATTRIBUTE_NAME, MAX_INLINED_MEDIA, MAX_INLINED_BYTES, MEDIA_SELECTOR, DEFAULT_REDACT_SELECTORS, ANIMATED_PROPERTIES, MAX_BACKGROUND_URLS, MAX_BACKGROUND_BYTES, MAX_BACKGROUND_IMAGE_BYTES, CSS_URL, PINNABLE_DISPLAY, BLOCK_FLOW, MAX_IMAGE_SIDE, SOURCE_ATTRIBUTES, SHEET_FETCH_TIMEOUT_MS, FREEZE_MOTION_CSS;
|
|
633
|
+
var init_screenshot_dom = __esm({
|
|
634
|
+
"src/capture/screenshot-dom.ts"() {
|
|
635
|
+
init_screenshot();
|
|
636
|
+
XML_INVALID_CHARS = /[\u0000-\u0008\u000B\u000C\u000E-\u001F]/g;
|
|
637
|
+
XML_ATTRIBUTE_NAME = /^[A-Za-z_][\w.:-]*$/;
|
|
638
|
+
MAX_INLINED_MEDIA = 60;
|
|
639
|
+
MAX_INLINED_BYTES = 12 * 1024 * 1024;
|
|
640
|
+
MEDIA_SELECTOR = "img, video, canvas, iframe, object, embed";
|
|
641
|
+
DEFAULT_REDACT_SELECTORS = ["[data-mushi-redact]"];
|
|
642
|
+
ANIMATED_PROPERTIES = [
|
|
643
|
+
"transform",
|
|
644
|
+
"translate",
|
|
645
|
+
"scale",
|
|
646
|
+
"rotate",
|
|
647
|
+
"opacity",
|
|
648
|
+
"visibility",
|
|
649
|
+
"clip-path",
|
|
650
|
+
"object-position",
|
|
651
|
+
"background-position",
|
|
652
|
+
"left",
|
|
653
|
+
"top",
|
|
654
|
+
"width",
|
|
655
|
+
"height",
|
|
656
|
+
"color",
|
|
657
|
+
"background-color"
|
|
658
|
+
];
|
|
659
|
+
MAX_BACKGROUND_URLS = 40;
|
|
660
|
+
MAX_BACKGROUND_BYTES = 6 * 1024 * 1024;
|
|
661
|
+
MAX_BACKGROUND_IMAGE_BYTES = 2 * 1024 * 1024;
|
|
662
|
+
CSS_URL = /url\(\s*(['"]?)([^)'"]+)\1\s*\)/gi;
|
|
663
|
+
PINNABLE_DISPLAY = /^(block|flow-root|flex|grid|list-item|inline-block|inline-flex|inline-grid|table)$/;
|
|
664
|
+
BLOCK_FLOW = /^(block|flow-root|list-item)$/;
|
|
665
|
+
MAX_IMAGE_SIDE = 2048;
|
|
666
|
+
SOURCE_ATTRIBUTES = /* @__PURE__ */ new Set(["src", "srcset", "sizes", "poster", "data", "loading", "decoding"]);
|
|
667
|
+
SHEET_FETCH_TIMEOUT_MS = 2500;
|
|
668
|
+
FREEZE_MOTION_CSS = "*,*::before,*::after{animation:none!important;transition:none!important}";
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
// src/capture/screenshot.ts
|
|
673
|
+
function createScreenshotCapture(options = {}) {
|
|
674
|
+
let activeOptions = options;
|
|
675
|
+
const fail = (reason) => {
|
|
676
|
+
activeOptions.onFailed?.(reason);
|
|
677
|
+
emitScreenshotFailed(reason);
|
|
678
|
+
};
|
|
679
|
+
async function take() {
|
|
680
|
+
try {
|
|
681
|
+
if (typeof document === "undefined") return null;
|
|
682
|
+
const { captureDomScreenshot: captureDomScreenshot2 } = await Promise.resolve().then(() => (init_screenshot_dom(), screenshot_dom_exports));
|
|
683
|
+
return await captureDomScreenshot2(activeOptions.privacy, fail);
|
|
684
|
+
} catch {
|
|
685
|
+
fail("error");
|
|
686
|
+
return null;
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
return {
|
|
690
|
+
take,
|
|
691
|
+
updateOptions(nextOptions) {
|
|
692
|
+
activeOptions = nextOptions;
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
function emitScreenshotFailed(reason) {
|
|
697
|
+
try {
|
|
698
|
+
document.dispatchEvent(new CustomEvent("mushi:screenshot_failed", { detail: { reason }, bubbles: false }));
|
|
699
|
+
} catch {
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
var ALWAYS_REDACT_SELECTORS;
|
|
703
|
+
var init_screenshot = __esm({
|
|
704
|
+
"src/capture/screenshot.ts"() {
|
|
705
|
+
ALWAYS_REDACT_SELECTORS = 'input[type="password"],input[autocomplete^="cc-"],[data-private],[data-mushi-mask]';
|
|
706
|
+
}
|
|
707
|
+
});
|
|
5
708
|
var DEFAULT_INTERNAL_URL_MATCHERS = [
|
|
6
709
|
/\/v1\/sdk(?:\/|$)/,
|
|
7
710
|
/\/v1\/reports(?:\/|$)/,
|
|
@@ -312,36 +1015,51 @@ function createConsoleCapture() {
|
|
|
312
1015
|
const originals = {};
|
|
313
1016
|
const wrappers = {};
|
|
314
1017
|
const levels = ["log", "warn", "error", "info", "debug"];
|
|
1018
|
+
const str = (arg) => {
|
|
1019
|
+
if (arg instanceof Error) return `${arg.name}: ${arg.message}`;
|
|
1020
|
+
try {
|
|
1021
|
+
return typeof arg === "string" ? arg : JSON.stringify(arg);
|
|
1022
|
+
} catch {
|
|
1023
|
+
return String(arg);
|
|
1024
|
+
}
|
|
1025
|
+
};
|
|
1026
|
+
const record = (level, message, err, extra) => {
|
|
1027
|
+
const entry = { level, message: message.slice(0, MAX_MESSAGE_LENGTH), timestamp: Date.now(), ...extra };
|
|
1028
|
+
if (err instanceof Error && err.stack) entry.stack = err.stack.slice(0, 1e3);
|
|
1029
|
+
entries.push(entry);
|
|
1030
|
+
if (entries.length > MAX_ENTRIES2) entries.shift();
|
|
1031
|
+
};
|
|
315
1032
|
for (const level of levels) {
|
|
316
1033
|
const original = console[level];
|
|
317
1034
|
originals[level] = original;
|
|
318
1035
|
const wrapper = (...args) => {
|
|
319
|
-
const message = args.map((arg) => {
|
|
320
|
-
try {
|
|
321
|
-
return typeof arg === "string" ? arg : JSON.stringify(arg);
|
|
322
|
-
} catch {
|
|
323
|
-
return String(arg);
|
|
324
|
-
}
|
|
325
|
-
}).join(" ").slice(0, MAX_MESSAGE_LENGTH);
|
|
326
1036
|
const correlationId = getActiveCorrelationId();
|
|
327
|
-
|
|
1037
|
+
record(
|
|
328
1038
|
level,
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
if (level === "error" && args[0] instanceof Error) {
|
|
334
|
-
entry.stack = args[0].stack?.slice(0, 1e3);
|
|
335
|
-
}
|
|
336
|
-
entries.push(entry);
|
|
337
|
-
if (entries.length > MAX_ENTRIES2) {
|
|
338
|
-
entries.shift();
|
|
339
|
-
}
|
|
1039
|
+
args.map(str).join(" "),
|
|
1040
|
+
level === "error" ? args.find((a) => a instanceof Error) : void 0,
|
|
1041
|
+
correlationId ? { correlationId } : void 0
|
|
1042
|
+
);
|
|
340
1043
|
original.call(console, ...args);
|
|
341
1044
|
};
|
|
342
1045
|
wrappers[level] = wrapper;
|
|
343
1046
|
console[level] = wrapper;
|
|
344
1047
|
}
|
|
1048
|
+
const onUncaught = (event) => {
|
|
1049
|
+
if (event.type === "unhandledrejection") {
|
|
1050
|
+
const reason = event.reason;
|
|
1051
|
+
record("error", `Unhandled rejection: ${str(reason)}`, reason);
|
|
1052
|
+
} else {
|
|
1053
|
+
const e = event;
|
|
1054
|
+
const where = e.filename ? ` (${e.filename}:${e.lineno}:${e.colno})` : "";
|
|
1055
|
+
record("error", `Uncaught ${str(e.error ?? (e.message || "error"))}${where}`, e.error);
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
const canListen = typeof window !== "undefined" && typeof window.addEventListener === "function";
|
|
1059
|
+
if (canListen) {
|
|
1060
|
+
window.addEventListener("error", onUncaught);
|
|
1061
|
+
window.addEventListener("unhandledrejection", onUncaught);
|
|
1062
|
+
}
|
|
345
1063
|
return {
|
|
346
1064
|
getEntries() {
|
|
347
1065
|
return [...entries];
|
|
@@ -355,261 +1073,16 @@ function createConsoleCapture() {
|
|
|
355
1073
|
console[level] = originals[level];
|
|
356
1074
|
}
|
|
357
1075
|
}
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
// src/capture/screenshot.ts
|
|
363
|
-
function createScreenshotCapture(options = {}) {
|
|
364
|
-
let activeOptions = options;
|
|
365
|
-
async function take() {
|
|
366
|
-
try {
|
|
367
|
-
if (typeof document === "undefined") return null;
|
|
368
|
-
const canvas = document.createElement("canvas");
|
|
369
|
-
const ctx = canvas.getContext("2d", { willReadFrequently: true });
|
|
370
|
-
if (!ctx) {
|
|
371
|
-
activeOptions.onFailed?.("unsupported");
|
|
372
|
-
emitScreenshotFailed("unsupported");
|
|
373
|
-
return null;
|
|
1076
|
+
if (canListen) {
|
|
1077
|
+
window.removeEventListener("error", onUncaught);
|
|
1078
|
+
window.removeEventListener("unhandledrejection", onUncaught);
|
|
374
1079
|
}
|
|
375
|
-
const width = window.innerWidth;
|
|
376
|
-
const height = window.innerHeight;
|
|
377
|
-
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
378
|
-
canvas.width = width * dpr;
|
|
379
|
-
canvas.height = height * dpr;
|
|
380
|
-
ctx.scale(dpr, dpr);
|
|
381
|
-
const safeDocument = buildPrivacySafeDocument(activeOptions.privacy);
|
|
382
|
-
const svgData = `
|
|
383
|
-
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">
|
|
384
|
-
<foreignObject width="100%" height="100%">
|
|
385
|
-
<div xmlns="http://www.w3.org/1999/xhtml">
|
|
386
|
-
${new XMLSerializer().serializeToString(safeDocument)}
|
|
387
|
-
</div>
|
|
388
|
-
</foreignObject>
|
|
389
|
-
</svg>
|
|
390
|
-
`;
|
|
391
|
-
const img = new Image();
|
|
392
|
-
const url = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgData)}`;
|
|
393
|
-
let cspBlocked = false;
|
|
394
|
-
const onViolation = (e) => {
|
|
395
|
-
const v = e;
|
|
396
|
-
if (/^img-src|^default-src/.test(v.violatedDirective ?? "") && /^data/.test(v.blockedURI ?? "")) cspBlocked = true;
|
|
397
|
-
};
|
|
398
|
-
document.addEventListener("securitypolicyviolation", onViolation);
|
|
399
|
-
return new Promise((resolve) => {
|
|
400
|
-
let settled = false;
|
|
401
|
-
const settle = (value, reason) => {
|
|
402
|
-
if (settled) return;
|
|
403
|
-
settled = true;
|
|
404
|
-
clearTimeout(timer);
|
|
405
|
-
document.removeEventListener("securitypolicyviolation", onViolation);
|
|
406
|
-
if (reason) {
|
|
407
|
-
activeOptions.onFailed?.(reason);
|
|
408
|
-
emitScreenshotFailed(reason);
|
|
409
|
-
}
|
|
410
|
-
resolve(value);
|
|
411
|
-
};
|
|
412
|
-
const timer = setTimeout(() => settle(null, "timeout"), 5e3);
|
|
413
|
-
img.onload = () => {
|
|
414
|
-
try {
|
|
415
|
-
ctx.drawImage(img, 0, 0, width, height);
|
|
416
|
-
if (isCanvasBlank(ctx, width, height)) {
|
|
417
|
-
settle(null, "unsupported");
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
420
|
-
settle(canvas.toDataURL("image/jpeg", 0.7));
|
|
421
|
-
} catch {
|
|
422
|
-
settle(null, "taint");
|
|
423
|
-
}
|
|
424
|
-
};
|
|
425
|
-
img.onerror = () => setTimeout(() => settle(null, cspBlocked ? "csp" : "error"), 0);
|
|
426
|
-
img.src = url;
|
|
427
|
-
});
|
|
428
|
-
} catch {
|
|
429
|
-
activeOptions.onFailed?.("error");
|
|
430
|
-
emitScreenshotFailed("error");
|
|
431
|
-
return null;
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
return {
|
|
435
|
-
take,
|
|
436
|
-
updateOptions(nextOptions) {
|
|
437
|
-
activeOptions = nextOptions;
|
|
438
1080
|
}
|
|
439
1081
|
};
|
|
440
1082
|
}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
} catch {
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
var ALWAYS_REDACT_SELECTORS = 'input[type="password"],input[autocomplete^="cc-"],[data-private],[data-mushi-mask]';
|
|
448
|
-
var DEFAULT_REDACT_SELECTORS = ["[data-mushi-redact]"];
|
|
449
|
-
function buildPrivacySafeDocument(privacy) {
|
|
450
|
-
const clone = document.documentElement.cloneNode(true);
|
|
451
|
-
clone.querySelector("#mushi-mushi-widget")?.remove();
|
|
452
|
-
stripTaintSources(clone);
|
|
453
|
-
inlineDocumentStyles(clone);
|
|
454
|
-
freezeMotion(clone);
|
|
455
|
-
fillViewport(clone);
|
|
456
|
-
const redactSelectors = [
|
|
457
|
-
ALWAYS_REDACT_SELECTORS,
|
|
458
|
-
...privacy?.redactSelectors ?? DEFAULT_REDACT_SELECTORS
|
|
459
|
-
];
|
|
460
|
-
for (const selector of redactSelectors) {
|
|
461
|
-
for (const el of safeQueryAll(clone, selector)) {
|
|
462
|
-
redactElement(el);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
for (const selector of privacy?.blockSelectors ?? []) {
|
|
466
|
-
for (const el of safeQueryAll(clone, selector)) {
|
|
467
|
-
el.remove();
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
for (const selector of privacy?.maskSelectors ?? []) {
|
|
471
|
-
for (const el of safeQueryAll(clone, selector)) {
|
|
472
|
-
maskElement(el);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
return clone;
|
|
476
|
-
}
|
|
477
|
-
function safeQueryAll(root, selector) {
|
|
478
|
-
try {
|
|
479
|
-
return Array.from(root.querySelectorAll(selector));
|
|
480
|
-
} catch {
|
|
481
|
-
return [];
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
function redactElement(el) {
|
|
485
|
-
if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
|
|
486
|
-
el.value = "";
|
|
487
|
-
el.setAttribute("value", "");
|
|
488
|
-
}
|
|
489
|
-
el.textContent = "";
|
|
490
|
-
el.setAttribute(
|
|
491
|
-
"style",
|
|
492
|
-
`${el.getAttribute("style") ?? ""};background:#000!important;color:#000!important;text-shadow:none!important;border-color:#000!important;`
|
|
493
|
-
);
|
|
494
|
-
el.setAttribute("data-mushi-redacted", "true");
|
|
495
|
-
while (el.firstChild) el.removeChild(el.firstChild);
|
|
496
|
-
}
|
|
497
|
-
function stripTaintSources(root) {
|
|
498
|
-
const pageOrigin = typeof location !== "undefined" ? location.origin : "";
|
|
499
|
-
for (const el of root.querySelectorAll("img, video, iframe, object, embed, picture")) {
|
|
500
|
-
el.remove();
|
|
501
|
-
}
|
|
502
|
-
for (const link of root.querySelectorAll('link[rel="stylesheet"], link[as="style"]')) {
|
|
503
|
-
link.remove();
|
|
504
|
-
}
|
|
505
|
-
for (const styleEl of root.querySelectorAll("style")) {
|
|
506
|
-
const cleaned = stripCssUrlRefs(styleEl.textContent ?? "", pageOrigin);
|
|
507
|
-
if (cleaned !== styleEl.textContent) styleEl.textContent = cleaned;
|
|
508
|
-
}
|
|
509
|
-
for (const el of root.querySelectorAll("[style]")) {
|
|
510
|
-
const s = el.getAttribute("style");
|
|
511
|
-
if (!s) continue;
|
|
512
|
-
const cleaned = stripCssUrlRefs(s, pageOrigin);
|
|
513
|
-
if (cleaned !== s) el.setAttribute("style", cleaned);
|
|
514
|
-
}
|
|
515
|
-
for (const script of root.querySelectorAll("script")) {
|
|
516
|
-
script.remove();
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
function stripCssUrlRefs(css, pageOrigin) {
|
|
520
|
-
return css.replace(/url\(\s*(['"]?)([^)'"]+)\1\s*\)/gi, (_match, _q, href) => {
|
|
521
|
-
const trimmed = href.trim();
|
|
522
|
-
if (!trimmed || trimmed.startsWith("data:") || trimmed.startsWith("#")) return _match;
|
|
523
|
-
if (isCrossOriginUrl(trimmed, pageOrigin)) return "none";
|
|
524
|
-
return _match;
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
function inlineDocumentStyles(clone) {
|
|
528
|
-
const pageOrigin = typeof location !== "undefined" ? location.origin : "";
|
|
529
|
-
const chunks = [];
|
|
530
|
-
const sheets = [
|
|
531
|
-
...Array.from(document.styleSheets),
|
|
532
|
-
...document.adoptedStyleSheets ?? []
|
|
533
|
-
];
|
|
534
|
-
for (const sheet of sheets) {
|
|
535
|
-
const owner = sheet.ownerNode;
|
|
536
|
-
if (owner instanceof Element && owner.tagName === "STYLE") continue;
|
|
537
|
-
chunks.push(serializeSheet(sheet));
|
|
538
|
-
}
|
|
539
|
-
const css = stripCssUrlRefs(chunks.filter(Boolean).join("\n"), pageOrigin);
|
|
540
|
-
if (!css) return;
|
|
541
|
-
const styleEl = document.createElement("style");
|
|
542
|
-
styleEl.textContent = css;
|
|
543
|
-
(clone.querySelector("head") ?? clone).appendChild(styleEl);
|
|
544
|
-
}
|
|
545
|
-
var FREEZE_MOTION_CSS = "*,*::before,*::after{animation:none!important;transition:none!important}";
|
|
546
|
-
function fillViewport(clone) {
|
|
547
|
-
const clear = (c) => !c || c === "transparent" || /^rgba\(.*,\s*0\)$/.test(c);
|
|
548
|
-
let bg = getComputedStyle(document.documentElement).backgroundColor;
|
|
549
|
-
if (clear(bg) && document.body) bg = getComputedStyle(document.body).backgroundColor;
|
|
550
|
-
const root = clone;
|
|
551
|
-
root.style.setProperty("background-color", clear(bg) ? "Canvas" : bg);
|
|
552
|
-
root.style.setProperty("min-height", `${window.innerHeight}px`);
|
|
553
|
-
}
|
|
554
|
-
function freezeMotion(clone) {
|
|
555
|
-
const styleEl = document.createElement("style");
|
|
556
|
-
styleEl.textContent = FREEZE_MOTION_CSS;
|
|
557
|
-
(clone.querySelector("head") ?? clone).appendChild(styleEl);
|
|
558
|
-
}
|
|
559
|
-
function serializeSheet(sheet) {
|
|
560
|
-
let rules;
|
|
561
|
-
try {
|
|
562
|
-
rules = sheet.cssRules;
|
|
563
|
-
} catch {
|
|
564
|
-
return "";
|
|
565
|
-
}
|
|
566
|
-
let text = "";
|
|
567
|
-
for (const rule of Array.from(rules)) {
|
|
568
|
-
if (rule instanceof CSSImportRule) {
|
|
569
|
-
if (rule.styleSheet) text += serializeSheet(rule.styleSheet);
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
|
-
text += `${rule.cssText}
|
|
573
|
-
`;
|
|
574
|
-
}
|
|
575
|
-
return text;
|
|
576
|
-
}
|
|
577
|
-
function isCanvasBlank(ctx, width, height) {
|
|
578
|
-
try {
|
|
579
|
-
const stepX = Math.max(1, Math.floor(width / 8));
|
|
580
|
-
const stepY = Math.max(1, Math.floor(height / 8));
|
|
581
|
-
for (let y = 0; y < height; y += stepY) {
|
|
582
|
-
for (let x = 0; x < width; x += stepX) {
|
|
583
|
-
if (ctx.getImageData(x, y, 1, 1).data[3] !== 0) return false;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
return true;
|
|
587
|
-
} catch {
|
|
588
|
-
return false;
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
function isCrossOriginUrl(raw, pageOrigin) {
|
|
592
|
-
if (!raw || raw.startsWith("data:") || raw.startsWith("blob:")) return false;
|
|
593
|
-
try {
|
|
594
|
-
const resolved = new URL(raw, typeof location !== "undefined" ? location.href : "http://localhost/");
|
|
595
|
-
return Boolean(pageOrigin) && resolved.origin !== pageOrigin;
|
|
596
|
-
} catch {
|
|
597
|
-
return true;
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
function maskElement(el) {
|
|
601
|
-
if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
|
|
602
|
-
el.value = "";
|
|
603
|
-
el.setAttribute("value", "");
|
|
604
|
-
el.setAttribute("placeholder", "\u2022\u2022\u2022\u2022");
|
|
605
|
-
}
|
|
606
|
-
el.textContent = el.children.length === 0 ? "\u2022\u2022\u2022\u2022" : el.textContent;
|
|
607
|
-
el.setAttribute(
|
|
608
|
-
"style",
|
|
609
|
-
`${el.getAttribute("style") ?? ""};background:#8f8f8f!important;color:transparent!important;text-shadow:none!important;`
|
|
610
|
-
);
|
|
611
|
-
el.setAttribute("data-mushi-masked", "true");
|
|
612
|
-
}
|
|
1083
|
+
|
|
1084
|
+
// src/capture/index.ts
|
|
1085
|
+
init_screenshot();
|
|
613
1086
|
|
|
614
1087
|
// src/capture/performance.ts
|
|
615
1088
|
var INP_DURATION_THRESHOLD_MS = 40;
|