import { isTrustedSessionReplayIframeParentOrigin, SESSION_REPLAY_IFRAME_PROBE, SESSION_REPLAY_IFRAME_START, SESSION_REPLAY_IFRAME_STOP, } from "../session-replay-iframe-protocol.js"; export const RRWEB_RECORD_IFRAME_CDN_URL = "https://cdn.jsdelivr.net/npm/@rrweb/record@2.1.0/umd/record.min.js"; export const RRWEB_RECORD_IFRAME_SRI = "sha384-MrD66HBNSykaP2N95+6hQCFlF5oH2tvL3TD/zyvHNkP/sAFWZx98DX9MEDy8MdVT"; /** * Installs the cooperative side of rrweb's cross-origin iframe protocol. * Nothing is fetched or recorded until a trusted first-party parent asks the * frame to start. rrweb then forwards its events directly to that parent. */ export function buildSessionReplayIframeBootstrap(): string { const probeType = JSON.stringify(SESSION_REPLAY_IFRAME_PROBE); const startType = JSON.stringify(SESSION_REPLAY_IFRAME_START); const stopType = JSON.stringify(SESSION_REPLAY_IFRAME_STOP); const scriptUrl = JSON.stringify(RRWEB_RECORD_IFRAME_CDN_URL); const integrity = JSON.stringify(RRWEB_RECORD_IFRAME_SRI); return ``; } export function injectSessionReplayIframeBootstrap(html: string): string { const bootstrap = buildSessionReplayIframeBootstrap(); const headClose = html.search(/<\/head\s*>/i); if (headClose >= 0) { return `${html.slice(0, headClose)}${bootstrap}${html.slice(headClose)}`; } const bodyOpen = html.search(/]*)?>/i); if (bodyOpen >= 0) { return `${html.slice(0, bodyOpen)}${bootstrap}${html.slice(bodyOpen)}`; } return `${bootstrap}${html}`; }