Skip to content

Commit

Permalink
catch calls to iframe.contentDocument in rrweb-snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
chargome committed Nov 15, 2024
1 parent e77deea commit d6cc9ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
shouldMaskInput,
setTimeout,
clearTimeout,
getIframeContentDocument,
} from './utils';

let _id = 1;
Expand Down Expand Up @@ -1046,7 +1047,7 @@ function serializeElementNode(
if (tagName === 'iframe' && !keepIframeSrcFn(attributes.src as string)) {
// Don't try to access `contentDocument` if iframe is blocked, otherwise it
// will trigger browser warnings.
if (!needBlock && !(n as HTMLIFrameElement).contentDocument) {
if (!needBlock && !getIframeContentDocument(n as HTMLIFrameElement)) {
// we can't record it directly as we can't see into it
// preserve the src attribute so a decision can be taken at replay time
attributes.rr_src = attributes.src;
Expand Down Expand Up @@ -1384,7 +1385,7 @@ export function serializeNodeWithId(
onceIframeLoaded(
n as HTMLIFrameElement,
() => {
const iframeDoc = (n as HTMLIFrameElement).contentDocument;
const iframeDoc = getIframeContentDocument(n as HTMLIFrameElement);
if (iframeDoc && onIframeLoad) {
const serializedIframeNode = serializeNodeWithId(iframeDoc, {
doc: iframeDoc,
Expand Down
14 changes: 14 additions & 0 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,17 @@ export function clearTimeout(
): ReturnType<typeof window.clearTimeout> {
return getImplementation('clearTimeout')(...rest);
}

/**
* Get the content document of an iframe.
* Catching errors is necessary because some older browsers block access to the content document of a sandboxed iframe.
*/
export function getIframeContentDocument(iframe?: HTMLIFrameElement) {
try {
if (iframe) {
return iframe.contentDocument;
}
} catch (e) {
// noop
}
}

0 comments on commit d6cc9ad

Please sign in to comment.