Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rrweb sends message to cross-origin iframe to force snapshot #1465

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rotten-squids-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb': patch
---

rrweb sends message to cross-origin iframe to force snapshot
32 changes: 30 additions & 2 deletions packages/rrweb/src/record/iframe-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class IframeManager {
private mirror: Mirror;
private mutationCb: mutationCallBack;
private wrappedEmit: (e: eventWithoutTime, isCheckout?: boolean) => void;
private takeFullSnapshot: (isCheckout?: boolean) => void;
private loadListener?: (iframeEl: HTMLIFrameElement) => unknown;
private stylesheetManager: StylesheetManager;
private recordCrossOriginIframes: boolean;
Expand All @@ -31,9 +32,11 @@ export class IframeManager {
stylesheetManager: StylesheetManager;
recordCrossOriginIframes: boolean;
wrappedEmit: (e: eventWithoutTime, isCheckout?: boolean) => void;
takeFullSnapshot: (isCheckout?: boolean) => void;
}) {
this.mutationCb = options.mutationCb;
this.wrappedEmit = options.wrappedEmit;
this.takeFullSnapshot = options.takeFullSnapshot;
this.stylesheetManager = options.stylesheetManager;
this.recordCrossOriginIframes = options.recordCrossOriginIframes;
this.crossOriginIframeStyleMirror = new CrossOriginIframeMirror(
Expand All @@ -47,10 +50,24 @@ export class IframeManager {
}
}

public setTakeFullSnapshot(takeFullSnapshot: (isCheckout?: boolean) => void) {
this.takeFullSnapshot = takeFullSnapshot;
}

public addIframe(iframeEl: HTMLIFrameElement) {
this.iframes.set(iframeEl, true);
if (iframeEl.contentWindow)
this.crossOriginIframeMap.set(iframeEl.contentWindow, iframeEl);

if (!iframeEl.contentDocument && iframeEl.contentWindow)
iframeEl.contentWindow.postMessage(
{
type: 'rrweb',
origin: window.location.origin,
snapshot: true,
},
'*',
);
}

public addLoadListener(cb: (iframeEl: HTMLIFrameElement) => unknown) {
Expand Down Expand Up @@ -95,10 +112,21 @@ export class IframeManager {
)
return;

const iframeSourceWindow = message.source;
const iframeSourceWindow = crossOriginMessageEvent.source;
if (!iframeSourceWindow) return;

const iframeEl = this.crossOriginIframeMap.get(message.source);
if (
iframeSourceWindow == window.parent &&
window != window.parent &&
crossOriginMessageEvent.data.snapshot
) {
this.takeFullSnapshot();
return;
}

const iframeEl = this.crossOriginIframeMap.get(
crossOriginMessageEvent.source,
);
if (!iframeEl) return;

const transformedEvent = this.transformCrossOriginEvent(
Expand Down
7 changes: 5 additions & 2 deletions packages/rrweb/src/record/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
} from './error-handler';

let wrappedEmit!: (e: eventWithoutTime, isCheckout?: boolean) => void;

let takeFullSnapshot!: (isCheckout?: boolean) => void;
let takeFullSnapshot: (isCheckout?: boolean) => void = () => {
/* no-op */
};
let canvasManager!: CanvasManager;
let recording = false;

Expand Down Expand Up @@ -278,6 +279,7 @@
stylesheetManager: stylesheetManager,
recordCrossOriginIframes,
wrappedEmit,
takeFullSnapshot,
});

/**
Expand Down Expand Up @@ -411,6 +413,7 @@
mirror.getId(document),
);
};
iframeManager.setTakeFullSnapshot(takeFullSnapshot);

try {
const handlers: listenerHandler[] = [];
Expand Down Expand Up @@ -534,7 +537,7 @@
plugins
?.filter((p) => p.observer)
?.map((p) => ({
observer: p.observer!,

Check warning on line 540 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/index.ts#L540

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
options: p.options,
callback: (payload: object) =>
wrappedEmit({
Expand All @@ -552,7 +555,7 @@

iframeManager.addLoadListener((iframeEl) => {
try {
handlers.push(observe(iframeEl.contentDocument!));

Check warning on line 558 in packages/rrweb/src/record/index.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/index.ts#L558

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
} catch (error) {
// TODO: handle internal error
console.warn(error);
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export type CrossOriginIframeMessageEventContent<T = eventWithTime> = {
// The origin of the iframe which originally emits this message. It is used to check the integrity of message and to filter out the rrweb messages which are forwarded by some sites.
origin: string;
isCheckout?: boolean;
snapshot?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note is a reminder that we need to add a test

};
export type CrossOriginIframeMessageEvent =
MessageEvent<CrossOriginIframeMessageEventContent>;
Expand Down
Loading