@mushi-mushi/web 1.29.0 → 1.31.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/headless.cjs CHANGED
@@ -349,8 +349,12 @@ function createScreenshotCapture(options = {}) {
349
349
  try {
350
350
  if (typeof document === "undefined") return null;
351
351
  const canvas = document.createElement("canvas");
352
- const ctx = canvas.getContext("2d");
353
- if (!ctx) return null;
352
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
353
+ if (!ctx) {
354
+ activeOptions.onFailed?.("unsupported");
355
+ emitScreenshotFailed("unsupported");
356
+ return null;
357
+ }
354
358
  const width = window.innerWidth;
355
359
  const height = window.innerHeight;
356
360
  const dpr = Math.min(window.devicePixelRatio || 1, 2);
@@ -368,27 +372,32 @@ function createScreenshotCapture(options = {}) {
368
372
  </svg>
369
373
  `;
370
374
  const img = new Image();
371
- const blob = new Blob([svgData], { type: "image/svg+xml;charset=utf-8" });
372
- const url = URL.createObjectURL(blob);
375
+ const url = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svgData)}`;
376
+ let cspBlocked = false;
377
+ const onViolation = (e) => {
378
+ const v = e;
379
+ if (/^img-src|^default-src/.test(v.violatedDirective ?? "") && /^data/.test(v.blockedURI ?? "")) cspBlocked = true;
380
+ };
381
+ document.addEventListener("securitypolicyviolation", onViolation);
373
382
  return new Promise((resolve) => {
374
383
  let settled = false;
375
384
  const settle = (value, reason) => {
376
385
  if (settled) return;
377
386
  settled = true;
378
387
  clearTimeout(timer);
379
- URL.revokeObjectURL(url);
388
+ document.removeEventListener("securitypolicyviolation", onViolation);
380
389
  if (reason) {
381
390
  activeOptions.onFailed?.(reason);
382
391
  emitScreenshotFailed(reason);
383
392
  }
384
393
  resolve(value);
385
394
  };
386
- const timer = setTimeout(() => settle(null, "error"), 5e3);
395
+ const timer = setTimeout(() => settle(null, "timeout"), 5e3);
387
396
  img.onload = () => {
388
397
  try {
389
398
  ctx.drawImage(img, 0, 0, width, height);
390
399
  if (isCanvasBlank(ctx, width, height)) {
391
- settle(null, "error");
400
+ settle(null, "unsupported");
392
401
  return;
393
402
  }
394
403
  settle(canvas.toDataURL("image/jpeg", 0.7));
@@ -396,7 +405,7 @@ function createScreenshotCapture(options = {}) {
396
405
  settle(null, "taint");
397
406
  }
398
407
  };
399
- img.onerror = () => settle(null, "error");
408
+ img.onerror = () => setTimeout(() => settle(null, cspBlocked ? "csp" : "error"), 0);
400
409
  img.src = url;
401
410
  });
402
411
  } catch {
@@ -418,16 +427,19 @@ function emitScreenshotFailed(reason) {
418
427
  } catch {
419
428
  }
420
429
  }
421
- var DEFAULT_REDACT_SELECTORS = [
422
- 'input[type="password"]',
423
- "[data-mushi-redact]"
424
- ];
430
+ var ALWAYS_REDACT_SELECTORS = 'input[type="password"],input[autocomplete^="cc-"],[data-private],[data-mushi-mask]';
431
+ var DEFAULT_REDACT_SELECTORS = ["[data-mushi-redact]"];
425
432
  function buildPrivacySafeDocument(privacy) {
426
433
  const clone = document.documentElement.cloneNode(true);
427
434
  clone.querySelector("#mushi-mushi-widget")?.remove();
428
435
  stripTaintSources(clone);
429
436
  inlineDocumentStyles(clone);
430
- const redactSelectors = privacy?.redactSelectors !== void 0 ? privacy.redactSelectors : DEFAULT_REDACT_SELECTORS;
437
+ freezeMotion(clone);
438
+ fillViewport(clone);
439
+ const redactSelectors = [
440
+ ALWAYS_REDACT_SELECTORS,
441
+ ...privacy?.redactSelectors ?? DEFAULT_REDACT_SELECTORS
442
+ ];
431
443
  for (const selector of redactSelectors) {
432
444
  for (const el of safeQueryAll(clone, selector)) {
433
445
  redactElement(el);
@@ -513,6 +525,20 @@ function inlineDocumentStyles(clone) {
513
525
  styleEl.textContent = css;
514
526
  (clone.querySelector("head") ?? clone).appendChild(styleEl);
515
527
  }
528
+ var FREEZE_MOTION_CSS = "*,*::before,*::after{animation:none!important;transition:none!important}";
529
+ function fillViewport(clone) {
530
+ const clear = (c) => !c || c === "transparent" || /^rgba\(.*,\s*0\)$/.test(c);
531
+ let bg = getComputedStyle(document.documentElement).backgroundColor;
532
+ if (clear(bg) && document.body) bg = getComputedStyle(document.body).backgroundColor;
533
+ const root = clone;
534
+ root.style.setProperty("background-color", clear(bg) ? "Canvas" : bg);
535
+ root.style.setProperty("min-height", `${window.innerHeight}px`);
536
+ }
537
+ function freezeMotion(clone) {
538
+ const styleEl = document.createElement("style");
539
+ styleEl.textContent = FREEZE_MOTION_CSS;
540
+ (clone.querySelector("head") ?? clone).appendChild(styleEl);
541
+ }
516
542
  function serializeSheet(sheet) {
517
543
  let rules;
518
544
  try {