Skip to content

Commit 136f8df

Browse files
committed
Realise that we'd need to be consistent about which document/window we are operating on if we are to use window.top
1 parent a84fe4f commit 136f8df

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

src/record/index.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import initObservers from './observer';
33
import {
44
mirror,
55
on,
6+
getTopWindow,
67
getWindowWidth,
78
getWindowHeight,
89
polyfill,
@@ -97,20 +98,21 @@ function record<T = eventWithTime>(
9798
};
9899

99100
function takeFullSnapshot(isCheckout = false) {
101+
const twindow = getTopWindow();
100102
wrappedEmit(
101103
wrapEvent({
102104
type: EventType.Meta,
103105
data: {
104-
href: window.location.href,
106+
href: twindow.location.href,
105107
width: getWindowWidth(),
106108
height: getWindowHeight(),
107109
},
108110
}),
109111
isCheckout,
110112
);
113+
111114
const [node, idNodeMap] = snapshot(
112-
window.top && window.top.document ?
113-
window.top.document : document,
115+
tdocument,
114116
blockClass,
115117
inlineStylesheet,
116118
maskInputOptions,
@@ -121,25 +123,27 @@ function record<T = eventWithTime>(
121123
}
122124

123125
mirror.map = idNodeMap;
126+
const twindow = getTopWindow();
127+
const tdoc = twindow.document;
124128
wrappedEmit(
125129
wrapEvent({
126130
type: EventType.FullSnapshot,
127131
data: {
128132
node,
129133
initialOffset: {
130134
left:
131-
window.pageXOffset !== undefined
132-
? window.pageXOffset
133-
: document?.documentElement.scrollLeft ||
134-
document?.body?.parentElement?.scrollLeft ||
135-
document?.body.scrollLeft ||
135+
twindow.pageXOffset !== undefined
136+
? twindow.pageXOffset
137+
: tdoc?.documentElement.scrollLeft ||
138+
tdoc?.body?.parentElement?.scrollLeft ||
139+
tdoc?.body.scrollLeft ||
136140
0,
137141
top:
138-
window.pageYOffset !== undefined
139-
? window.pageYOffset
140-
: document?.documentElement.scrollTop ||
141-
document?.body?.parentElement?.scrollTop ||
142-
document?.body.scrollTop ||
142+
twindow.pageYOffset !== undefined
143+
? twindow.pageYOffset
144+
: tdoc?.documentElement.scrollTop ||
145+
tdoc?.body?.parentElement?.scrollTop ||
146+
tdoc?.body.scrollTop ||
143147
0,
144148
},
145149
},
@@ -255,9 +259,10 @@ function record<T = eventWithTime>(
255259
),
256260
);
257261
};
262+
const tdoc = getTopWindow().document;
258263
if (
259-
document.readyState === 'interactive' ||
260-
document.readyState === 'complete'
264+
tdoc.readyState === 'interactive' ||
265+
tdoc.readyState === 'complete'
261266
) {
262267
init();
263268
} else {

src/record/observer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
throttle,
55
on,
66
hookSetter,
7+
getTopWindow,
78
getWindowHeight,
89
getWindowWidth,
910
isBlocked,
@@ -47,7 +48,8 @@ function initMutationObserver(
4748
maskInputOptions,
4849
);
4950
const observer = new MutationObserver(mutationBuffer.processMutations);
50-
observer.observe(document, {
51+
const twindow = getTopWindow();
52+
observer.observe(twindow.document, {
5153
attributes: true,
5254
attributeOldValue: true,
5355
characterData: true,
@@ -173,8 +175,9 @@ function initScrollObserver(
173175
return;
174176
}
175177
const id = mirror.getId(evt.target as INode);
176-
if (evt.target === document) {
177-
const scrollEl = (document.scrollingElement || document.documentElement)!;
178+
const tdoc = getTopWindow().document;
179+
if (evt.target === tdoc) {
180+
const scrollEl = (tdoc.scrollingElement || tdoc.documentElement)!;
178181
cb({
179182
id,
180183
x: scrollEl.scrollLeft,
@@ -247,8 +250,9 @@ function initInputObserver(
247250
// if a radio was checked
248251
// the other radios with the same name attribute will be unchecked.
249252
const name: string | undefined = (target as HTMLInputElement).name;
253+
const tdoc = getTopWindow().document;
250254
if (type === 'radio' && name && isChecked) {
251-
document
255+
tdoc
252256
.querySelectorAll(`input[type="radio"][name="${name}"]`)
253257
.forEach((el) => {
254258
if (el !== target) {

src/utils.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,37 @@ export function patch(
156156
};
157157
}
158158

159+
export function getTopWindow(): Window {
160+
let twindow = window;
161+
while (twindow.parent && twindow.parent != twindow) {
162+
// check each parent in case the window.top.document fails, but an intermediary one would succeed
163+
try {
164+
let tdoc = window.parent.document; // this can fail apparently: https://stackoverflow.com/questions/2937118
165+
twindow = window.parent;
166+
} catch (err) {
167+
break;
168+
}
169+
}
170+
return twindow;
171+
}
172+
159173
export function getWindowHeight(): number {
174+
const twindow = getTopWindow();
175+
const tdoc = twindow.document;
160176
return (
161-
window.innerHeight ||
162-
(document.documentElement && document.documentElement.clientHeight) ||
163-
(document.body && document.body.clientHeight)
177+
twindow.innerHeight ||
178+
(tdoc.documentElement && tdoc.documentElement.clientHeight) ||
179+
(tdoc.body && tdoc.body.clientHeight)
164180
);
165181
}
166182

167183
export function getWindowWidth(): number {
184+
const twindow = getTopWindow();
185+
const tdoc = twindow.document;
168186
return (
169-
window.innerWidth ||
170-
(document.documentElement && document.documentElement.clientWidth) ||
171-
(document.body && document.body.clientWidth)
187+
twindow.innerWidth ||
188+
(tdoc.documentElement && tdoc.documentElement.clientWidth) ||
189+
(tdoc.body && tdoc.body.clientWidth)
172190
);
173191
}
174192

0 commit comments

Comments
 (0)