diff --git a/src/record/index.ts b/src/record/index.ts index d4873f60..8823a486 100644 --- a/src/record/index.ts +++ b/src/record/index.ts @@ -1,6 +1,6 @@ import { snapshot } from 'rrweb-snapshot'; import initObservers from './observer'; -import { mirror, on } from '../utils'; +import { mirror, on, getWindowWidth, getWindowHeight } from '../utils'; import { EventType, event, @@ -36,7 +36,12 @@ function record(options: recordOptions = {}) { on( 'load', () => { - emit(wrapEvent({ type: EventType.Load, data: {} })); + emit( + wrapEvent({ + type: EventType.Load, + data: { width: getWindowWidth(), height: getWindowHeight() }, + }), + ); const [node, idNodeMap] = snapshot(document); if (!node) { return console.warn('Failed to snapshot the document'); diff --git a/src/record/observer.ts b/src/record/observer.ts index 0f07fd2b..9c33eaa1 100644 --- a/src/record/observer.ts +++ b/src/record/observer.ts @@ -1,5 +1,12 @@ import { INode, serializeNodeWithId } from 'rrweb-snapshot'; -import { mirror, throttle, on, hookSetter } from '../utils'; +import { + mirror, + throttle, + on, + hookSetter, + getWindowHeight, + getWindowWidth, +} from '../utils'; import { mutationCallBack, textMutation, @@ -183,14 +190,14 @@ function initScrollObserver(cb: scrollCallback): listenerHandler { if (evt.target === document) { cb({ id, - x: document.documentElement.scrollTop, - y: document.documentElement.scrollLeft, + x: document.documentElement.scrollLeft, + y: document.documentElement.scrollTop, }); } else { cb({ id, - x: (evt.target as HTMLElement).scrollTop, - y: (evt.target as HTMLElement).scrollLeft, + x: (evt.target as HTMLElement).scrollLeft, + y: (evt.target as HTMLElement).scrollTop, }); } }, 100); @@ -201,14 +208,8 @@ function initViewportResizeObserver( cb: viewportResizeCallback, ): listenerHandler { const updateDimension = throttle(() => { - const height = - window.innerHeight || - (document.documentElement && document.documentElement.clientHeight) || - (document.body && document.body.clientHeight); - const width = - window.innerWidth || - (document.documentElement && document.documentElement.clientWidth) || - (document.body && document.body.clientWidth); + const height = getWindowHeight(); + const width = getWindowWidth(); cb({ width: Number(width), height: Number(height), diff --git a/src/types.ts b/src/types.ts index e7e12a7d..85b4021f 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,7 +16,10 @@ export type domContentLoadedEvent = { export type loadedEvent = { type: EventType.Load; - data: {}; + data: { + width: number; + height: number; + }; }; export type fullSnapshotEvent = { diff --git a/src/utils.ts b/src/utils.ts index 9539319a..03d3a508 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -79,3 +79,19 @@ export function hookSetter( }); return () => hookSetter(target, key, original || {}); } + +export function getWindowHeight(): number { + return ( + window.innerHeight || + (document.documentElement && document.documentElement.clientHeight) || + (document.body && document.body.clientHeight) + ); +} + +export function getWindowWidth(): number { + return ( + window.innerWidth || + (document.documentElement && document.documentElement.clientWidth) || + (document.body && document.body.clientWidth) + ); +}