@botim/mp-debug-sdk 0.1.0 → 0.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/index.js CHANGED
@@ -772,32 +772,95 @@ async function defaultDomScreenshot() {
772
772
  return await captureViaHtmlSnapshot();
773
773
  }
774
774
  }
775
- async function captureViaSvgForeignObject() {
776
- const dpr = Math.min(window.devicePixelRatio || 1, 2);
777
- const w = Math.max(1, Math.min(window.innerWidth || document.documentElement.clientWidth || 1024, 2400));
778
- const h = Math.max(1, Math.min(document.documentElement.scrollHeight, 4e3));
779
- const inlinedCss = readInlineableStyles();
780
- const clone = document.documentElement.cloneNode(true);
781
- clone.querySelectorAll('link[rel="stylesheet"]').forEach((el) => el.remove());
782
- clone.querySelectorAll("script,noscript,template").forEach((el) => el.remove());
783
- clone.querySelectorAll("canvas").forEach((el) => el.remove());
784
- clone.querySelectorAll("img").forEach((img) => {
785
- const src = img.getAttribute("src") || "";
786
- if (src && /^(https?:|\/\/)/i.test(src)) {
775
+ var VOID_ELEMENTS = /* @__PURE__ */ new Set([
776
+ "area",
777
+ "base",
778
+ "br",
779
+ "col",
780
+ "embed",
781
+ "hr",
782
+ "img",
783
+ "input",
784
+ "link",
785
+ "meta",
786
+ "source",
787
+ "track",
788
+ "wbr"
789
+ ]);
790
+ var SKIP_TAGS = /* @__PURE__ */ new Set(["script", "noscript", "template"]);
791
+ var DEVTOOL_OVERLAY_PREFIXES = ["vite-", "astro-dev-", "next-route-"];
792
+ function isDevtoolOverlay(tag) {
793
+ return DEVTOOL_OVERLAY_PREFIXES.some((p) => tag.startsWith(p));
794
+ }
795
+ function escapeText(s) {
796
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
797
+ }
798
+ function escapeAttr(s) {
799
+ return s.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
800
+ }
801
+ function serializeComposed(node, opts, depth) {
802
+ if (depth > 256) return "";
803
+ if (node.nodeType === Node.TEXT_NODE) {
804
+ return escapeText(node.textContent ?? "");
805
+ }
806
+ if (node.nodeType === Node.COMMENT_NODE) return "";
807
+ if (node.nodeType !== Node.ELEMENT_NODE) return "";
808
+ const el = node;
809
+ const tag = el.tagName.toLowerCase();
810
+ if (SKIP_TAGS.has(tag)) return "";
811
+ if (isDevtoolOverlay(tag)) return "";
812
+ if (tag === "slot") {
813
+ const slot = el;
814
+ let assigned = [];
815
+ try {
816
+ assigned = slot.assignedNodes({ flatten: true });
817
+ } catch {
818
+ }
819
+ const source = assigned.length > 0 ? assigned : Array.from(el.childNodes);
820
+ return source.map((c) => serializeComposed(c, opts, depth + 1)).join("");
821
+ }
822
+ if (tag === "link" && (el.getAttribute("rel") || "").toLowerCase() === "stylesheet") {
823
+ return "";
824
+ }
825
+ if (tag === "style") {
826
+ return `<style>${el.textContent ?? ""}</style>`;
827
+ }
828
+ if (tag === "img" && opts.stripCrossOriginImages) {
829
+ const src = el.getAttribute("src") || "";
830
+ if (/^(https?:|\/\/)/i.test(src)) {
787
831
  try {
788
832
  const u = new URL(src, location.href);
789
- if (u.origin !== location.origin) img.remove();
833
+ if (u.origin !== location.origin) return "";
790
834
  } catch {
791
- img.remove();
835
+ return "";
792
836
  }
793
837
  }
794
- });
795
- clone.querySelectorAll("*").forEach((el) => {
796
- if (el.tagName.includes("-")) el.remove();
797
- });
798
- clone.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
799
- const xs = new XMLSerializer();
800
- const htmlStr = xs.serializeToString(clone);
838
+ }
839
+ if (tag === "canvas") return `<canvas></canvas>`;
840
+ const attrs = Array.from(el.attributes).filter((a) => !a.name.startsWith("on")).map((a) => ` ${a.name}="${escapeAttr(a.value)}"`).join("");
841
+ if (VOID_ELEMENTS.has(tag)) return `<${tag}${attrs}>`;
842
+ const childSource = el.shadowRoot ?? el;
843
+ const inner = Array.from(childSource.childNodes).map((c) => serializeComposed(c, opts, depth + 1)).join("");
844
+ return `<${tag}${attrs}>${inner}</${tag}>`;
845
+ }
846
+ function readInlineableStyles() {
847
+ const chunks = [];
848
+ for (const sheet of Array.from(document.styleSheets)) {
849
+ try {
850
+ const rules = sheet.cssRules;
851
+ for (const rule of Array.from(rules)) chunks.push(rule.cssText);
852
+ } catch {
853
+ }
854
+ }
855
+ return chunks.join("\n");
856
+ }
857
+ async function captureViaSvgForeignObject() {
858
+ const dpr = Math.min(window.devicePixelRatio || 1, 2);
859
+ const w = Math.max(1, Math.min(window.innerWidth || document.documentElement.clientWidth || 1024, 2400));
860
+ const h = Math.max(1, Math.min(document.documentElement.scrollHeight, 4e3));
861
+ const inlinedCss = readInlineableStyles();
862
+ const bodyHtml = serializeComposed(document.body, { stripCrossOriginImages: true }, 0);
863
+ const htmlStr = `<html xmlns="http://www.w3.org/1999/xhtml"><body>${bodyHtml}</body></html>`;
801
864
  const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}"><foreignObject width="100%" height="100%">` + (inlinedCss ? `<style xmlns="http://www.w3.org/1999/xhtml">${escapeForXml(inlinedCss)}</style>` : "") + htmlStr + `</foreignObject></svg>`;
802
865
  const blobUrl = URL.createObjectURL(
803
866
  new Blob([svg], { type: "image/svg+xml;charset=utf-8" })
@@ -831,30 +894,19 @@ function loadImage(src) {
831
894
  function escapeForXml(s) {
832
895
  return s.replace(/]]>/g, "]]&gt;").replace(/<\//g, "&lt;/");
833
896
  }
834
- function readInlineableStyles() {
835
- const chunks = [];
836
- for (const sheet of Array.from(document.styleSheets)) {
837
- try {
838
- const rules = sheet.cssRules;
839
- for (const rule of Array.from(rules)) chunks.push(rule.cssText);
840
- } catch {
841
- }
842
- }
843
- return chunks.join("\n");
844
- }
845
897
  async function captureViaHtmlSnapshot() {
846
898
  const inlinedCss = readInlineableStyles();
847
- const clone = document.documentElement.cloneNode(true);
848
- clone.querySelectorAll('link[rel="stylesheet"]').forEach((el) => el.remove());
849
- clone.querySelectorAll("script").forEach((el) => el.remove());
850
- const head = clone.querySelector("head");
851
- if (head && inlinedCss) {
852
- const styleEl = document.createElement("style");
853
- styleEl.setAttribute("data-botim-snapshot", "1");
854
- styleEl.textContent = inlinedCss;
855
- head.insertBefore(styleEl, head.firstChild);
856
- }
857
- const html = "<!DOCTYPE html>\n" + clone.outerHTML;
899
+ const bodyHtml = serializeComposed(document.body, { stripCrossOriginImages: false }, 0);
900
+ const html = `<!DOCTYPE html>
901
+ <html lang="en">
902
+ <head>
903
+ <meta charset="utf-8">
904
+ <meta name="viewport" content="width=device-width,initial-scale=1">
905
+ <base href="${escapeAttr(location.href)}">
906
+ ` + (inlinedCss ? `<style data-botim-snapshot="1">${inlinedCss}</style>
907
+ ` : "") + `</head>
908
+ <body>${bodyHtml}</body>
909
+ </html>`;
858
910
  return {
859
911
  data: JSON.stringify({
860
912
  html,