UNPKG

@sentry/browser

Version:
71 lines (70 loc) • 2.22 kB
const require_helpers = require("../helpers.js"); let _sentry_core = require("@sentry/core"); let _sentry_browser_utils = require("@sentry/browser-utils"); // src/integrations/view-hierarchy.ts /** * An integration to include a view hierarchy attachment which contains the DOM. */ const viewHierarchyIntegration = (0, _sentry_core.defineIntegration)((options = {}) => { const skipHtmlTags = ["script"]; /** Walk an element */ function walk(element, windows, depth = 0) { if (!element) return; const children = "shadowRoot" in element && element.shadowRoot ? element.shadowRoot.children : element.children; for (const child of children) { if (!(child instanceof HTMLElement)) continue; const componentName = (0, _sentry_browser_utils.getComponentName)(child, 1) || void 0; const tagName = child.tagName.toLowerCase(); if (skipHtmlTags.includes(tagName)) continue; const result = options.onElement?.({ element: child, componentName, tagName, depth }) || {}; if (result === "skip") continue; if (result === "children") { walk(child, windows, depth + 1); continue; } const { x, y, width, height } = child.getBoundingClientRect(); const window = { identifier: child.id || void 0, type: componentName || tagName, visible: true, alpha: 1, height, width, x, y, ...result }; window.children = []; walk(child, window.children, depth + 1); windows.push(window); } } return { name: "ViewHierarchy", processEvent: (event, hint) => { if (event.type !== void 0 || options.shouldAttach?.(event, hint) === false) return event; const root = { rendering_system: "DOM", positioning: "absolute", windows: [] }; walk(options.rootElement?.() || require_helpers.WINDOW.document.body, root.windows); const attachment = { filename: "view-hierarchy.json", attachmentType: "event.view_hierarchy", contentType: "application/json", data: JSON.stringify(root) }; hint.attachments = hint.attachments || []; hint.attachments.push(attachment); return event; } }; }); exports.viewHierarchyIntegration = viewHierarchyIntegration; //# sourceMappingURL=view-hierarchy.js.map