UNPKG

@sentry/browser

Version:
62 lines (61 loc) • 2.31 kB
const require_debug_build = require("./debug-build.js"); const require_helpers = require("./helpers.js"); let _sentry_core = require("@sentry/core"); // src/report-dialog.ts /** * Present the user with a report dialog. * * @param options Everything is optional, we try to fetch all info need from the current scope. */ function showReportDialog(options = {}) { const optionalDocument = require_helpers.WINDOW.document; const injectionPoint = optionalDocument?.head || optionalDocument?.body; if (!injectionPoint) { require_debug_build.DEBUG_BUILD && _sentry_core.debug.error("[showReportDialog] Global document not defined"); return; } const scope = (0, _sentry_core.getCurrentScope)(); const dsn = (0, _sentry_core.getClient)()?.getDsn(); if (!dsn) { require_debug_build.DEBUG_BUILD && _sentry_core.debug.error("[showReportDialog] DSN not configured"); return; } const mergedOptions = { ...options, user: { ...scope.getUser(), ...options.user }, eventId: options.eventId || (0, _sentry_core.lastEventId)() }; const { onLoad, onClose, onError } = mergedOptions; if (!mergedOptions.eventId) { require_debug_build.DEBUG_BUILD && _sentry_core.debug.error("[showReportDialog] No event ID"); onError?.(/* @__PURE__ */ new Error("No event ID to show the report dialog for")); return; } const script = require_helpers.WINDOW.document.createElement("script"); script.async = true; script.crossOrigin = "anonymous"; script.src = (0, _sentry_core.getReportDialogEndpoint)(dsn, mergedOptions); if (onLoad) script.onload = onLoad; if (onError) script.onerror = () => { onError(/* @__PURE__ */ new Error("Failed to load the report dialog script")); }; if (onClose) { const reportDialogClosedMessageHandler = (event) => { if (event.data === "__sentry_reportdialog_closed__") try { onClose(); } finally { require_helpers.WINDOW.removeEventListener("message", reportDialogClosedMessageHandler); } }; require_helpers.WINDOW.addEventListener("message", reportDialogClosedMessageHandler); script.addEventListener("error", () => { require_helpers.WINDOW.removeEventListener("message", reportDialogClosedMessageHandler); }); } injectionPoint.appendChild(script); } exports.showReportDialog = showReportDialog; //# sourceMappingURL=report-dialog.js.map