@highlight-run/rrweb 1.2.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (185) hide show
  1. package/dist/plugins/console-record.js +655 -0
  2. package/dist/plugins/console-record.min.js +1 -1
  3. package/dist/plugins/console-record.min.js.map +1 -1
  4. package/dist/plugins/console-replay.js +293 -0
  5. package/dist/plugins/console-replay.min.js +16 -0
  6. package/dist/plugins/console-replay.min.js.map +1 -0
  7. package/dist/plugins/sequential-id-record.js +33 -0
  8. package/dist/plugins/sequential-id-record.min.js +2 -0
  9. package/dist/plugins/sequential-id-record.min.js.map +1 -0
  10. package/dist/plugins/sequential-id-replay.js +37 -0
  11. package/dist/plugins/sequential-id-replay.min.js +2 -0
  12. package/dist/plugins/sequential-id-replay.min.js.map +1 -0
  13. package/dist/record/rrweb-record-pack.js +747 -0
  14. package/dist/record/rrweb-record-pack.min.js +16 -0
  15. package/dist/record/rrweb-record-pack.min.js.map +1 -0
  16. package/dist/record/rrweb-record.js +3318 -0
  17. package/dist/record/rrweb-record.min.js +16 -0
  18. package/dist/record/rrweb-record.min.js.map +1 -0
  19. package/dist/replay/rrweb-replay-unpack.js +4626 -0
  20. package/dist/replay/rrweb-replay-unpack.min.css +2 -0
  21. package/dist/replay/rrweb-replay-unpack.min.css.map +1 -0
  22. package/dist/replay/rrweb-replay-unpack.min.js +30 -0
  23. package/dist/replay/rrweb-replay-unpack.min.js.map +1 -0
  24. package/dist/replay/rrweb-replay.js +4216 -0
  25. package/dist/replay/rrweb-replay.min.css +2 -0
  26. package/dist/replay/rrweb-replay.min.css.map +1 -0
  27. package/dist/replay/rrweb-replay.min.js +30 -0
  28. package/dist/replay/rrweb-replay.min.js.map +1 -0
  29. package/dist/rrweb-all.js +8708 -0
  30. package/dist/rrweb-all.min.css +2 -0
  31. package/dist/rrweb-all.min.css.map +1 -0
  32. package/dist/rrweb-all.min.js +30 -0
  33. package/dist/rrweb-all.min.js.map +1 -0
  34. package/dist/rrweb.js +7294 -0
  35. package/dist/rrweb.min.css +2 -0
  36. package/dist/rrweb.min.css.map +1 -0
  37. package/dist/rrweb.min.js +2 -2
  38. package/dist/rrweb.min.js.map +1 -1
  39. package/es/rrweb/ext/@xstate/fsm/es/index.js +17 -0
  40. package/es/rrweb/ext/base64-arraybuffer/dist/base64-arraybuffer.es5.js +49 -0
  41. package/es/rrweb/ext/fflate/esm/browser.js +777 -0
  42. package/es/rrweb/ext/mitt/dist/mitt.es.js +63 -0
  43. package/es/rrweb/packages/rrweb/ext/tslib/tslib.es6.js +78 -0
  44. package/es/rrweb/packages/rrweb/src/entries/all.js +13 -0
  45. package/es/rrweb/packages/rrweb/src/index.js +12 -0
  46. package/es/rrweb/packages/rrweb/src/packer/base.js +3 -0
  47. package/es/rrweb/packages/rrweb/src/packer/pack.js +10 -0
  48. package/es/rrweb/packages/rrweb/src/packer/unpack.js +29 -0
  49. package/es/rrweb/packages/rrweb/src/plugins/console/record/error-stack-parser.js +179 -0
  50. package/es/rrweb/packages/rrweb/src/plugins/console/record/index.js +134 -0
  51. package/es/rrweb/packages/rrweb/src/plugins/console/record/stringify.js +141 -0
  52. package/es/rrweb/packages/rrweb/src/plugins/console/replay/index.js +111 -0
  53. package/es/rrweb/packages/rrweb/src/plugins/sequential-id/record/index.js +23 -0
  54. package/es/rrweb/packages/rrweb/src/plugins/sequential-id/replay/index.js +28 -0
  55. package/es/rrweb/packages/rrweb/src/record/iframe-manager.js +32 -0
  56. package/es/rrweb/packages/rrweb/src/record/index.js +381 -0
  57. package/es/rrweb/packages/rrweb/src/record/mutation.js +532 -0
  58. package/es/rrweb/packages/rrweb/src/record/observer.js +665 -0
  59. package/es/rrweb/packages/rrweb/src/record/observers/canvas/2d.js +78 -0
  60. package/es/rrweb/packages/rrweb/src/record/observers/canvas/canvas-manager.js +93 -0
  61. package/es/rrweb/packages/rrweb/src/record/observers/canvas/canvas.js +30 -0
  62. package/es/rrweb/packages/rrweb/src/record/observers/canvas/serialize-args.js +116 -0
  63. package/es/rrweb/packages/rrweb/src/record/observers/canvas/webgl.js +78 -0
  64. package/es/rrweb/packages/rrweb/src/record/shadow-dom-manager.js +45 -0
  65. package/es/rrweb/packages/rrweb/src/replay/canvas/2d.js +25 -0
  66. package/es/rrweb/packages/rrweb/src/replay/canvas/index.js +35 -0
  67. package/es/rrweb/packages/rrweb/src/replay/canvas/webgl.js +124 -0
  68. package/es/rrweb/packages/rrweb/src/replay/index.js +1864 -0
  69. package/es/rrweb/packages/rrweb/src/replay/machine.js +321 -0
  70. package/es/rrweb/packages/rrweb/src/replay/smoothscroll.js +221 -0
  71. package/es/rrweb/packages/rrweb/src/replay/styles/inject-style.js +8 -0
  72. package/es/rrweb/packages/rrweb/src/replay/timer.js +96 -0
  73. package/es/rrweb/packages/rrweb/src/replay/virtual-styles.js +121 -0
  74. package/es/rrweb/packages/rrweb/src/types.js +75 -0
  75. package/es/rrweb/packages/rrweb/src/utils.js +509 -0
  76. package/es/rrweb/packages/rrweb-snapshot/es/rrweb-snapshot.js +1697 -0
  77. package/lib/plugins/console-record.js +650 -0
  78. package/lib/plugins/console-replay.js +288 -0
  79. package/lib/plugins/sequential-id-record.js +28 -0
  80. package/lib/plugins/sequential-id-replay.js +32 -0
  81. package/lib/record/rrweb-record-pack.js +742 -0
  82. package/lib/record/rrweb-record.js +3315 -0
  83. package/lib/replay/rrweb-replay-unpack.js +4621 -0
  84. package/lib/replay/rrweb-replay.js +4211 -0
  85. package/lib/rrweb-all.js +8703 -0
  86. package/lib/rrweb.js +7289 -0
  87. package/package.json +41 -43
  88. package/typings/entries/all.d.ts +6 -0
  89. package/{dist → typings}/entries/record-pack.d.ts +0 -0
  90. package/{dist → typings}/entries/replay-unpack.d.ts +0 -0
  91. package/{dist → typings}/index.d.ts +1 -2
  92. package/{dist → typings}/packer/base.d.ts +0 -0
  93. package/{dist → typings}/packer/index.d.ts +0 -0
  94. package/{dist → typings}/packer/pack.d.ts +0 -0
  95. package/{dist → typings}/packer/unpack.d.ts +0 -0
  96. package/{dist → typings}/plugins/console/record/error-stack-parser.d.ts +0 -0
  97. package/{dist → typings}/plugins/console/record/index.d.ts +3 -2
  98. package/{dist → typings}/plugins/console/record/stringify.d.ts +0 -0
  99. package/{dist → typings}/plugins/console/replay/index.d.ts +2 -2
  100. package/{dist → typings}/plugins/sequential-id/record/index.d.ts +0 -0
  101. package/{dist → typings}/plugins/sequential-id/replay/index.d.ts +0 -0
  102. package/{dist → typings}/record/iframe-manager.d.ts +1 -1
  103. package/{dist → typings}/record/index.d.ts +0 -0
  104. package/{dist → typings}/record/mutation.d.ts +5 -8
  105. package/typings/record/observer.d.ts +7 -0
  106. package/{dist → typings}/record/observers/canvas/2d.d.ts +0 -0
  107. package/{dist → typings}/record/observers/canvas/canvas-manager.d.ts +0 -0
  108. package/{dist → typings}/record/observers/canvas/canvas.d.ts +0 -0
  109. package/{dist → typings}/record/observers/canvas/serialize-args.d.ts +1 -1
  110. package/{dist → typings}/record/observers/canvas/webgl.d.ts +0 -0
  111. package/typings/record/observers/canvas/webgl2.d.ts +2 -0
  112. package/typings/record/shadow-dom-manager.d.ts +21 -0
  113. package/{dist → typings}/replay/canvas/2d.d.ts +0 -0
  114. package/{dist → typings}/replay/canvas/index.d.ts +0 -0
  115. package/{dist → typings}/replay/canvas/webgl.d.ts +0 -0
  116. package/{dist → typings}/replay/index.d.ts +1 -0
  117. package/{dist → typings}/replay/machine.d.ts +0 -0
  118. package/{dist → typings}/replay/smoothscroll.d.ts +0 -0
  119. package/{dist → typings}/replay/styles/inject-style.d.ts +0 -0
  120. package/{dist → typings}/replay/timer.d.ts +0 -0
  121. package/{dist → typings}/replay/virtual-styles.d.ts +1 -1
  122. package/{dist → typings}/rrdom/index.d.ts +0 -0
  123. package/{dist → typings}/rrdom/tree-node.d.ts +0 -0
  124. package/{dist → typings}/types.d.ts +7 -8
  125. package/{dist → typings}/utils.d.ts +3 -4
  126. package/CHANGELOG.md +0 -26
  127. package/README.md +0 -124
  128. package/README.zh_CN.md +0 -122
  129. package/dist/entries/all.d.ts +0 -4
  130. package/dist/index.css +0 -82
  131. package/dist/index.css.map +0 -1
  132. package/dist/index.js +0 -3
  133. package/dist/index.js.LICENSE.txt +0 -14
  134. package/dist/index.js.map +0 -1
  135. package/dist/plugins/entries/all.d.ts +0 -4
  136. package/dist/plugins/entries/record-pack.d.ts +0 -2
  137. package/dist/plugins/entries/replay-unpack.d.ts +0 -2
  138. package/dist/plugins/index.d.ts +0 -9
  139. package/dist/plugins/packer/base.d.ts +0 -7
  140. package/dist/plugins/packer/index.d.ts +0 -2
  141. package/dist/plugins/packer/pack.d.ts +0 -2
  142. package/dist/plugins/packer/unpack.d.ts +0 -2
  143. package/dist/plugins/plugins/console/record/error-stack-parser.d.ts +0 -37
  144. package/dist/plugins/plugins/console/record/index.d.ts +0 -41
  145. package/dist/plugins/plugins/console/record/stringify.d.ts +0 -2
  146. package/dist/plugins/plugins/console/replay/index.d.ts +0 -9
  147. package/dist/plugins/plugins/sequential-id/record/index.d.ts +0 -6
  148. package/dist/plugins/plugins/sequential-id/replay/index.d.ts +0 -7
  149. package/dist/plugins/record/iframe-manager.d.ts +0 -13
  150. package/dist/plugins/record/index.d.ts +0 -9
  151. package/dist/plugins/record/mutation.d.ts +0 -45
  152. package/dist/plugins/record/observer.d.ts +0 -11
  153. package/dist/plugins/record/observers/canvas/2d.d.ts +0 -2
  154. package/dist/plugins/record/observers/canvas/canvas-manager.d.ts +0 -32
  155. package/dist/plugins/record/observers/canvas/canvas.d.ts +0 -2
  156. package/dist/plugins/record/observers/canvas/serialize-args.d.ts +0 -6
  157. package/dist/plugins/record/observers/canvas/webgl.d.ts +0 -2
  158. package/dist/plugins/record/shadow-dom-manager.d.ts +0 -34
  159. package/dist/plugins/replay/canvas/2d.d.ts +0 -9
  160. package/dist/plugins/replay/canvas/index.d.ts +0 -9
  161. package/dist/plugins/replay/canvas/webgl.d.ts +0 -11
  162. package/dist/plugins/replay/index.d.ts +0 -82
  163. package/dist/plugins/replay/machine.d.ts +0 -85
  164. package/dist/plugins/replay/smoothscroll.d.ts +0 -1
  165. package/dist/plugins/replay/styles/inject-style.d.ts +0 -2
  166. package/dist/plugins/replay/timer.d.ts +0 -19
  167. package/dist/plugins/replay/virtual-styles.d.ts +0 -43
  168. package/dist/plugins/rrdom/index.d.ts +0 -1
  169. package/dist/plugins/rrdom/tree-node.d.ts +0 -20
  170. package/dist/plugins/snapshot/css.d.ts +0 -92
  171. package/dist/plugins/snapshot/index.d.ts +0 -5
  172. package/dist/plugins/snapshot/rebuild.d.ts +0 -19
  173. package/dist/plugins/snapshot/snapshot.d.ts +0 -49
  174. package/dist/plugins/snapshot/types.d.ts +0 -99
  175. package/dist/plugins/snapshot/utils.d.ts +0 -11
  176. package/dist/plugins/types.d.ts +0 -509
  177. package/dist/plugins/utils.d.ts +0 -71
  178. package/dist/record/observer.d.ts +0 -11
  179. package/dist/record/shadow-dom-manager.d.ts +0 -34
  180. package/dist/snapshot/css.d.ts +0 -92
  181. package/dist/snapshot/index.d.ts +0 -5
  182. package/dist/snapshot/rebuild.d.ts +0 -19
  183. package/dist/snapshot/snapshot.d.ts +0 -49
  184. package/dist/snapshot/types.d.ts +0 -99
  185. package/dist/snapshot/utils.d.ts +0 -11
