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