-
Notifications
You must be signed in to change notification settings - Fork 141
/
observer.ts
418 lines (379 loc) · 13.1 KB
/
observer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import type { DefaultPrivacyLevel } from '@datadog/browser-core'
import {
instrumentSetter,
instrumentMethodAndCallOriginal,
assign,
monitor,
throttle,
DOM_EVENT,
addEventListeners,
addEventListener,
noop,
} from '@datadog/browser-core'
import { NodePrivacyLevel } from '../../constants'
import type {
InputState,
MousePosition,
MouseInteraction,
MutationPayload,
ScrollPosition,
StyleSheetRule,
ViewportResizeDimension,
MediaInteraction,
FocusRecord,
VisualViewportRecord,
} from '../../types'
import { IncrementalSource, MediaInteractionType, MouseInteractionType } from '../../types'
import { getNodePrivacyLevel, shouldMaskNode } from './privacy'
import { getElementInputValue, getSerializedNodeId, hasSerializedNode } from './serializationUtils'
import { forEach, isTouchEvent } from './utils'
import type { MutationController } from './mutationObserver'
import { startMutationObserver } from './mutationObserver'
import {
getVisualViewport,
getWindowHeight,
getWindowWidth,
getScrollX,
getScrollY,
convertMouseEventToLayoutCoordinates,
} from './viewports'
const MOUSE_MOVE_OBSERVER_THRESHOLD = 50
const SCROLL_OBSERVER_THRESHOLD = 100
const VISUAL_VIEWPORT_OBSERVER_THRESHOLD = 200
type ListenerHandler = () => void
type MousemoveCallBack = (
p: MousePosition[],
source: typeof IncrementalSource.MouseMove | typeof IncrementalSource.TouchMove
) => void
export type MutationCallBack = (m: MutationPayload) => void
type MouseInteractionCallBack = (d: MouseInteraction) => void
type ScrollCallback = (p: ScrollPosition) => void
type StyleSheetRuleCallback = (s: StyleSheetRule) => void
type ViewportResizeCallback = (d: ViewportResizeDimension) => void
export type InputCallback = (v: InputState & { id: number }) => void
type MediaInteractionCallback = (p: MediaInteraction) => void
type FocusCallback = (data: FocusRecord['data']) => void
type VisualViewportResizeCallback = (data: VisualViewportRecord['data']) => void
interface ObserverParam {
defaultPrivacyLevel: DefaultPrivacyLevel
mutationController: MutationController
mutationCb: MutationCallBack
mousemoveCb: MousemoveCallBack
mouseInteractionCb: MouseInteractionCallBack
scrollCb: ScrollCallback
viewportResizeCb: ViewportResizeCallback
visualViewportResizeCb: VisualViewportResizeCallback
inputCb: InputCallback
mediaInteractionCb: MediaInteractionCallback
styleSheetRuleCb: StyleSheetRuleCallback
focusCb: FocusCallback
}
export function initObservers(o: ObserverParam): ListenerHandler {
const mutationHandler = initMutationObserver(o.mutationController, o.mutationCb, o.defaultPrivacyLevel)
const mousemoveHandler = initMoveObserver(o.mousemoveCb)
const mouseInteractionHandler = initMouseInteractionObserver(o.mouseInteractionCb, o.defaultPrivacyLevel)
const scrollHandler = initScrollObserver(o.scrollCb, o.defaultPrivacyLevel)
const viewportResizeHandler = initViewportResizeObserver(o.viewportResizeCb)
const inputHandler = initInputObserver(o.inputCb, o.defaultPrivacyLevel)
const mediaInteractionHandler = initMediaInteractionObserver(o.mediaInteractionCb, o.defaultPrivacyLevel)
const styleSheetObserver = initStyleSheetObserver(o.styleSheetRuleCb)
const focusHandler = initFocusObserver(o.focusCb)
const visualViewportResizeHandler = initVisualViewportResizeObserver(o.visualViewportResizeCb)
return () => {
mutationHandler()
mousemoveHandler()
mouseInteractionHandler()
scrollHandler()
viewportResizeHandler()
inputHandler()
mediaInteractionHandler()
styleSheetObserver()
focusHandler()
visualViewportResizeHandler()
}
}
function initMutationObserver(
mutationController: MutationController,
cb: MutationCallBack,
defaultPrivacyLevel: DefaultPrivacyLevel
) {
return startMutationObserver(mutationController, cb, defaultPrivacyLevel).stop
}
function initMoveObserver(cb: MousemoveCallBack): ListenerHandler {
const { throttled: updatePosition } = throttle(
monitor((event: MouseEvent | TouchEvent) => {
const target = event.target as Node
if (hasSerializedNode(target)) {
const { clientX, clientY } = isTouchEvent(event) ? event.changedTouches[0] : event
const position: MousePosition = {
id: getSerializedNodeId(target),
timeOffset: 0,
x: clientX,
y: clientY,
}
if (window.visualViewport) {
const { visualViewportX, visualViewportY } = convertMouseEventToLayoutCoordinates(clientX, clientY)
position.x = visualViewportX
position.y = visualViewportY
}
cb([position], isTouchEvent(event) ? IncrementalSource.TouchMove : IncrementalSource.MouseMove)
}
}),
MOUSE_MOVE_OBSERVER_THRESHOLD,
{
trailing: false,
}
)
return addEventListeners(document, [DOM_EVENT.MOUSE_MOVE, DOM_EVENT.TOUCH_MOVE], updatePosition, {
capture: true,
passive: true,
}).stop
}
const eventTypeToMouseInteraction = {
[DOM_EVENT.MOUSE_UP]: MouseInteractionType.MouseUp,
[DOM_EVENT.MOUSE_DOWN]: MouseInteractionType.MouseDown,
[DOM_EVENT.CLICK]: MouseInteractionType.Click,
[DOM_EVENT.CONTEXT_MENU]: MouseInteractionType.ContextMenu,
[DOM_EVENT.DBL_CLICK]: MouseInteractionType.DblClick,
[DOM_EVENT.FOCUS]: MouseInteractionType.Focus,
[DOM_EVENT.BLUR]: MouseInteractionType.Blur,
[DOM_EVENT.TOUCH_START]: MouseInteractionType.TouchStart,
[DOM_EVENT.TOUCH_END]: MouseInteractionType.TouchEnd,
}
function initMouseInteractionObserver(
cb: MouseInteractionCallBack,
defaultPrivacyLevel: DefaultPrivacyLevel
): ListenerHandler {
const handler = (event: MouseEvent | TouchEvent) => {
const target = event.target as Node
if (getNodePrivacyLevel(target, defaultPrivacyLevel) === NodePrivacyLevel.HIDDEN || !hasSerializedNode(target)) {
return
}
const { clientX, clientY } = isTouchEvent(event) ? event.changedTouches[0] : event
const position: MouseInteraction = {
id: getSerializedNodeId(target),
type: eventTypeToMouseInteraction[event.type as keyof typeof eventTypeToMouseInteraction],
x: clientX,
y: clientY,
}
if (window.visualViewport) {
const { visualViewportX, visualViewportY } = convertMouseEventToLayoutCoordinates(clientX, clientY)
position.x = visualViewportX
position.y = visualViewportY
}
cb(position)
}
return addEventListeners(document, Object.keys(eventTypeToMouseInteraction) as DOM_EVENT[], handler, {
capture: true,
passive: true,
}).stop
}
function initScrollObserver(cb: ScrollCallback, defaultPrivacyLevel: DefaultPrivacyLevel): ListenerHandler {
const { throttled: updatePosition } = throttle(
monitor((event: UIEvent) => {
const target = event.target as HTMLElement | Document
if (
!target ||
getNodePrivacyLevel(target, defaultPrivacyLevel) === NodePrivacyLevel.HIDDEN ||
!hasSerializedNode(target)
) {
return
}
const id = getSerializedNodeId(target)
if (target === document) {
cb({
id,
x: getScrollX(),
y: getScrollY(),
})
} else {
cb({
id,
x: (target as HTMLElement).scrollLeft,
y: (target as HTMLElement).scrollTop,
})
}
}),
SCROLL_OBSERVER_THRESHOLD
)
return addEventListener(document, DOM_EVENT.SCROLL, updatePosition, { capture: true, passive: true }).stop
}
function initViewportResizeObserver(cb: ViewportResizeCallback): ListenerHandler {
const { throttled: updateDimension } = throttle(
monitor(() => {
const height = getWindowHeight()
const width = getWindowWidth()
cb({
height: Number(height),
width: Number(width),
})
}),
200
)
return addEventListener(window, DOM_EVENT.RESIZE, updateDimension, { capture: true, passive: true }).stop
}
export function initInputObserver(cb: InputCallback, defaultPrivacyLevel: DefaultPrivacyLevel): ListenerHandler {
const lastInputStateMap: WeakMap<Node, InputState> = new WeakMap()
function onElementChange(target: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement) {
const nodePrivacyLevel = getNodePrivacyLevel(target, defaultPrivacyLevel)
if (nodePrivacyLevel === NodePrivacyLevel.HIDDEN) {
return
}
const type = target.type
let inputState: InputState
if (type === 'radio' || type === 'checkbox') {
if (shouldMaskNode(target, nodePrivacyLevel)) {
return
}
inputState = { isChecked: (target as HTMLInputElement).checked }
} else {
const value = getElementInputValue(target, nodePrivacyLevel)
if (value === undefined) {
return
}
inputState = { text: value }
}
// Can be multiple changes on the same node within the same batched mutation observation.
cbWithDedup(target, inputState)
// If a radio was checked, other radios with the same name attribute will be unchecked.
const name = target.name
if (type === 'radio' && name && (target as HTMLInputElement).checked) {
forEach(document.querySelectorAll(`input[type="radio"][name="${name}"]`), (el: Element) => {
if (el !== target) {
// TODO: Consider the privacy implications for various differing input privacy levels
cbWithDedup(el, { isChecked: false })
}
})
}
}
/**
* There can be multiple changes on the same node within the same batched mutation observation.
*/
function cbWithDedup(target: Node, inputState: InputState) {
if (!hasSerializedNode(target)) {
return
}
const lastInputState = lastInputStateMap.get(target)
if (
!lastInputState ||
(lastInputState as { text?: string }).text !== (inputState as { text?: string }).text ||
(lastInputState as { isChecked?: boolean }).isChecked !== (inputState as { isChecked?: boolean }).isChecked
) {
lastInputStateMap.set(target, inputState)
cb(
assign(
{
id: getSerializedNodeId(target),
},
inputState
)
)
}
}
const { stop: stopEventListeners } = addEventListeners(
document,
[DOM_EVENT.INPUT, DOM_EVENT.CHANGE],
(event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLTextAreaElement ||
event.target instanceof HTMLSelectElement
) {
onElementChange(event.target)
}
},
{
capture: true,
passive: true,
}
)
const instrumentationStoppers = [
instrumentSetter(HTMLInputElement.prototype, 'value', onElementChange),
instrumentSetter(HTMLInputElement.prototype, 'checked', onElementChange),
instrumentSetter(HTMLSelectElement.prototype, 'value', onElementChange),
instrumentSetter(HTMLTextAreaElement.prototype, 'value', onElementChange),
instrumentSetter(HTMLSelectElement.prototype, 'selectedIndex', onElementChange),
]
return () => {
instrumentationStoppers.forEach((stopper) => stopper.stop())
stopEventListeners()
}
}
function initStyleSheetObserver(cb: StyleSheetRuleCallback): ListenerHandler {
const { stop: restoreInsertRule } = instrumentMethodAndCallOriginal(CSSStyleSheet.prototype, 'insertRule', {
before(rule, index) {
if (hasSerializedNode(this.ownerNode!)) {
cb({
id: getSerializedNodeId(this.ownerNode),
adds: [{ rule, index }],
})
}
},
})
const { stop: restoreDeleteRule } = instrumentMethodAndCallOriginal(CSSStyleSheet.prototype, 'deleteRule', {
before(index) {
if (hasSerializedNode(this.ownerNode!)) {
cb({
id: getSerializedNodeId(this.ownerNode),
removes: [{ index }],
})
}
},
})
return () => {
restoreInsertRule()
restoreDeleteRule()
}
}
function initMediaInteractionObserver(
mediaInteractionCb: MediaInteractionCallback,
defaultPrivacyLevel: DefaultPrivacyLevel
): ListenerHandler {
const handler = (event: Event) => {
const target = event.target as Node
if (
!target ||
getNodePrivacyLevel(target, defaultPrivacyLevel) === NodePrivacyLevel.HIDDEN ||
!hasSerializedNode(target)
) {
return
}
mediaInteractionCb({
id: getSerializedNodeId(target),
type: event.type === DOM_EVENT.PLAY ? MediaInteractionType.Play : MediaInteractionType.Pause,
})
}
return addEventListeners(document, [DOM_EVENT.PLAY, DOM_EVENT.PAUSE], handler, { capture: true, passive: true }).stop
}
function initFocusObserver(focusCb: FocusCallback): ListenerHandler {
return addEventListeners(window, [DOM_EVENT.FOCUS, DOM_EVENT.BLUR], () => {
focusCb({ has_focus: document.hasFocus() })
}).stop
}
function initVisualViewportResizeObserver(cb: VisualViewportResizeCallback): ListenerHandler {
if (!window.visualViewport) {
return noop
}
const { throttled: updateDimension, cancel: cancelThrottle } = throttle(
monitor(() => {
cb(getVisualViewport())
}),
VISUAL_VIEWPORT_OBSERVER_THRESHOLD,
{
trailing: false,
}
)
const removeListener = addEventListeners(
window.visualViewport,
[DOM_EVENT.RESIZE, DOM_EVENT.SCROLL],
updateDimension,
{
capture: true,
passive: true,
}
).stop
return function stop() {
removeListener()
cancelThrottle()
}
}