@@ -0,0 +1,1864 @@
1
+ import { __spreadArray, __read, __values, __assign } from '../../ext/tslib/tslib.es6.js';
2
+ import { createCache, rebuild, buildNodeWithSN, NodeType } from '../../../rrweb-snapshot/es/rrweb-snapshot.js';
3
+ import * as mitt_es from '../../../../ext/mitt/dist/mitt.es.js';
4
+ import mitt$1 from '../../../../ext/mitt/dist/mitt.es.js';
5
+ import { polyfill } from './smoothscroll.js';
6
+ import { Timer } from './timer.js';
7
+ import { createPlayerService, createSpeedService } from './machine.js';
8
+ import { ReplayerEvents, EventType, IncrementalSource, MouseInteractions } from '../types.js';
9
+ import { polyfill as polyfill$1, isIframeINode, hasShadowRoot, queueToResolveTrees, iterateResolveTree, getBaseDimension, createMirror, TreeIndex } from '../utils.js';
10
+ import rules from './styles/inject-style.js';
11
+ import { getNestedRule, StyleRuleType, getPositionsAndIndex, storeCSSRules, applyVirtualStyleRulesToNode } from './virtual-styles.js';
12
+ import canvasMutation from './canvas/index.js';
13
+
14
+ var SKIP_TIME_THRESHOLD = 10 * 1000;
15
+ var SKIP_TIME_INTERVAL = 2 * 1000;
16
+ var SKIP_TIME_MIN = 1 * 1000;
17
+ var SKIP_DURATION_LIMIT = 60 * 60 * 1000;
18
+ var mitt = mitt$1 || mitt_es;
19
+ var REPLAY_CONSOLE_PREFIX = '[replayer]';
20
+ var defaultMouseTailConfig = {
21
+ duration: 500,
22
+ lineCap: 'round',
23
+ lineWidth: 3,
24
+ strokeStyle: 'red',
25
+ };
26
+ function indicatesTouchDevice(e) {
27
+ return (e.type == EventType.IncrementalSnapshot &&
28
+ (e.data.source == IncrementalSource.TouchMove ||
29
+ (e.data.source == IncrementalSource.MouseInteraction &&
30
+ e.data.type == MouseInteractions.TouchStart)));
31
+ }
32
+ var Replayer = (function () {
33
+ function Replayer(events, config) {
34
+ var _this = this;
35
+ this.mouseTail = null;
36
+ this.tailPositions = [];
37
+ this.emitter = mitt();
38
+ this.activityIntervals = [];
39
+ this.legacy_missingNodeRetryMap = {};
40
+ this.cache = createCache();
41
+ this.imageMap = new Map();
42
+ this.mirror = createMirror();
43
+ this.firstFullSnapshot = null;
44
+ this.newDocumentQueue = [];
45
+ this.mousePos = null;
46
+ this.touchActive = null;
47
+ if (!(config === null || config === void 0 ? void 0 : config.liveMode) && events.length < 2) {
48
+ throw new Error('Replayer need at least 2 events.');
49
+ }
50
+ var defaultConfig = {
51
+ speed: 1,
52
+ maxSpeed: 360,
53
+ root: document.body,
54
+ loadTimeout: 0,
55
+ skipInactive: false,
56
+ showWarning: true,
57
+ showDebug: false,
58
+ blockClass: 'highlight-block',
59
+ liveMode: false,
60
+ insertStyleRules: [],
61
+ triggerFocus: true,
62
+ UNSAFE_replayCanvas: false,
63
+ pauseAnimation: true,
64
+ mouseTail: defaultMouseTailConfig,
65
+ inactiveThreshold: 0.02,
66
+ inactiveSkipTime: SKIP_TIME_INTERVAL,
67
+ };
68
+ this.config = Object.assign({}, defaultConfig, config);
69
+ this.handleResize = this.handleResize.bind(this);
70
+ this.getCastFn = this.getCastFn.bind(this);
71
+ this.applyEventsSynchronously = this.applyEventsSynchronously.bind(this);
72
+ this.emitter.on(ReplayerEvents.Resize, this.handleResize);
73
+ this.setupDom();
74
+ this.treeIndex = new TreeIndex();
75
+ this.fragmentParentMap = new Map();
76
+ this.elementStateMap = new Map();
77
+ this.virtualStyleRulesMap = new Map();
78
+ this.emitter.on(ReplayerEvents.Flush, function () {
79
+ var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
80
+ var _e = _this.treeIndex.flush(), scrollMap = _e.scrollMap, inputMap = _e.inputMap, mutationData = _e.mutationData;
81
+ _this.fragmentParentMap.forEach(function (parent, frag) {
82
+ return _this.restoreRealParent(frag, parent);
83
+ });
84
+ try {
85
+ for (var _f = __values(mutationData.texts), _g = _f.next(); !_g.done; _g = _f.next()) {
86
+ var d = _g.value;
87
+ _this.applyText(d, mutationData);
88
+ }
89
+ }
90
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
91
+ finally {
92
+ try {
93
+ if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
94
+ }
95
+ finally { if (e_1) throw e_1.error; }
96
+ }
97
+ try {
98
+ for (var _h = __values(_this.virtualStyleRulesMap.keys()), _j = _h.next(); !_j.done; _j = _h.next()) {
99
+ var node = _j.value;
100
+ _this.restoreNodeSheet(node);
101
+ }
102
+ }
103
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
104
+ finally {
105
+ try {
106
+ if (_j && !_j.done && (_b = _h.return)) _b.call(_h);
107
+ }
108
+ finally { if (e_2) throw e_2.error; }
109
+ }
110
+ _this.fragmentParentMap.clear();
111
+ _this.elementStateMap.clear();
112
+ _this.virtualStyleRulesMap.clear();
113
+ try {
114
+ for (var _k = __values(scrollMap.values()), _l = _k.next(); !_l.done; _l = _k.next()) {
115
+ var d = _l.value;
116
+ _this.applyScroll(d, true);
117
+ }
118
+ }
119
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
120
+ finally {
121
+ try {
122
+ if (_l && !_l.done && (_c = _k.return)) _c.call(_k);
123
+ }
124
+ finally { if (e_3) throw e_3.error; }
125
+ }
126
+ try {
127
+ for (var _m = __values(inputMap.values()), _o = _m.next(); !_o.done; _o = _m.next()) {
128
+ var d = _o.value;
129
+ _this.applyInput(d);
130
+ }
131
+ }
132
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
133
+ finally {
134
+ try {
135
+ if (_o && !_o.done && (_d = _m.return)) _d.call(_m);
136
+ }
137
+ finally { if (e_4) throw e_4.error; }
138
+ }
139
+ });
140
+ this.emitter.on(ReplayerEvents.PlayBack, function () {
141
+ _this.firstFullSnapshot = null;
142
+ _this.mirror.reset();
143
+ });
144
+ var timer = new Timer([], (config === null || config === void 0 ? void 0 : config.speed) || defaultConfig.speed);
145
+ this.service = createPlayerService({
146
+ events: events
147
+ .map(function (e) {
148
+ if (config && config.unpackFn) {
149
+ return config.unpackFn(e);
150
+ }
151
+ return e;
152
+ })
153
+ .sort(function (a1, a2) { return a1.timestamp - a2.timestamp; }),
154
+ timer: timer,
155
+ timeOffset: 0,
156
+ baselineTime: 0,
157
+ lastPlayedEvent: null,
158
+ }, {
159
+ getCastFn: this.getCastFn,
160
+ applyEventsSynchronously: this.applyEventsSynchronously,
161
+ emitter: this.emitter,
162
+ });
163
+ this.service.start();
164
+ this.service.subscribe(function (state) {
165
+ _this.emitter.emit(ReplayerEvents.StateChange, {
166
+ player: state,
167
+ });
168
+ });
169
+ this.speedService = createSpeedService({
170
+ normalSpeed: -1,
171
+ timer: timer,
172
+ });
173
+ this.speedService.start();
174
+ this.speedService.subscribe(function (state) {
175
+ _this.emitter.emit(ReplayerEvents.StateChange, {
176
+ speed: state,
177
+ });
178
+ });
179
+ var firstMeta = this.service.state.context.events.find(function (e) { return e.type === EventType.Meta; });
180
+ var firstFullsnapshot = this.service.state.context.events.find(function (e) { return e.type === EventType.FullSnapshot; });
181
+ if (firstMeta) {
182
+ var _a = firstMeta.data, width_1 = _a.width, height_1 = _a.height;
183
+ setTimeout(function () {
184
+ _this.emitter.emit(ReplayerEvents.Resize, {
185
+ width: width_1,
186
+ height: height_1,
187
+ });
188
+ }, 0);
189
+ }
190
+ if (firstFullsnapshot) {
191
+ setTimeout(function () {
192
+ if (_this.firstFullSnapshot) {
193
+ return;
194
+ }
195
+ _this.firstFullSnapshot = firstFullsnapshot;
196
+ _this.rebuildFullSnapshot(firstFullsnapshot);
197
+ _this.iframe.contentWindow.scrollTo(firstFullsnapshot.data.initialOffset);
198
+ }, 1);
199
+ }
200
+ if (this.service.state.context.events.find(indicatesTouchDevice)) {
201
+ this.mouse.classList.add('touch-device');
202
+ }
203
+ }
204
+ Object.defineProperty(Replayer.prototype, "timer", {
205
+ get: function () {
206
+ return this.service.state.context.timer;
207
+ },
208
+ enumerable: false,
209
+ configurable: true
210
+ });
211
+ Replayer.prototype.on = function (event, handler) {
212
+ this.emitter.on(event, handler);
213
+ return this;
214
+ };
215
+ Replayer.prototype.off = function (event, handler) {
216
+ this.emitter.off(event, handler);
217
+ return this;
218
+ };
219
+ Replayer.prototype.setConfig = function (config) {
220
+ var _this = this;
221
+ Object.keys(config).forEach(function (key) {
222
+ _this.config[key] = config[key];
223
+ });
224
+ if (!this.config.skipInactive) {
225
+ this.backToNormal();
226
+ }
227
+ if (typeof config.speed !== 'undefined') {
228
+ this.speedService.send({
229
+ type: 'SET_SPEED',
230
+ payload: {
231
+ speed: config.speed,
232
+ },
233
+ });
234
+ }
235
+ if (typeof config.mouseTail !== 'undefined') {
236
+ if (config.mouseTail === false) {
237
+ if (this.mouseTail) {
238
+ this.mouseTail.style.display = 'none';
239
+ }
240
+ }
241
+ else {
242
+ if (!this.mouseTail) {
243
+ this.mouseTail = document.createElement('canvas');
244
+ this.mouseTail.width = Number.parseFloat(this.iframe.width);
245
+ this.mouseTail.height = Number.parseFloat(this.iframe.height);
246
+ this.mouseTail.classList.add('replayer-mouse-tail');
247
+ this.wrapper.insertBefore(this.mouseTail, this.iframe);
248
+ }
249
+ this.mouseTail.style.display = 'inherit';
250
+ }
251
+ }
252
+ };
253
+ Replayer.prototype.getActivityIntervals = function () {
254
+ var _this = this;
255
+ if (this.activityIntervals.length == 0) {
256
+ var allIntervals = [];
257
+ var metadata = this.getMetaData();
258
+ var userInteractionEvents = __spreadArray(__spreadArray([
259
+ { timestamp: metadata.startTime }
260
+ ], __read(this.service.state.context.events.filter(function (ev) {
261
+ return _this.isUserInteraction(ev);
262
+ })), false), [
263
+ { timestamp: metadata.endTime },
264
+ ], false);
265
+ for (var i = 1; i < userInteractionEvents.length; i++) {
266
+ var currentInterval_1 = userInteractionEvents[i - 1];
267
+ var _event = userInteractionEvents[i];
268
+ if (_event.timestamp - currentInterval_1.timestamp >
269
+ SKIP_TIME_THRESHOLD) {
270
+ allIntervals.push({
271
+ startTime: currentInterval_1.timestamp,
272
+ endTime: _event.timestamp,
273
+ duration: _event.timestamp - currentInterval_1.timestamp,
274
+ active: false,
275
+ });
276
+ }
277
+ else {
278
+ allIntervals.push({
279
+ startTime: currentInterval_1.timestamp,
280
+ endTime: _event.timestamp,
281
+ duration: _event.timestamp - currentInterval_1.timestamp,
282
+ active: true,
283
+ });
284
+ }
285
+ }
286
+ var mergedIntervals = [];
287
+ var currentInterval = allIntervals[0];
288
+ for (var i = 1; i < allIntervals.length; i++) {
289
+ if (allIntervals[i].active != allIntervals[i - 1].active) {
290
+ mergedIntervals.push({
291
+ startTime: currentInterval.startTime,
292
+ endTime: allIntervals[i - 1].endTime,
293
+ duration: allIntervals[i - 1].endTime - currentInterval.startTime,
294
+ active: allIntervals[i - 1].active,
295
+ });
296
+ currentInterval = allIntervals[i];
297
+ }
298
+ }
299
+ if (currentInterval && allIntervals.length > 0) {
300
+ mergedIntervals.push({
301
+ startTime: currentInterval.startTime,
302
+ endTime: allIntervals[allIntervals.length - 1].endTime,
303
+ duration: allIntervals[allIntervals.length - 1].endTime -
304
+ currentInterval.startTime,
305
+ active: allIntervals[allIntervals.length - 1].active,
306
+ });
307
+ }
308
+ currentInterval = mergedIntervals[0];
309
+ for (var i = 1; i < mergedIntervals.length; i++) {
310
+ if ((!mergedIntervals[i].active &&
311
+ mergedIntervals[i].duration >
312
+ this.config.inactiveThreshold * metadata.totalTime) ||
313
+ (!mergedIntervals[i - 1].active &&
314
+ mergedIntervals[i - 1].duration >
315
+ this.config.inactiveThreshold * metadata.totalTime)) {
316
+ this.activityIntervals.push({
317
+ startTime: currentInterval.startTime,
318
+ endTime: mergedIntervals[i - 1].endTime,
319
+ duration: mergedIntervals[i - 1].endTime - currentInterval.startTime,
320
+ active: mergedIntervals[i - 1].active,
321
+ });
322
+ currentInterval = mergedIntervals[i];
323
+ }
324
+ }
325
+ if (currentInterval && mergedIntervals.length > 0) {
326
+ this.activityIntervals.push({
327
+ startTime: currentInterval.startTime,
328
+ endTime: mergedIntervals[mergedIntervals.length - 1].endTime,
329
+ duration: mergedIntervals[mergedIntervals.length - 1].endTime -
330
+ currentInterval.startTime,
331
+ active: mergedIntervals[mergedIntervals.length - 1].active,
332
+ });
333
+ }
334
+ }
335
+ return this.activityIntervals;
336
+ };
337
+ Replayer.prototype.getMetaData = function () {
338
+ var firstEvent = this.service.state.context.events[0];
339
+ var lastEvent = this.service.state.context.events[this.service.state.context.events.length - 1];
340
+ return {
341
+ startTime: firstEvent.timestamp,
342
+ endTime: lastEvent.timestamp,
343
+ totalTime: lastEvent.timestamp - firstEvent.timestamp,
344
+ };
345
+ };
346
+ Replayer.prototype.getCurrentTime = function () {
347
+ return this.timer.timeOffset + this.getTimeOffset();
348
+ };
349
+ Replayer.prototype.getTimeOffset = function () {
350
+ var _a = this.service.state.context, baselineTime = _a.baselineTime, events = _a.events;
351
+ return baselineTime - events[0].timestamp;
352
+ };
353
+ Replayer.prototype.getMirror = function () {
354
+ return this.mirror;
355
+ };
356
+ Replayer.prototype.play = function (timeOffset) {
357
+ var _a;
358
+ if (timeOffset === void 0) { timeOffset = 0; }
359
+ if (this.service.state.matches('paused')) {
360
+ this.service.send({ type: 'PLAY', payload: { timeOffset: timeOffset } });
361
+ }
362
+ else {
363
+ this.service.send({ type: 'PAUSE' });
364
+ this.service.send({ type: 'PLAY', payload: { timeOffset: timeOffset } });
365
+ }
366
+ (_a = this.iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.getElementsByTagName('html')[0].classList.remove('rrweb-paused');
367
+ this.emitter.emit(ReplayerEvents.Start);
368
+ this.handleInactivity(this.getMetaData().startTime + timeOffset, true);
369
+ };
370
+ Replayer.prototype.pause = function (timeOffset) {
371
+ var _a;
372
+ if (timeOffset === undefined && this.service.state.matches('playing')) {
373
+ this.service.send({ type: 'PAUSE' });
374
+ }
375
+ if (typeof timeOffset === 'number') {
376
+ this.play(timeOffset);
377
+ this.service.send({ type: 'PAUSE' });
378
+ }
379
+ (_a = this.iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.getElementsByTagName('html')[0].classList.add('rrweb-paused');
380
+ this.emitter.emit(ReplayerEvents.Pause);
381
+ };
382
+ Replayer.prototype.resume = function (timeOffset) {
383
+ if (timeOffset === void 0) { timeOffset = 0; }
384
+ console.warn("The 'resume' will be departed in 1.0. Please use 'play' method which has the same interface.");
385
+ this.play(timeOffset);
386
+ this.emitter.emit(ReplayerEvents.Resume);
387
+ };
388
+ Replayer.prototype.startLive = function (baselineTime) {
389
+ this.service.send({ type: 'TO_LIVE', payload: { baselineTime: baselineTime } });
390
+ };
391
+ Replayer.prototype.addEvent = function (rawEvent) {
392
+ var _this = this;
393
+ var event = this.config.unpackFn
394
+ ? this.config.unpackFn(rawEvent)
395
+ : rawEvent;
396
+ if (indicatesTouchDevice(event)) {
397
+ this.mouse.classList.add('touch-device');
398
+ }
399
+ Promise.resolve().then(function () {
400
+ return _this.service.send({ type: 'ADD_EVENT', payload: { event: event } });
401
+ });
402
+ };
403
+ Replayer.prototype.replaceEvents = function (events) {
404
+ var e_5, _a;
405
+ try {
406
+ for (var events_1 = __values(events), events_1_1 = events_1.next(); !events_1_1.done; events_1_1 = events_1.next()) {
407
+ var event_1 = events_1_1.value;
408
+ if (indicatesTouchDevice(event_1)) {
409
+ this.mouse.classList.add('touch-device');
410
+ break;
411
+ }
412
+ }
413
+ }
414
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
415
+ finally {
416
+ try {
417
+ if (events_1_1 && !events_1_1.done && (_a = events_1.return)) _a.call(events_1);
418
+ }
419
+ finally { if (e_5) throw e_5.error; }
420
+ }
421
+ this.service.send({ type: 'REPLACE_EVENTS', payload: { events: events } });
422
+ };
423
+ Replayer.prototype.enableInteract = function () {
424
+ this.iframe.setAttribute('scrolling', 'auto');
425
+ this.iframe.style.pointerEvents = 'auto';
426
+ };
427
+ Replayer.prototype.disableInteract = function () {
428
+ this.iframe.setAttribute('scrolling', 'no');
429
+ this.iframe.style.pointerEvents = 'none';
430
+ };
431
+ Replayer.prototype.resetCache = function () {
432
+ this.cache = createCache();
433
+ };
434
+ Replayer.prototype.setupDom = function () {
435
+ this.wrapper = document.createElement('div');
436
+ this.wrapper.classList.add('replayer-wrapper');
437
+ this.config.root.appendChild(this.wrapper);
438
+ this.mouse = document.createElement('div');
439
+ this.mouse.classList.add('replayer-mouse');
440
+ this.wrapper.appendChild(this.mouse);
441
+ if (this.config.mouseTail !== false) {
442
+ this.mouseTail = document.createElement('canvas');
443
+ this.mouseTail.classList.add('replayer-mouse-tail');
444
+ this.mouseTail.style.display = 'inherit';
445
+ this.wrapper.appendChild(this.mouseTail);
446
+ }
447
+ this.iframe = document.createElement('iframe');
448
+ var attributes = ['allow-same-origin'];
449
+ if (this.config.UNSAFE_replayCanvas) {
450
+ attributes.push('allow-scripts');
451
+ }
452
+ this.iframe.style.display = 'none';
453
+ this.iframe.setAttribute('sandbox', attributes.join(' '));
454
+ this.disableInteract();
455
+ this.wrapper.appendChild(this.iframe);
456
+ if (this.iframe.contentWindow && this.iframe.contentDocument) {
457
+ polyfill(this.iframe.contentWindow, this.iframe.contentDocument);
458
+ polyfill$1(this.iframe.contentWindow);
459
+ }
460
+ };
461
+ Replayer.prototype.handleResize = function (dimension) {
462
+ var e_6, _a;
463
+ this.iframe.style.display = 'inherit';
464
+ try {
465
+ for (var _b = __values([this.mouseTail, this.iframe]), _c = _b.next(); !_c.done; _c = _b.next()) {
466
+ var el = _c.value;
467
+ if (!el) {
468
+ continue;
469
+ }
470
+ el.setAttribute('width', String(dimension.width));
471
+ el.setAttribute('height', String(dimension.height));
472
+ }
473
+ }
474
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
475
+ finally {
476
+ try {
477
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
478
+ }
479
+ finally { if (e_6) throw e_6.error; }
480
+ }
481
+ };
482
+ Replayer.prototype.applyEventsSynchronously = function (events) {
483
+ var e_7, _a;
484
+ try {
485
+ for (var events_2 = __values(events), events_2_1 = events_2.next(); !events_2_1.done; events_2_1 = events_2.next()) {
486
+ var event_2 = events_2_1.value;
487
+ switch (event_2.type) {
488
+ case EventType.DomContentLoaded:
489
+ case EventType.Load:
490
+ case EventType.Custom:
491
+ continue;
492
+ case EventType.FullSnapshot:
493
+ case EventType.Meta:
494
+ case EventType.Plugin:
495
+ break;
496
+ case EventType.IncrementalSnapshot:
497
+ switch (event_2.data.source) {
498
+ case IncrementalSource.MediaInteraction:
499
+ continue;
500
+ default:
501
+ break;
502
+ }
503
+ break;
504
+ default:
505
+ break;
506
+ }
507
+ var castFn = this.getCastFn(event_2, true);
508
+ castFn();
509
+ }
510
+ }
511
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
512
+ finally {
513
+ try {
514
+ if (events_2_1 && !events_2_1.done && (_a = events_2.return)) _a.call(events_2);
515
+ }
516
+ finally { if (e_7) throw e_7.error; }
517
+ }
518
+ if (this.mousePos) {
519
+ this.moveAndHover(this.mousePos.x, this.mousePos.y, this.mousePos.id, true, this.mousePos.debugData);
520
+ }
521
+ this.mousePos = null;
522
+ if (this.touchActive === true) {
523
+ this.mouse.classList.add('touch-active');
524
+ }
525
+ else if (this.touchActive === false) {
526
+ this.mouse.classList.remove('touch-active');
527
+ }
528
+ this.touchActive = null;
529
+ };
530
+ Replayer.prototype.getCastFn = function (event, isSync) {
531
+ var _this = this;
532
+ if (isSync === void 0) { isSync = false; }
533
+ var castFn;
534
+ switch (event.type) {
535
+ case EventType.DomContentLoaded:
536
+ case EventType.Load:
537
+ break;
538
+ case EventType.Custom:
539
+ castFn = function () {
540
+ _this.emitter.emit(ReplayerEvents.CustomEvent, event);
541
+ };
542
+ break;
543
+ case EventType.Meta:
544
+ castFn = function () {
545
+ return _this.emitter.emit(ReplayerEvents.Resize, {
546
+ width: event.data.width,
547
+ height: event.data.height,
548
+ });
549
+ };
550
+ break;
551
+ case EventType.FullSnapshot:
552
+ castFn = function () {
553
+ if (_this.firstFullSnapshot) {
554
+ if (_this.firstFullSnapshot === event) {
555
+ _this.firstFullSnapshot = true;
556
+ return;
557
+ }
558
+ }
559
+ else {
560
+ _this.firstFullSnapshot = true;
561
+ }
562
+ _this.rebuildFullSnapshot(event, isSync);
563
+ _this.iframe.contentWindow.scrollTo(event.data.initialOffset);
564
+ };
565
+ break;
566
+ case EventType.IncrementalSnapshot:
567
+ castFn = function () {
568
+ var e_8, _a;
569
+ _this.applyIncremental(event, isSync);
570
+ if (isSync) {
571
+ return;
572
+ }
573
+ _this.handleInactivity(event.timestamp);
574
+ if (event === _this.nextUserInteractionEvent) {
575
+ _this.nextUserInteractionEvent = null;
576
+ _this.backToNormal();
577
+ }
578
+ if (_this.config.skipInactive && !_this.nextUserInteractionEvent) {
579
+ try {
580
+ for (var _b = __values(_this.service.state.context.events), _c = _b.next(); !_c.done; _c = _b.next()) {
581
+ var _event = _c.value;
582
+ if (_event.timestamp <= event.timestamp) {
583
+ continue;
584
+ }
585
+ if (_this.isUserInteraction(_event)) {
586
+ if (_event.delay - event.delay >
587
+ SKIP_TIME_THRESHOLD *
588
+ _this.speedService.state.context.timer.speed) {
589
+ _this.nextUserInteractionEvent = _event;
590
+ }
591
+ break;
592
+ }
593
+ }
594
+ }
595
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
596
+ finally {
597
+ try {
598
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
599
+ }
600
+ finally { if (e_8) throw e_8.error; }
601
+ }
602
+ if (_this.nextUserInteractionEvent) {
603
+ var skipTime = _this.nextUserInteractionEvent.delay - event.delay;
604
+ var payload = {
605
+ speed: Math.min(Math.round(skipTime / SKIP_TIME_INTERVAL), _this.config.maxSpeed),
606
+ };
607
+ _this.speedService.send({ type: 'FAST_FORWARD', payload: payload });
608
+ _this.emitter.emit(ReplayerEvents.SkipStart, payload);
609
+ }
610
+ }
611
+ };
612
+ break;
613
+ }
614
+ var wrappedCastFn = function () {
615
+ var e_9, _a;
616
+ if (castFn) {
617
+ castFn();
618
+ }
619
+ try {
620
+ for (var _b = __values(_this.config.plugins || []), _c = _b.next(); !_c.done; _c = _b.next()) {
621
+ var plugin = _c.value;
622
+ plugin.handler(event, isSync, { replayer: _this });
623
+ }
624
+ }
625
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
626
+ finally {
627
+ try {
628
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
629
+ }
630
+ finally { if (e_9) throw e_9.error; }
631
+ }
632
+ _this.service.send({ type: 'CAST_EVENT', payload: { event: event } });
633
+ var last_index = _this.service.state.context.events.length - 1;
634
+ if (event === _this.service.state.context.events[last_index]) {
635
+ var finish_1 = function () {
636
+ if (last_index < _this.service.state.context.events.length - 1) {
637
+ return;
638
+ }
639
+ _this.backToNormal();
640
+ _this.service.send('END');
641
+ _this.emitter.emit(ReplayerEvents.Finish);
642
+ };
643
+ if (event.type === EventType.IncrementalSnapshot &&
644
+ event.data.source === IncrementalSource.MouseMove &&
645
+ event.data.positions.length) {
646
+ setTimeout(function () {
647
+ finish_1();
648
+ }, Math.max(0, -event.data.positions[0].timeOffset + 50));
649
+ }
650
+ else {
651
+ finish_1();
652
+ }
653
+ }
654
+ _this.emitter.emit(ReplayerEvents.EventCast, event);
655
+ };
656
+ return wrappedCastFn;
657
+ };
658
+ Replayer.prototype.handleInactivity = function (timestamp, resetNext) {
659
+ var e_10, _a;
660
+ if (timestamp === this.inactiveEndTimestamp || resetNext) {
661
+ this.inactiveEndTimestamp = null;
662
+ this.backToNormal();
663
+ }
664
+ if (this.config.skipInactive && !this.inactiveEndTimestamp) {
665
+ try {
666
+ for (var _b = __values(this.getActivityIntervals()), _c = _b.next(); !_c.done; _c = _b.next()) {
667
+ var interval = _c.value;
668
+ if (timestamp >= interval.startTime &&
669
+ timestamp < interval.endTime &&
670
+ !interval.active) {
671
+ this.inactiveEndTimestamp = interval.endTime;
672
+ break;
673
+ }
674
+ }
675
+ }
676
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
677
+ finally {
678
+ try {
679
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
680
+ }
681
+ finally { if (e_10) throw e_10.error; }
682
+ }
683
+ if (this.inactiveEndTimestamp) {
684
+ var skipTime = this.inactiveEndTimestamp - timestamp;
685
+ var payload = {
686
+ speed: (skipTime / SKIP_DURATION_LIMIT) * this.config.inactiveSkipTime <
687
+ SKIP_TIME_MIN
688
+ ? skipTime / SKIP_TIME_MIN
689
+ : Math.round(Math.max(skipTime, SKIP_DURATION_LIMIT) /
690
+ this.config.inactiveSkipTime),
691
+ };
692
+ this.speedService.send({ type: 'FAST_FORWARD', payload: payload });
693
+ this.emitter.emit(ReplayerEvents.SkipStart, payload);
694
+ }
695
+ }
696
+ };
697
+ Replayer.prototype.rebuildFullSnapshot = function (event, isSync) {
698
+ var e_11, _a;
699
+ var _this = this;
700
+ if (isSync === void 0) { isSync = false; }
701
+ if (!this.iframe.contentDocument) {
702
+ return console.warn('Looks like your replayer has been destroyed.');
703
+ }
704
+ if (Object.keys(this.legacy_missingNodeRetryMap).length) {
705
+ console.warn('Found unresolved missing node map', this.legacy_missingNodeRetryMap);
706
+ }
707
+ this.legacy_missingNodeRetryMap = {};
708
+ var collected = [];
709
+ this.mirror.map = rebuild(event.data.node, {
710
+ doc: this.iframe.contentDocument,
711
+ afterAppend: function (builtNode) {
712
+ _this.collectIframeAndAttachDocument(collected, builtNode);
713
+ },
714
+ cache: this.cache,
715
+ })[1];
716
+ var _loop_1 = function (mutationInQueue, builtNode) {
717
+ this_1.attachDocumentToIframe(mutationInQueue, builtNode);
718
+ this_1.newDocumentQueue = this_1.newDocumentQueue.filter(function (m) { return m !== mutationInQueue; });
719
+ };
720
+ var this_1 = this;
721
+ try {
722
+ for (var collected_1 = __values(collected), collected_1_1 = collected_1.next(); !collected_1_1.done; collected_1_1 = collected_1.next()) {
723
+ var _b = collected_1_1.value, mutationInQueue = _b.mutationInQueue, builtNode = _b.builtNode;
724
+ _loop_1(mutationInQueue, builtNode);
725
+ }
726
+ }
727
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
728
+ finally {
729
+ try {
730
+ if (collected_1_1 && !collected_1_1.done && (_a = collected_1.return)) _a.call(collected_1);
731
+ }
732
+ finally { if (e_11) throw e_11.error; }
733
+ }
734
+ var _c = this.iframe.contentDocument, documentElement = _c.documentElement, head = _c.head;
735
+ this.insertStyleRules(documentElement, head);
736
+ if (!this.service.state.matches('playing')) {
737
+ this.iframe.contentDocument
738
+ .getElementsByTagName('html')[0]
739
+ .classList.add('rrweb-paused');
740
+ }
741
+ this.emitter.emit(ReplayerEvents.FullsnapshotRebuilded, event);
742
+ if (!isSync) {
743
+ this.waitForStylesheetLoad();
744
+ }
745
+ if (this.config.UNSAFE_replayCanvas) {
746
+ this.preloadAllImages();
747
+ }
748
+ };
749
+ Replayer.prototype.insertStyleRules = function (documentElement, head) {
750
+ var styleEl = document.createElement('style');
751
+ documentElement.insertBefore(styleEl, head);
752
+ var injectStylesRules = rules(this.config.blockClass).concat(this.config.insertStyleRules);
753
+ if (this.config.pauseAnimation) {
754
+ injectStylesRules.push('html.rrweb-paused *, html.rrweb-paused *:before, html.rrweb-paused *:after { animation-play-state: paused !important; }');
755
+ }
756
+ for (var idx = 0; idx < injectStylesRules.length; idx++) {
757
+ styleEl.sheet.insertRule(injectStylesRules[idx], idx);
758
+ }
759
+ };
760
+ Replayer.prototype.attachDocumentToIframe = function (mutation, iframeEl) {
761
+ var e_12, _a;
762
+ var _this = this;
763
+ var collected = [];
764
+ if (!iframeEl.contentDocument) {
765
+ var parent_1 = iframeEl.parentNode;
766
+ while (parent_1) {
767
+ if (this.fragmentParentMap.has(parent_1)) {
768
+ var frag = parent_1;
769
+ var realParent = this.fragmentParentMap.get(frag);
770
+ this.restoreRealParent(frag, realParent);
771
+ break;
772
+ }
773
+ parent_1 = parent_1.parentNode;
774
+ }
775
+ }
776
+ buildNodeWithSN(mutation.node, {
777
+ doc: iframeEl.contentDocument,
778
+ map: this.mirror.map,
779
+ hackCss: true,
780
+ skipChild: false,
781
+ afterAppend: function (builtNode) {
782
+ _this.collectIframeAndAttachDocument(collected, builtNode);
783
+ if (builtNode.__sn.type === NodeType.Element &&
784
+ builtNode.__sn.tagName.toUpperCase() === 'HTML') {
785
+ var _a = iframeEl.contentDocument, documentElement = _a.documentElement, head = _a.head;
786
+ _this.insertStyleRules(documentElement, head);
787
+ }
788
+ },
789
+ cache: this.cache,
790
+ });
791
+ var _loop_2 = function (mutationInQueue, builtNode) {
792
+ this_2.attachDocumentToIframe(mutationInQueue, builtNode);
793
+ this_2.newDocumentQueue = this_2.newDocumentQueue.filter(function (m) { return m !== mutationInQueue; });
794
+ };
795
+ var this_2 = this;
796
+ try {
797
+ for (var collected_2 = __values(collected), collected_2_1 = collected_2.next(); !collected_2_1.done; collected_2_1 = collected_2.next()) {
798
+ var _b = collected_2_1.value, mutationInQueue = _b.mutationInQueue, builtNode = _b.builtNode;
799
+ _loop_2(mutationInQueue, builtNode);
800
+ }
801
+ }
802
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
803
+ finally {
804
+ try {
805
+ if (collected_2_1 && !collected_2_1.done && (_a = collected_2.return)) _a.call(collected_2);
806
+ }
807
+ finally { if (e_12) throw e_12.error; }
808
+ }
809
+ };
810
+ Replayer.prototype.collectIframeAndAttachDocument = function (collected, builtNode) {
811
+ if (isIframeINode(builtNode)) {
812
+ var mutationInQueue = this.newDocumentQueue.find(function (m) { return m.parentId === builtNode.__sn.id; });
813
+ if (mutationInQueue) {
814
+ collected.push({ mutationInQueue: mutationInQueue, builtNode: builtNode });
815
+ }
816
+ }
817
+ };
818
+ Replayer.prototype.waitForStylesheetLoad = function () {
819
+ var _this = this;
820
+ var _a;
821
+ var head = (_a = this.iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.head;
822
+ if (head) {
823
+ var unloadSheets_1 = new Set();
824
+ var timer_1;
825
+ var beforeLoadState_1 = this.service.state;
826
+ var stateHandler_1 = function () {
827
+ beforeLoadState_1 = _this.service.state;
828
+ };
829
+ this.emitter.on(ReplayerEvents.Start, stateHandler_1);
830
+ this.emitter.on(ReplayerEvents.Pause, stateHandler_1);
831
+ var unsubscribe_1 = function () {
832
+ _this.emitter.off(ReplayerEvents.Start, stateHandler_1);
833
+ _this.emitter.off(ReplayerEvents.Pause, stateHandler_1);
834
+ };
835
+ head
836
+ .querySelectorAll('link[rel="stylesheet"]')
837
+ .forEach(function (css) {
838
+ if (!css.sheet) {
839
+ unloadSheets_1.add(css);
840
+ css.addEventListener('load', function () {
841
+ unloadSheets_1.delete(css);
842
+ if (unloadSheets_1.size === 0 && timer_1 !== -1) {
843
+ if (beforeLoadState_1.matches('playing')) {
844
+ _this.play(_this.getCurrentTime());
845
+ }
846
+ _this.emitter.emit(ReplayerEvents.LoadStylesheetEnd);
847
+ if (timer_1) {
848
+ clearTimeout(timer_1);
849
+ }
850
+ unsubscribe_1();
851
+ }
852
+ });
853
+ }
854
+ });
855
+ if (unloadSheets_1.size > 0) {
856
+ this.service.send({ type: 'PAUSE' });
857
+ this.emitter.emit(ReplayerEvents.LoadStylesheetStart);
858
+ timer_1 = setTimeout(function () {
859
+ if (beforeLoadState_1.matches('playing')) {
860
+ _this.play(_this.getCurrentTime());
861
+ }
862
+ timer_1 = -1;
863
+ unsubscribe_1();
864
+ }, this.config.loadTimeout);
865
+ }
866
+ }
867
+ };
868
+ Replayer.prototype.hasImageArg = function (args) {
869
+ var e_13, _a;
870
+ try {
871
+ for (var args_1 = __values(args), args_1_1 = args_1.next(); !args_1_1.done; args_1_1 = args_1.next()) {
872
+ var arg = args_1_1.value;
873
+ if (!arg || typeof arg !== 'object') {
874
+ }
875
+ else if ('rr_type' in arg && 'args' in arg) {
876
+ if (this.hasImageArg(arg.args))
877
+ return true;
878
+ }
879
+ else if ('rr_type' in arg && arg.rr_type === 'HTMLImageElement') {
880
+ return true;
881
+ }
882
+ else if (arg instanceof Array) {
883
+ if (this.hasImageArg(arg))
884
+ return true;
885
+ }
886
+ }
887
+ }
888
+ catch (e_13_1) { e_13 = { error: e_13_1 }; }
889
+ finally {
890
+ try {
891
+ if (args_1_1 && !args_1_1.done && (_a = args_1.return)) _a.call(args_1);
892
+ }
893
+ finally { if (e_13) throw e_13.error; }
894
+ }
895
+ return false;
896
+ };
897
+ Replayer.prototype.getImageArgs = function (args) {
898
+ var e_14, _a;
899
+ var images = [];
900
+ try {
901
+ for (var args_2 = __values(args), args_2_1 = args_2.next(); !args_2_1.done; args_2_1 = args_2.next()) {
902
+ var arg = args_2_1.value;
903
+ if (!arg || typeof arg !== 'object') {
904
+ }
905
+ else if ('rr_type' in arg && 'args' in arg) {
906
+ images.push.apply(images, __spreadArray([], __read(this.getImageArgs(arg.args)), false));
907
+ }
908
+ else if ('rr_type' in arg && arg.rr_type === 'HTMLImageElement') {
909
+ images.push(arg.src);
910
+ }
911
+ else if (arg instanceof Array) {
912
+ images.push.apply(images, __spreadArray([], __read(this.getImageArgs(arg)), false));
913
+ }
914
+ }
915
+ }
916
+ catch (e_14_1) { e_14 = { error: e_14_1 }; }
917
+ finally {
918
+ try {
919
+ if (args_2_1 && !args_2_1.done && (_a = args_2.return)) _a.call(args_2);
920
+ }
921
+ finally { if (e_14) throw e_14.error; }
922
+ }
923
+ return images;
924
+ };
925
+ Replayer.prototype.preloadAllImages = function () {
926
+ var e_15, _a;
927
+ var _this = this;
928
+ this.service.state;
929
+ var stateHandler = function () {
930
+ _this.service.state;
931
+ };
932
+ this.emitter.on(ReplayerEvents.Start, stateHandler);
933
+ this.emitter.on(ReplayerEvents.Pause, stateHandler);
934
+ var _loop_3 = function (event_3) {
935
+ if (event_3.type === EventType.IncrementalSnapshot &&
936
+ event_3.data.source === IncrementalSource.CanvasMutation)
937
+ if ('commands' in event_3.data) {
938
+ event_3.data.commands.forEach(function (c) { return _this.preloadImages(c, event_3); });
939
+ }
940
+ else {
941
+ this_3.preloadImages(event_3.data, event_3);
942
+ }
943
+ };
944
+ var this_3 = this;
945
+ try {
946
+ for (var _b = __values(this.service.state.context.events), _c = _b.next(); !_c.done; _c = _b.next()) {
947
+ var event_3 = _c.value;
948
+ _loop_3(event_3);
949
+ }
950
+ }
951
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
952
+ finally {
953
+ try {
954
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
955
+ }
956
+ finally { if (e_15) throw e_15.error; }
957
+ }
958
+ };
959
+ Replayer.prototype.preloadImages = function (data, event) {
960
+ var _this = this;
961
+ if (data.property === 'drawImage' &&
962
+ typeof data.args[0] === 'string' &&
963
+ !this.imageMap.has(event)) {
964
+ var canvas = document.createElement('canvas');
965
+ var ctx = canvas.getContext('2d');
966
+ var imgd = ctx === null || ctx === void 0 ? void 0 : ctx.createImageData(canvas.width, canvas.height);
967
+ imgd === null || imgd === void 0 ? void 0 : imgd.data;
968
+ JSON.parse(data.args[0]);
969
+ ctx === null || ctx === void 0 ? void 0 : ctx.putImageData(imgd, 0, 0);
970
+ }
971
+ else if (this.hasImageArg(data.args)) {
972
+ this.getImageArgs(data.args).forEach(function (url) {
973
+ var image = new Image();
974
+ image.src = url;
975
+ _this.imageMap.set(url, image);
976
+ });
977
+ }
978
+ };
979
+ Replayer.prototype.applyIncremental = function (e, isSync) {
980
+ var _this = this;
981
+ var _a, _b;
982
+ var d = e.data;
983
+ switch (d.source) {
984
+ case IncrementalSource.Mutation: {
985
+ if (isSync) {
986
+ d.adds.forEach(function (m) { return _this.treeIndex.add(m); });
987
+ d.texts.forEach(function (m) {
988
+ var target = _this.mirror.getNode(m.id);
989
+ var parent = target === null || target === void 0 ? void 0 : target.parentNode;
990
+ if (parent && _this.virtualStyleRulesMap.has(parent))
991
+ _this.virtualStyleRulesMap.delete(parent);
992
+ _this.treeIndex.text(m);
993
+ });
994
+ d.attributes.forEach(function (m) { return _this.treeIndex.attribute(m); });
995
+ d.removes.forEach(function (m) { return _this.treeIndex.remove(m, _this.mirror); });
996
+ }
997
+ try {
998
+ this.applyMutation(d, isSync);
999
+ }
1000
+ catch (error) {
1001
+ this.warn("Exception in mutation ".concat(error.message || error), d);
1002
+ }
1003
+ break;
1004
+ }
1005
+ case IncrementalSource.Drag:
1006
+ case IncrementalSource.TouchMove:
1007
+ case IncrementalSource.MouseMove:
1008
+ if (isSync) {
1009
+ var lastPosition = d.positions[d.positions.length - 1];
1010
+ this.mousePos = {
1011
+ x: lastPosition.x,
1012
+ y: lastPosition.y,
1013
+ id: lastPosition.id,
1014
+ debugData: d,
1015
+ };
1016
+ }
1017
+ else {
1018
+ d.positions.forEach(function (p) {
1019
+ var action = {
1020
+ doAction: function () {
1021
+ _this.moveAndHover(p.x, p.y, p.id, isSync, d);
1022
+ },
1023
+ delay: p.timeOffset +
1024
+ e.timestamp -
1025
+ _this.service.state.context.baselineTime,
1026
+ };
1027
+ _this.timer.addAction(action);
1028
+ });
1029
+ this.timer.addAction({
1030
+ doAction: function () { },
1031
+ delay: e.delay - ((_a = d.positions[0]) === null || _a === void 0 ? void 0 : _a.timeOffset),
1032
+ });
1033
+ }
1034
+ break;
1035
+ case IncrementalSource.MouseInteraction: {
1036
+ if (d.id === -1) {
1037
+ break;
1038
+ }
1039
+ var event_4 = new Event(MouseInteractions[d.type].toLowerCase());
1040
+ var target = this.mirror.getNode(d.id);
1041
+ if (!target) {
1042
+ return this.debugNodeNotFound(d, d.id);
1043
+ }
1044
+ this.emitter.emit(ReplayerEvents.MouseInteraction, {
1045
+ type: d.type,
1046
+ target: target,
1047
+ });
1048
+ var triggerFocus = this.config.triggerFocus;
1049
+ switch (d.type) {
1050
+ case MouseInteractions.Blur:
1051
+ if ('blur' in target) {
1052
+ target.blur();
1053
+ }
1054
+ break;
1055
+ case MouseInteractions.Focus:
1056
+ if (triggerFocus && target.focus) {
1057
+ target.focus({
1058
+ preventScroll: true,
1059
+ });
1060
+ }
1061
+ break;
1062
+ case MouseInteractions.Click:
1063
+ case MouseInteractions.TouchStart:
1064
+ case MouseInteractions.TouchEnd:
1065
+ if (isSync) {
1066
+ if (d.type === MouseInteractions.TouchStart) {
1067
+ this.touchActive = true;
1068
+ }
1069
+ else if (d.type === MouseInteractions.TouchEnd) {
1070
+ this.touchActive = false;
1071
+ }
1072
+ this.mousePos = {
1073
+ x: d.x,
1074
+ y: d.y,
1075
+ id: d.id,
1076
+ debugData: d,
1077
+ };
1078
+ }
1079
+ else {
1080
+ if (d.type === MouseInteractions.TouchStart) {
1081
+ this.tailPositions.length = 0;
1082
+ }
1083
+ this.moveAndHover(d.x, d.y, d.id, isSync, d);
1084
+ if (d.type === MouseInteractions.Click) {
1085
+ this.mouse.classList.remove('active');
1086
+ void this.mouse.offsetWidth;
1087
+ this.mouse.classList.add('active');
1088
+ }
1089
+ else if (d.type === MouseInteractions.TouchStart) {
1090
+ void this.mouse.offsetWidth;
1091
+ this.mouse.classList.add('touch-active');
1092
+ }
1093
+ else if (d.type === MouseInteractions.TouchEnd) {
1094
+ this.mouse.classList.remove('touch-active');
1095
+ }
1096
+ }
1097
+ break;
1098
+ case MouseInteractions.TouchCancel:
1099
+ if (isSync) {
1100
+ this.touchActive = false;
1101
+ }
1102
+ else {
1103
+ this.mouse.classList.remove('touch-active');
1104
+ }
1105
+ break;
1106
+ default:
1107
+ target.dispatchEvent(event_4);
1108
+ }
1109
+ break;
1110
+ }
1111
+ case IncrementalSource.Scroll: {
1112
+ if (d.id === -1) {
1113
+ break;
1114
+ }
1115
+ if (isSync) {
1116
+ this.treeIndex.scroll(d);
1117
+ break;
1118
+ }
1119
+ this.applyScroll(d, false);
1120
+ break;
1121
+ }
1122
+ case IncrementalSource.ViewportResize:
1123
+ this.emitter.emit(ReplayerEvents.Resize, {
1124
+ width: d.width,
1125
+ height: d.height,
1126
+ });
1127
+ break;
1128
+ case IncrementalSource.Input: {
1129
+ if (d.id === -1) {
1130
+ break;
1131
+ }
1132
+ if (isSync) {
1133
+ this.treeIndex.input(d);
1134
+ break;
1135
+ }
1136
+ this.applyInput(d);
1137
+ break;
1138
+ }
1139
+ case IncrementalSource.MediaInteraction: {
1140
+ var target = this.mirror.getNode(d.id);
1141
+ if (!target) {
1142
+ return this.debugNodeNotFound(d, d.id);
1143
+ }
1144
+ var mediaEl = target;
1145
+ try {
1146
+ if (d.currentTime) {
1147
+ mediaEl.currentTime = d.currentTime;
1148
+ }
1149
+ if (d.volume) {
1150
+ mediaEl.volume = d.volume;
1151
+ }
1152
+ if (d.muted) {
1153
+ mediaEl.muted = d.muted;
1154
+ }
1155
+ if (d.type === 1) {
1156
+ mediaEl.pause();
1157
+ }
1158
+ if (d.type === 0) {
1159
+ mediaEl.play();
1160
+ }
1161
+ }
1162
+ catch (error) {
1163
+ if (this.config.showWarning) {
1164
+ console.warn("Failed to replay media interactions: ".concat(error.message || error));
1165
+ }
1166
+ }
1167
+ break;
1168
+ }
1169
+ case IncrementalSource.StyleSheetRule: {
1170
+ var target = this.mirror.getNode(d.id);
1171
+ if (!target) {
1172
+ return this.debugNodeNotFound(d, d.id);
1173
+ }
1174
+ var styleEl = target;
1175
+ var parent_2 = target.parentNode;
1176
+ var usingVirtualParent_1 = this.fragmentParentMap.has(parent_2);
1177
+ var styleSheet_1 = usingVirtualParent_1 ? null : styleEl.sheet;
1178
+ var rules_1;
1179
+ if (!styleSheet_1) {
1180
+ if (this.virtualStyleRulesMap.has(target)) {
1181
+ rules_1 = this.virtualStyleRulesMap.get(target);
1182
+ }
1183
+ else {
1184
+ rules_1 = [];
1185
+ this.virtualStyleRulesMap.set(target, rules_1);
1186
+ }
1187
+ }
1188
+ if (d.adds) {
1189
+ d.adds.forEach(function (_a) {
1190
+ var rule = _a.rule, nestedIndex = _a.index;
1191
+ if (styleSheet_1) {
1192
+ try {
1193
+ if (Array.isArray(nestedIndex)) {
1194
+ var _b = getPositionsAndIndex(nestedIndex), positions = _b.positions, index = _b.index;
1195
+ var nestedRule = getNestedRule(styleSheet_1.cssRules, positions);
1196
+ nestedRule.insertRule(rule, index);
1197
+ }
1198
+ else {
1199
+ var index = nestedIndex === undefined
1200
+ ? undefined
1201
+ : Math.min(nestedIndex, styleSheet_1.cssRules.length);
1202
+ styleSheet_1.insertRule(rule, index);
1203
+ }
1204
+ }
1205
+ catch (e) {
1206
+ }
1207
+ }
1208
+ else {
1209
+ rules_1 === null || rules_1 === void 0 ? void 0 : rules_1.push({
1210
+ cssText: rule,
1211
+ index: nestedIndex,
1212
+ type: StyleRuleType.Insert,
1213
+ });
1214
+ }
1215
+ });
1216
+ }
1217
+ if (d.removes) {
1218
+ d.removes.forEach(function (_a) {
1219
+ var nestedIndex = _a.index;
1220
+ if (usingVirtualParent_1) {
1221
+ rules_1 === null || rules_1 === void 0 ? void 0 : rules_1.push({ index: nestedIndex, type: StyleRuleType.Remove });
1222
+ }
1223
+ else {
1224
+ try {
1225
+ if (Array.isArray(nestedIndex)) {
1226
+ var _b = getPositionsAndIndex(nestedIndex), positions = _b.positions, index = _b.index;
1227
+ var nestedRule = getNestedRule(styleSheet_1.cssRules, positions);
1228
+ nestedRule.deleteRule(index || 0);
1229
+ }
1230
+ else {
1231
+ styleSheet_1 === null || styleSheet_1 === void 0 ? void 0 : styleSheet_1.deleteRule(nestedIndex);
1232
+ }
1233
+ }
1234
+ catch (e) {
1235
+ }
1236
+ }
1237
+ });
1238
+ }
1239
+ break;
1240
+ }
1241
+ case IncrementalSource.StyleDeclaration: {
1242
+ var target = this.mirror.getNode(d.id);
1243
+ if (!target) {
1244
+ return this.debugNodeNotFound(d, d.id);
1245
+ }
1246
+ var styleEl = target;
1247
+ var parent_3 = target.parentNode;
1248
+ var usingVirtualParent = this.fragmentParentMap.has(parent_3);
1249
+ var styleSheet = usingVirtualParent ? null : styleEl.sheet;
1250
+ var rules = [];
1251
+ if (!styleSheet) {
1252
+ if (this.virtualStyleRulesMap.has(target)) {
1253
+ rules = this.virtualStyleRulesMap.get(target);
1254
+ }
1255
+ else {
1256
+ rules = [];
1257
+ this.virtualStyleRulesMap.set(target, rules);
1258
+ }
1259
+ }
1260
+ if (d.set) {
1261
+ if (styleSheet) {
1262
+ var rule = getNestedRule(styleSheet.rules, d.index);
1263
+ rule.style.setProperty(d.set.property, d.set.value, d.set.priority);
1264
+ }
1265
+ else {
1266
+ rules.push(__assign({ type: StyleRuleType.SetProperty, index: d.index }, d.set));
1267
+ }
1268
+ }
1269
+ if (d.remove) {
1270
+ if (styleSheet) {
1271
+ var rule = getNestedRule(styleSheet.rules, d.index);
1272
+ rule.style.removeProperty(d.remove.property);
1273
+ }
1274
+ else {
1275
+ rules.push(__assign({ type: StyleRuleType.RemoveProperty, index: d.index }, d.remove));
1276
+ }
1277
+ }
1278
+ break;
1279
+ }
1280
+ case IncrementalSource.CanvasMutation: {
1281
+ if (!this.config.UNSAFE_replayCanvas) {
1282
+ return;
1283
+ }
1284
+ var target = this.mirror.getNode(d.id);
1285
+ if (!target) {
1286
+ return this.debugNodeNotFound(d, d.id);
1287
+ }
1288
+ canvasMutation({
1289
+ event: e,
1290
+ mutation: d,
1291
+ target: target,
1292
+ imageMap: this.imageMap,
1293
+ errorHandler: this.warnCanvasMutationFailed.bind(this),
1294
+ });
1295
+ break;
1296
+ }
1297
+ case IncrementalSource.Font: {
1298
+ try {
1299
+ var fontFace = new FontFace(d.family, d.buffer ? new Uint8Array(JSON.parse(d.fontSource)) : d.fontSource, d.descriptors);
1300
+ (_b = this.iframe.contentDocument) === null || _b === void 0 ? void 0 : _b.fonts.add(fontFace);
1301
+ }
1302
+ catch (error) {
1303
+ if (this.config.showWarning) {
1304
+ console.warn(error);
1305
+ }
1306
+ }
1307
+ break;
1308
+ }
1309
+ }
1310
+ };
1311
+ Replayer.prototype.applyMutation = function (d, useVirtualParent) {
1312
+ var e_16, _a;
1313
+ var _this = this;
1314
+ d.removes.forEach(function (mutation) {
1315
+ var target = _this.mirror.getNode(mutation.id);
1316
+ if (!target) {
1317
+ if (d.removes.find(function (r) { return r.id === mutation.parentId; })) {
1318
+ return;
1319
+ }
1320
+ return _this.warnNodeNotFound(d, mutation.id);
1321
+ }
1322
+ if (_this.virtualStyleRulesMap.has(target)) {
1323
+ _this.virtualStyleRulesMap.delete(target);
1324
+ }
1325
+ var parent = _this.mirror.getNode(mutation.parentId);
1326
+ if (!parent) {
1327
+ return _this.warnNodeNotFound(d, mutation.parentId);
1328
+ }
1329
+ if (mutation.isShadow && hasShadowRoot(parent)) {
1330
+ parent = parent.shadowRoot;
1331
+ }
1332
+ _this.mirror.removeNodeFromMap(target);
1333
+ if (parent) {
1334
+ var realTarget = null;
1335
+ var realParent = '__sn' in parent ? _this.fragmentParentMap.get(parent) : undefined;
1336
+ if (realParent && realParent.contains(target)) {
1337
+ parent = realParent;
1338
+ }
1339
+ else if (_this.fragmentParentMap.has(target)) {
1340
+ realTarget = _this.fragmentParentMap.get(target);
1341
+ _this.fragmentParentMap.delete(target);
1342
+ target = realTarget;
1343
+ }
1344
+ try {
1345
+ parent.removeChild(target);
1346
+ }
1347
+ catch (error) {
1348
+ if (error instanceof DOMException) {
1349
+ _this.warn('parent could not remove child in mutation', parent, realParent, target, realTarget, d);
1350
+ }
1351
+ else {
1352
+ throw error;
1353
+ }
1354
+ }
1355
+ }
1356
+ });
1357
+ var legacy_missingNodeMap = __assign({}, this.legacy_missingNodeRetryMap);
1358
+ var queue = [];
1359
+ var nextNotInDOM = function (mutation) {
1360
+ var next = null;
1361
+ if (mutation.nextId) {
1362
+ next = _this.mirror.getNode(mutation.nextId);
1363
+ }
1364
+ if (mutation.nextId !== null &&
1365
+ mutation.nextId !== undefined &&
1366
+ mutation.nextId !== -1 &&
1367
+ !next) {
1368
+ return true;
1369
+ }
1370
+ return false;
1371
+ };
1372
+ var appendNode = function (mutation) {
1373
+ var e_17, _a;
1374
+ var _b, _c;
1375
+ if (!_this.iframe.contentDocument) {
1376
+ return console.warn('Looks like your replayer has been destroyed.');
1377
+ }
1378
+ var parent = _this.mirror.getNode(mutation.parentId);
1379
+ if (!parent) {
1380
+ if (mutation.node.type === NodeType.Document) {
1381
+ return _this.newDocumentQueue.push(mutation);
1382
+ }
1383
+ return queue.push(mutation);
1384
+ }
1385
+ var parentInDocument = null;
1386
+ if (_this.iframe.contentDocument.contains) {
1387
+ parentInDocument = _this.iframe.contentDocument.contains(parent);
1388
+ }
1389
+ else if (_this.iframe.contentDocument.body.contains) {
1390
+ parentInDocument = _this.iframe.contentDocument.body.contains(parent);
1391
+ }
1392
+ var hasIframeChild = ((_c = (_b = parent).getElementsByTagName) === null || _c === void 0 ? void 0 : _c.call(_b, 'iframe').length) > 0;
1393
+ if (useVirtualParent &&
1394
+ parentInDocument &&
1395
+ !isIframeINode(parent) &&
1396
+ !hasIframeChild) {
1397
+ var virtualParent = document.createDocumentFragment();
1398
+ _this.mirror.map[mutation.parentId] = virtualParent;
1399
+ _this.fragmentParentMap.set(virtualParent, parent);
1400
+ _this.storeState(parent);
1401
+ while (parent.firstChild) {
1402
+ virtualParent.appendChild(parent.firstChild);
1403
+ }
1404
+ parent = virtualParent;
1405
+ }
1406
+ if (mutation.node.isShadow) {
1407
+ if (!hasShadowRoot(parent)) {
1408
+ parent.attachShadow({ mode: 'open' });
1409
+ parent = parent.shadowRoot;
1410
+ }
1411
+ else
1412
+ parent = parent.shadowRoot;
1413
+ }
1414
+ var previous = null;
1415
+ var next = null;
1416
+ if (mutation.previousId) {
1417
+ previous = _this.mirror.getNode(mutation.previousId);
1418
+ }
1419
+ if (mutation.nextId) {
1420
+ next = _this.mirror.getNode(mutation.nextId);
1421
+ }
1422
+ if (nextNotInDOM(mutation)) {
1423
+ return queue.push(mutation);
1424
+ }
1425
+ if (mutation.node.rootId && !_this.mirror.getNode(mutation.node.rootId)) {
1426
+ return;
1427
+ }
1428
+ var targetDoc = mutation.node.rootId
1429
+ ? _this.mirror.getNode(mutation.node.rootId)
1430
+ : _this.iframe.contentDocument;
1431
+ if (isIframeINode(parent)) {
1432
+ _this.attachDocumentToIframe(mutation, parent);
1433
+ return;
1434
+ }
1435
+ var target = buildNodeWithSN(mutation.node, {
1436
+ doc: targetDoc,
1437
+ map: _this.mirror.map,
1438
+ skipChild: true,
1439
+ hackCss: true,
1440
+ cache: _this.cache,
1441
+ });
1442
+ if (mutation.previousId === -1 || mutation.nextId === -1) {
1443
+ legacy_missingNodeMap[mutation.node.id] = {
1444
+ node: target,
1445
+ mutation: mutation,
1446
+ };
1447
+ return;
1448
+ }
1449
+ if ('__sn' in parent &&
1450
+ parent.__sn.type === NodeType.Element &&
1451
+ parent.__sn.tagName === 'textarea' &&
1452
+ mutation.node.type === NodeType.Text) {
1453
+ try {
1454
+ for (var _d = __values(Array.from(parent.childNodes)), _e = _d.next(); !_e.done; _e = _d.next()) {
1455
+ var c = _e.value;
1456
+ if (c.nodeType === parent.TEXT_NODE) {
1457
+ parent.removeChild(c);
1458
+ }
1459
+ }
1460
+ }
1461
+ catch (e_17_1) { e_17 = { error: e_17_1 }; }
1462
+ finally {
1463
+ try {
1464
+ if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
1465
+ }
1466
+ finally { if (e_17) throw e_17.error; }
1467
+ }
1468
+ }
1469
+ if (previous && previous.nextSibling && previous.nextSibling.parentNode) {
1470
+ parent.insertBefore(target, previous.nextSibling);
1471
+ }
1472
+ else if (next && next.parentNode) {
1473
+ parent.contains(next)
1474
+ ? parent.insertBefore(target, next)
1475
+ : parent.insertBefore(target, null);
1476
+ }
1477
+ else {
1478
+ if (parent === targetDoc) {
1479
+ while (targetDoc.firstChild) {
1480
+ targetDoc.removeChild(targetDoc.firstChild);
1481
+ }
1482
+ }
1483
+ parent.appendChild(target);
1484
+ }
1485
+ if (isIframeINode(target)) {
1486
+ var mutationInQueue_1 = _this.newDocumentQueue.find(function (m) { return m.parentId === target.__sn.id; });
1487
+ if (mutationInQueue_1) {
1488
+ _this.attachDocumentToIframe(mutationInQueue_1, target);
1489
+ _this.newDocumentQueue = _this.newDocumentQueue.filter(function (m) { return m !== mutationInQueue_1; });
1490
+ }
1491
+ }
1492
+ if (mutation.previousId || mutation.nextId) {
1493
+ _this.legacy_resolveMissingNode(legacy_missingNodeMap, parent, target, mutation);
1494
+ }
1495
+ };
1496
+ d.adds.forEach(function (mutation) {
1497
+ appendNode(mutation);
1498
+ });
1499
+ var startTime = Date.now();
1500
+ while (queue.length) {
1501
+ var resolveTrees = queueToResolveTrees(queue);
1502
+ queue.length = 0;
1503
+ if (Date.now() - startTime > 500) {
1504
+ this.warn('Timeout in the loop, please check the resolve tree data:', resolveTrees);
1505
+ break;
1506
+ }
1507
+ try {
1508
+ for (var resolveTrees_1 = (e_16 = void 0, __values(resolveTrees)), resolveTrees_1_1 = resolveTrees_1.next(); !resolveTrees_1_1.done; resolveTrees_1_1 = resolveTrees_1.next()) {
1509
+ var tree = resolveTrees_1_1.value;
1510
+ var parent_4 = this.mirror.getNode(tree.value.parentId);
1511
+ if (!parent_4) {
1512
+ this.debug('Drop resolve tree since there is no parent for the root node.', tree);
1513
+ }
1514
+ else {
1515
+ iterateResolveTree(tree, function (mutation) {
1516
+ appendNode(mutation);
1517
+ });
1518
+ }
1519
+ }
1520
+ }
1521
+ catch (e_16_1) { e_16 = { error: e_16_1 }; }
1522
+ finally {
1523
+ try {
1524
+ if (resolveTrees_1_1 && !resolveTrees_1_1.done && (_a = resolveTrees_1.return)) _a.call(resolveTrees_1);
1525
+ }
1526
+ finally { if (e_16) throw e_16.error; }
1527
+ }
1528
+ }
1529
+ if (Object.keys(legacy_missingNodeMap).length) {
1530
+ Object.assign(this.legacy_missingNodeRetryMap, legacy_missingNodeMap);
1531
+ }
1532
+ d.texts.forEach(function (mutation) {
1533
+ var target = _this.mirror.getNode(mutation.id);
1534
+ if (!target) {
1535
+ if (d.removes.find(function (r) { return r.id === mutation.id; })) {
1536
+ return;
1537
+ }
1538
+ return _this.warnNodeNotFound(d, mutation.id);
1539
+ }
1540
+ if (_this.fragmentParentMap.has(target)) {
1541
+ target = _this.fragmentParentMap.get(target);
1542
+ }
1543
+ target.textContent = mutation.value;
1544
+ });
1545
+ d.attributes.forEach(function (mutation) {
1546
+ var target = _this.mirror.getNode(mutation.id);
1547
+ if (!target) {
1548
+ if (d.removes.find(function (r) { return r.id === mutation.id; })) {
1549
+ return;
1550
+ }
1551
+ return _this.warnNodeNotFound(d, mutation.id);
1552
+ }
1553
+ if (_this.fragmentParentMap.has(target)) {
1554
+ target = _this.fragmentParentMap.get(target);
1555
+ }
1556
+ for (var attributeName in mutation.attributes) {
1557
+ if (typeof attributeName === 'string') {
1558
+ var value = mutation.attributes[attributeName];
1559
+ if (value === null) {
1560
+ target.removeAttribute(attributeName);
1561
+ }
1562
+ else if (typeof value === 'string') {
1563
+ try {
1564
+ target.setAttribute(attributeName, value);
1565
+ }
1566
+ catch (error) {
1567
+ if (_this.config.showWarning) {
1568
+ console.warn('An error occurred may due to the checkout feature.', error);
1569
+ }
1570
+ }
1571
+ }
1572
+ else if (attributeName === 'style') {
1573
+ var styleValues = value;
1574
+ var targetEl = target;
1575
+ for (var s in styleValues) {
1576
+ if (styleValues[s] === false) {
1577
+ targetEl.style.removeProperty(s);
1578
+ }
1579
+ else if (styleValues[s] instanceof Array) {
1580
+ var svp = styleValues[s];
1581
+ targetEl.style.setProperty(s, svp[0], svp[1]);
1582
+ }
1583
+ else {
1584
+ var svs = styleValues[s];
1585
+ targetEl.style.setProperty(s, svs);
1586
+ }
1587
+ }
1588
+ }
1589
+ }
1590
+ }
1591
+ });
1592
+ };
1593
+ Replayer.prototype.applyScroll = function (d, isSync) {
1594
+ var target = this.mirror.getNode(d.id);
1595
+ if (!target) {
1596
+ return this.debugNodeNotFound(d, d.id);
1597
+ }
1598
+ if (target === this.iframe.contentDocument) {
1599
+ this.iframe.contentWindow.scrollTo({
1600
+ top: d.y,
1601
+ left: d.x,
1602
+ behavior: isSync ? 'auto' : 'smooth',
1603
+ });
1604
+ }
1605
+ else if (target.__sn.type === NodeType.Document) {
1606
+ target.defaultView.scrollTo({
1607
+ top: d.y,
1608
+ left: d.x,
1609
+ behavior: isSync ? 'auto' : 'smooth',
1610
+ });
1611
+ }
1612
+ else {
1613
+ try {
1614
+ target.scrollTop = d.y;
1615
+ target.scrollLeft = d.x;
1616
+ }
1617
+ catch (error) {
1618
+ }
1619
+ }
1620
+ };
1621
+ Replayer.prototype.applyInput = function (d) {
1622
+ var target = this.mirror.getNode(d.id);
1623
+ if (!target) {
1624
+ return this.debugNodeNotFound(d, d.id);
1625
+ }
1626
+ try {
1627
+ target.checked = d.isChecked;
1628
+ target.value = d.text;
1629
+ }
1630
+ catch (error) {
1631
+ }
1632
+ };
1633
+ Replayer.prototype.applyText = function (d, mutation) {
1634
+ var target = this.mirror.getNode(d.id);
1635
+ if (!target) {
1636
+ return this.debugNodeNotFound(mutation, d.id);
1637
+ }
1638
+ try {
1639
+ target.textContent = d.value;
1640
+ }
1641
+ catch (error) {
1642
+ }
1643
+ };
1644
+ Replayer.prototype.legacy_resolveMissingNode = function (map, parent, target, targetMutation) {
1645
+ var previousId = targetMutation.previousId, nextId = targetMutation.nextId;
1646
+ var previousInMap = previousId && map[previousId];
1647
+ var nextInMap = nextId && map[nextId];
1648
+ if (previousInMap) {
1649
+ var _a = previousInMap, node = _a.node, mutation = _a.mutation;
1650
+ parent.insertBefore(node, target);
1651
+ delete map[mutation.node.id];
1652
+ delete this.legacy_missingNodeRetryMap[mutation.node.id];
1653
+ if (mutation.previousId || mutation.nextId) {
1654
+ this.legacy_resolveMissingNode(map, parent, node, mutation);
1655
+ }
1656
+ }
1657
+ if (nextInMap) {
1658
+ var _b = nextInMap, node = _b.node, mutation = _b.mutation;
1659
+ parent.insertBefore(node, target.nextSibling);
1660
+ delete map[mutation.node.id];
1661
+ delete this.legacy_missingNodeRetryMap[mutation.node.id];
1662
+ if (mutation.previousId || mutation.nextId) {
1663
+ this.legacy_resolveMissingNode(map, parent, node, mutation);
1664
+ }
1665
+ }
1666
+ };
1667
+ Replayer.prototype.moveAndHover = function (x, y, id, isSync, debugData) {
1668
+ var target = this.mirror.getNode(id);
1669
+ if (!target) {
1670
+ return this.debugNodeNotFound(debugData, id);
1671
+ }
1672
+ var base = getBaseDimension(target, this.iframe);
1673
+ var _x = x * base.absoluteScale + base.x;
1674
+ var _y = y * base.absoluteScale + base.y;
1675
+ this.mouse.style.left = "".concat(_x, "px");
1676
+ this.mouse.style.top = "".concat(_y, "px");
1677
+ if (!isSync) {
1678
+ this.drawMouseTail({ x: _x, y: _y });
1679
+ }
1680
+ this.hoverElements(target);
1681
+ };
1682
+ Replayer.prototype.drawMouseTail = function (position) {
1683
+ var _this = this;
1684
+ if (!this.mouseTail) {
1685
+ return;
1686
+ }
1687
+ var _a = this.config.mouseTail === true
1688
+ ? defaultMouseTailConfig
1689
+ : Object.assign({}, defaultMouseTailConfig, this.config.mouseTail), lineCap = _a.lineCap, lineWidth = _a.lineWidth, strokeStyle = _a.strokeStyle, duration = _a.duration;
1690
+ var draw = function () {
1691
+ if (!_this.mouseTail) {
1692
+ return;
1693
+ }
1694
+ var ctx = _this.mouseTail.getContext('2d');
1695
+ if (!ctx || !_this.tailPositions.length) {
1696
+ return;
1697
+ }
1698
+ ctx.clearRect(0, 0, _this.mouseTail.width, _this.mouseTail.height);
1699
+ ctx.beginPath();
1700
+ ctx.lineWidth = lineWidth;
1701
+ ctx.lineCap = lineCap;
1702
+ ctx.strokeStyle = strokeStyle;
1703
+ ctx.moveTo(_this.tailPositions[0].x, _this.tailPositions[0].y);
1704
+ _this.tailPositions.forEach(function (p) { return ctx.lineTo(p.x, p.y); });
1705
+ ctx.stroke();
1706
+ };
1707
+ this.tailPositions.push(position);
1708
+ draw();
1709
+ setTimeout(function () {
1710
+ _this.tailPositions = _this.tailPositions.filter(function (p) { return p !== position; });
1711
+ draw();
1712
+ }, duration / this.speedService.state.context.timer.speed);
1713
+ };
1714
+ Replayer.prototype.hoverElements = function (el) {
1715
+ var _a;
1716
+ (_a = this.iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.querySelectorAll('.\\:hover').forEach(function (hoveredEl) {
1717
+ hoveredEl.classList.remove(':hover');
1718
+ });
1719
+ var currentEl = el;
1720
+ while (currentEl) {
1721
+ if (currentEl.classList) {
1722
+ currentEl.classList.add(':hover');
1723
+ }
1724
+ currentEl = currentEl.parentElement;
1725
+ }
1726
+ };
1727
+ Replayer.prototype.isUserInteraction = function (event) {
1728
+ if (event.type !== EventType.IncrementalSnapshot) {
1729
+ return false;
1730
+ }
1731
+ return (event.data.source > IncrementalSource.Mutation &&
1732
+ event.data.source <= IncrementalSource.Input);
1733
+ };
1734
+ Replayer.prototype.backToNormal = function () {
1735
+ this.nextUserInteractionEvent = null;
1736
+ if (this.speedService.state.matches('normal')) {
1737
+ return;
1738
+ }
1739
+ this.speedService.send({ type: 'BACK_TO_NORMAL' });
1740
+ this.emitter.emit(ReplayerEvents.SkipEnd, {
1741
+ speed: this.speedService.state.context.normalSpeed,
1742
+ });
1743
+ };
1744
+ Replayer.prototype.restoreRealParent = function (frag, parent) {
1745
+ this.mirror.map[parent.__sn.id] = parent;
1746
+ if (parent.__sn.type === NodeType.Element &&
1747
+ parent.__sn.tagName === 'textarea' &&
1748
+ frag.textContent) {
1749
+ parent.value = frag.textContent;
1750
+ }
1751
+ parent.appendChild(frag);
1752
+ this.restoreState(parent);
1753
+ };
1754
+ Replayer.prototype.storeState = function (parent) {
1755
+ var e_18, _a;
1756
+ if (parent) {
1757
+ if (parent.nodeType === parent.ELEMENT_NODE) {
1758
+ var parentElement = parent;
1759
+ if (parentElement.scrollLeft || parentElement.scrollTop) {
1760
+ this.elementStateMap.set(parent, {
1761
+ scroll: [parentElement.scrollLeft, parentElement.scrollTop],
1762
+ });
1763
+ }
1764
+ if (parentElement.tagName === 'STYLE')
1765
+ storeCSSRules(parentElement, this.virtualStyleRulesMap);
1766
+ var children = parentElement.children;
1767
+ try {
1768
+ for (var _b = __values(Array.from(children)), _c = _b.next(); !_c.done; _c = _b.next()) {
1769
+ var child = _c.value;
1770
+ this.storeState(child);
1771
+ }
1772
+ }
1773
+ catch (e_18_1) { e_18 = { error: e_18_1 }; }
1774
+ finally {
1775
+ try {
1776
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1777
+ }
1778
+ finally { if (e_18) throw e_18.error; }
1779
+ }
1780
+ }
1781
+ }
1782
+ };
1783
+ Replayer.prototype.restoreState = function (parent) {
1784
+ var e_19, _a;
1785
+ if (parent.nodeType === parent.ELEMENT_NODE) {
1786
+ var parentElement = parent;
1787
+ if (this.elementStateMap.has(parent)) {
1788
+ var storedState = this.elementStateMap.get(parent);
1789
+ if (storedState.scroll) {
1790
+ parentElement.scrollLeft = storedState.scroll[0];
1791
+ parentElement.scrollTop = storedState.scroll[1];
1792
+ }
1793
+ this.elementStateMap.delete(parent);
1794
+ }
1795
+ var children = parentElement.children;
1796
+ try {
1797
+ for (var _b = __values(Array.from(children)), _c = _b.next(); !_c.done; _c = _b.next()) {
1798
+ var child = _c.value;
1799
+ this.restoreState(child);
1800
+ }
1801
+ }
1802
+ catch (e_19_1) { e_19 = { error: e_19_1 }; }
1803
+ finally {
1804
+ try {
1805
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1806
+ }
1807
+ finally { if (e_19) throw e_19.error; }
1808
+ }
1809
+ }
1810
+ };
1811
+ Replayer.prototype.restoreNodeSheet = function (node) {
1812
+ var storedRules = this.virtualStyleRulesMap.get(node);
1813
+ if (node.nodeName !== 'STYLE') {
1814
+ return;
1815
+ }
1816
+ if (!storedRules) {
1817
+ return;
1818
+ }
1819
+ var styleNode = node;
1820
+ applyVirtualStyleRulesToNode(storedRules, styleNode);
1821
+ };
1822
+ Replayer.prototype.warnNodeNotFound = function (d, id) {
1823
+ if (this.treeIndex.idRemoved(id)) {
1824
+ this.warn("Node with id '".concat(id, "' was previously removed. "), d);
1825
+ }
1826
+ else {
1827
+ this.warn("Node with id '".concat(id, "' not found. "), d);
1828
+ }
1829
+ };
1830
+ Replayer.prototype.warnCanvasMutationFailed = function (d, error) {
1831
+ this.warn("Has error on canvas update", error, 'canvas mutation:', d);
1832
+ };
1833
+ Replayer.prototype.debugNodeNotFound = function (d, id) {
1834
+ if (this.treeIndex.idRemoved(id)) {
1835
+ this.debug(REPLAY_CONSOLE_PREFIX, "Node with id '".concat(id, "' was previously removed. "), d);
1836
+ }
1837
+ else {
1838
+ this.debug(REPLAY_CONSOLE_PREFIX, "Node with id '".concat(id, "' not found. "), d);
1839
+ }
1840
+ };
1841
+ Replayer.prototype.warn = function () {
1842
+ var args = [];
1843
+ for (var _i = 0; _i < arguments.length; _i++) {
1844
+ args[_i] = arguments[_i];
1845
+ }
1846
+ if (!this.config.showWarning) {
1847
+ return;
1848
+ }
1849
+ console.warn.apply(console, __spreadArray([REPLAY_CONSOLE_PREFIX], __read(args), false));
1850
+ };
1851
+ Replayer.prototype.debug = function () {
1852
+ var args = [];
1853
+ for (var _i = 0; _i < arguments.length; _i++) {
1854
+ args[_i] = arguments[_i];
1855
+ }
1856
+ if (!this.config.showDebug) {
1857
+ return;
1858
+ }
1859
+ console.log.apply(console, __spreadArray([REPLAY_CONSOLE_PREFIX], __read(args), false));
1860
+ };
1861
+ return Replayer;
1862
+ }());
1863
+
1864
+ export { Replayer };