@openreplay/tracker 3.4.10 → 3.4.11

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.
@@ -1,6 +1,6 @@
1
1
  import Message from "../messages/message.js";
2
2
  import Nodes from "./nodes.js";
3
- import Observer from "./observer/top_observer.js";
3
+ import Observer from "./observer.js";
4
4
  import Ticker from "./ticker.js";
5
5
  import type { Options as ObserverOptions } from "./observer.js";
6
6
  import type { Options as WebworkerOptions } from "../messages/webworker.js";
package/cjs/app/index.js CHANGED
@@ -4,7 +4,7 @@ exports.DEFAULT_INGEST_POINT = void 0;
4
4
  const utils_js_1 = require("../utils.js");
5
5
  const index_js_1 = require("../messages/index.js");
6
6
  const nodes_js_1 = require("./nodes.js");
7
- const top_observer_js_1 = require("./observer/top_observer.js");
7
+ const observer_js_1 = require("./observer.js");
8
8
  const ticker_js_1 = require("./ticker.js");
9
9
  const performance_js_1 = require("../modules/performance.js");
10
10
  // TODO: use backendHost only
@@ -17,7 +17,7 @@ class App {
17
17
  this.commitCallbacks = [];
18
18
  this._sessionID = null;
19
19
  this.isActive = false;
20
- this.version = '3.4.10';
20
+ this.version = '3.4.11';
21
21
  this.projectKey = projectKey;
22
22
  this.options = Object.assign({
23
23
  revID: '',
@@ -39,7 +39,7 @@ class App {
39
39
  }
40
40
  this.revID = this.options.revID;
41
41
  this.nodes = new nodes_js_1.default(this.options.node_id);
42
- this.observer = new top_observer_js_1.default(this, this.options);
42
+ this.observer = new observer_js_1.default(this, this.options);
43
43
  this.ticker = new ticker_js_1.default(this);
44
44
  this.ticker.attach(() => this.commit());
45
45
  try {
package/cjs/index.js CHANGED
@@ -115,7 +115,7 @@ class API {
115
115
  // no-cors issue only with text/plain or not-set Content-Type
116
116
  // req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
117
117
  req.send(JSON.stringify({
118
- trackerVersion: '3.4.10',
118
+ trackerVersion: '3.4.11',
119
119
  projectKey: options.projectKey,
120
120
  doNotTrack,
121
121
  // TODO: add precise reason (an exact API missing)
@@ -1,6 +1,6 @@
1
1
  import Message from "../messages/message.js";
2
2
  import Nodes from "./nodes.js";
3
- import Observer from "./observer/top_observer.js";
3
+ import Observer from "./observer.js";
4
4
  import Ticker from "./ticker.js";
5
5
  import type { Options as ObserverOptions } from "./observer.js";
6
6
  import type { Options as WebworkerOptions } from "../messages/webworker.js";
package/lib/app/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { timestamp, log, warn } from "../utils.js";
2
2
  import { Timestamp } from "../messages/index.js";
3
3
  import Nodes from "./nodes.js";
4
- import Observer from "./observer/top_observer.js";
4
+ import Observer from "./observer.js";
5
5
  import Ticker from "./ticker.js";
6
6
  import { deviceMemory, jsHeapSizeLimit } from "../modules/performance.js";
7
7
  // TODO: use backendHost only
@@ -14,7 +14,7 @@ export default class App {
14
14
  this.commitCallbacks = [];
15
15
  this._sessionID = null;
16
16
  this.isActive = false;
17
- this.version = '3.4.10';
17
+ this.version = '3.4.11';
18
18
  this.projectKey = projectKey;
19
19
  this.options = Object.assign({
20
20
  revID: '',
package/lib/index.js CHANGED
@@ -111,7 +111,7 @@ export default class API {
111
111
  // no-cors issue only with text/plain or not-set Content-Type
112
112
  // req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
113
113
  req.send(JSON.stringify({
114
- trackerVersion: '3.4.10',
114
+ trackerVersion: '3.4.11',
115
115
  projectKey: options.projectKey,
116
116
  doNotTrack,
117
117
  // TODO: add precise reason (an exact API missing)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openreplay/tracker",
3
3
  "description": "The OpenReplay tracker main package",
4
- "version": "3.4.10",
4
+ "version": "3.4.11",
5
5
  "keywords": [
6
6
  "logging",
7
7
  "replay"
@@ -1,4 +0,0 @@
1
- import Observer from "./observer.js";
2
- export default class IFrameObserver extends Observer {
3
- observe(iframe: HTMLIFrameElement): void;
4
- }
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const observer_js_1 = require("./observer.js");
4
- const index_js_1 = require("../../messages/index.js");
5
- class IFrameObserver extends observer_js_1.default {
6
- observe(iframe) {
7
- const doc = iframe.contentDocument;
8
- const hostID = this.app.nodes.getID(iframe);
9
- if (!doc || hostID === undefined) {
10
- return;
11
- } //log TODO common app.logger
12
- // Have to observe document, because the inner <html> might be changed
13
- this.observeRoot(doc, (docID) => {
14
- if (docID === undefined) {
15
- console.log("OpenReplay: Iframe document not bound");
16
- return;
17
- }
18
- this.app.send((0, index_js_1.CreateIFrameDocument)(hostID, docID));
19
- });
20
- }
21
- }
22
- exports.default = IFrameObserver;
@@ -1,49 +0,0 @@
1
- import App from "../index.js";
2
- export interface Window extends globalThis.Window {
3
- HTMLInputElement: typeof HTMLInputElement;
4
- HTMLLinkElement: typeof HTMLLinkElement;
5
- HTMLStyleElement: typeof HTMLStyleElement;
6
- SVGStyleElement: typeof SVGStyleElement;
7
- HTMLIFrameElement: typeof HTMLIFrameElement;
8
- Text: typeof Text;
9
- Element: typeof Element;
10
- ShadowRoot: typeof ShadowRoot;
11
- }
12
- declare type WindowConstructor = Document | Element | Text | ShadowRoot | HTMLInputElement | HTMLLinkElement | HTMLStyleElement | HTMLIFrameElement;
13
- declare type Constructor<T> = {
14
- new (...args: any[]): T;
15
- name: string;
16
- };
17
- export declare function isInstance<T extends WindowConstructor>(node: Node, constr: Constructor<T>): node is T;
18
- export interface Options {
19
- obscureTextEmails: boolean;
20
- obscureTextNumbers: boolean;
21
- }
22
- export default abstract class Observer<AdditionalOptions = {}> {
23
- protected readonly app: App;
24
- protected readonly context: Window;
25
- private readonly observer;
26
- private readonly commited;
27
- private readonly recents;
28
- private readonly indexes;
29
- private readonly attributesList;
30
- private readonly textSet;
31
- private readonly textMasked;
32
- protected readonly options: Options & AdditionalOptions;
33
- private readonly inUpperContext;
34
- constructor(app: App, options: Partial<Options> & AdditionalOptions, context?: Window);
35
- private clear;
36
- private sendNodeAttribute;
37
- getInnerTextSecure(el: HTMLElement): string;
38
- private checkObscure;
39
- private sendNodeData;
40
- private bindNode;
41
- private bindTree;
42
- private unbindNode;
43
- private _commitNode;
44
- private commitNode;
45
- private commitNodes;
46
- protected observeRoot(node: Node, beforeCommit: (id?: number) => unknown, nodeToBind?: Node): void;
47
- disconnect(): void;
48
- }
49
- export {};
@@ -1,374 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isInstance = void 0;
4
- const utils_js_1 = require("../../utils.js");
5
- const index_js_1 = require("../../messages/index.js");
6
- function isSVGElement(node) {
7
- return node.namespaceURI === 'http://www.w3.org/2000/svg';
8
- }
9
- // TODO: we need a type expert here so we won't have to ignore the lines
10
- // TODO: use it everywhere (static function; export from which file? <-- global Window typing required)
11
- function isInstance(node, constr) {
12
- const doc = node.ownerDocument;
13
- if (!doc) { // null if Document
14
- return constr.name === 'Document';
15
- }
16
- let context =
17
- // @ts-ignore (for EI, Safary)
18
- doc.parentWindow ||
19
- doc.defaultView; // TODO: smart global typing for Window object
20
- while (context.parent && context.parent !== context) {
21
- // @ts-ignore
22
- if (node instanceof context[constr.name]) {
23
- return true;
24
- }
25
- // @ts-ignore
26
- context = context.parent;
27
- }
28
- // @ts-ignore
29
- return node instanceof context[constr.name];
30
- }
31
- exports.isInstance = isInstance;
32
- function isIgnored(node) {
33
- if (isInstance(node, Text)) {
34
- return false;
35
- }
36
- if (!isInstance(node, Element)) {
37
- return true;
38
- }
39
- const tag = node.tagName.toUpperCase();
40
- if (tag === 'LINK') {
41
- const rel = node.getAttribute('rel');
42
- const as = node.getAttribute('as');
43
- return !((rel === null || rel === void 0 ? void 0 : rel.includes('stylesheet')) || as === "style" || as === "font");
44
- }
45
- return (tag === 'SCRIPT' ||
46
- tag === 'NOSCRIPT' ||
47
- tag === 'META' ||
48
- tag === 'TITLE' ||
49
- tag === 'BASE');
50
- }
51
- function isRootNode(node) {
52
- return isInstance(node, Document) || isInstance(node, ShadowRoot);
53
- }
54
- function isObservable(node) {
55
- if (isRootNode(node)) {
56
- return true;
57
- }
58
- return !isIgnored(node);
59
- }
60
- class Observer {
61
- constructor(app, options, context = window) {
62
- this.app = app;
63
- this.context = context;
64
- this.options = Object.assign({
65
- obscureTextEmails: true,
66
- obscureTextNumbers: false,
67
- }, options);
68
- this.inUpperContext = context.parent === context;
69
- this.observer = new MutationObserver(this.app.safe((mutations) => {
70
- for (const mutation of mutations) {
71
- const target = mutation.target;
72
- const type = mutation.type;
73
- // TODO TODO TODO: move to iframe_observer/remove??? (check if )
74
- // Special case
75
- // 'childList' on Document might happen in case of iframe.
76
- // TODO: generalize as much as possible
77
- // if (isInstance(target, Document) // Also ShadowRoot can be here
78
- // && type === 'childList'
79
- // //&& new Array(mutation.addedNodes).some(node => isInstance(node, HTMLHtmlElement))
80
- // ) {
81
- // const parentFrame = target.defaultView?.frameElement
82
- // if (!parentFrame) { continue }
83
- // this.bindTree(target.documentElement)
84
- // const frameID = this.app.nodes.getID(parentFrame)
85
- // const docID = this.app.nodes.getID(target.documentElement)
86
- // if (frameID === undefined || docID === undefined) { continue }
87
- // this.app.send(CreateIFrameDocument(frameID, docID));
88
- // continue;
89
- // }
90
- if (!isObservable(target) || !context.document.contains(target)) {
91
- continue;
92
- }
93
- if (type === 'childList') {
94
- for (let i = 0; i < mutation.removedNodes.length; i++) {
95
- this.bindTree(mutation.removedNodes[i]);
96
- }
97
- for (let i = 0; i < mutation.addedNodes.length; i++) {
98
- this.bindTree(mutation.addedNodes[i]);
99
- }
100
- continue;
101
- }
102
- const id = this.app.nodes.getID(target);
103
- if (id === undefined) {
104
- continue;
105
- }
106
- if (id >= this.recents.length) {
107
- this.recents[id] = undefined;
108
- }
109
- if (type === 'attributes') {
110
- const name = mutation.attributeName;
111
- if (name === null) {
112
- continue;
113
- }
114
- let attr = this.attributesList[id];
115
- if (attr === undefined) {
116
- this.attributesList[id] = attr = new Set();
117
- }
118
- attr.add(name);
119
- continue;
120
- }
121
- if (type === 'characterData') {
122
- this.textSet.add(id);
123
- continue;
124
- }
125
- }
126
- this.commitNodes();
127
- }));
128
- this.commited = [];
129
- this.recents = [];
130
- this.indexes = [0];
131
- this.attributesList = [];
132
- this.textSet = new Set();
133
- this.textMasked = new Set();
134
- }
135
- clear() {
136
- this.commited.length = 0;
137
- this.recents.length = 0;
138
- this.indexes.length = 1;
139
- this.attributesList.length = 0;
140
- this.textSet.clear();
141
- this.textMasked.clear();
142
- }
143
- sendNodeAttribute(id, node, name, value) {
144
- if (isSVGElement(node)) {
145
- if (name.substr(0, 6) === 'xlink:') {
146
- name = name.substr(6);
147
- }
148
- if (value === null) {
149
- this.app.send(new index_js_1.RemoveNodeAttribute(id, name));
150
- }
151
- else if (name === 'href') {
152
- if (value.length > 1e5) {
153
- value = '';
154
- }
155
- this.app.send(new index_js_1.SetNodeAttributeURLBased(id, name, value, this.app.getBaseHref()));
156
- }
157
- else {
158
- this.app.send(new index_js_1.SetNodeAttribute(id, name, value));
159
- }
160
- return;
161
- }
162
- if (name === 'src' ||
163
- name === 'srcset' ||
164
- name === 'integrity' ||
165
- name === 'crossorigin' ||
166
- name === 'autocomplete' ||
167
- name.substr(0, 2) === 'on') {
168
- return;
169
- }
170
- if (name === 'value' &&
171
- isInstance(node, HTMLInputElement) &&
172
- node.type !== 'button' &&
173
- node.type !== 'reset' &&
174
- node.type !== 'submit') {
175
- return;
176
- }
177
- if (value === null) {
178
- this.app.send(new index_js_1.RemoveNodeAttribute(id, name));
179
- return;
180
- }
181
- if (name === 'style' || name === 'href' && isInstance(node, HTMLLinkElement)) {
182
- this.app.send(new index_js_1.SetNodeAttributeURLBased(id, name, value, this.app.getBaseHref()));
183
- return;
184
- }
185
- if (name === 'href' || value.length > 1e5) {
186
- value = '';
187
- }
188
- this.app.send(new index_js_1.SetNodeAttribute(id, name, value));
189
- }
190
- /* TODO: abstract sanitation */
191
- getInnerTextSecure(el) {
192
- const id = this.app.nodes.getID(el);
193
- if (!id) {
194
- return '';
195
- }
196
- return this.checkObscure(id, el.innerText);
197
- }
198
- checkObscure(id, data) {
199
- if (this.textMasked.has(id)) {
200
- return data.replace(/[^\f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/g, '█');
201
- }
202
- if (this.options.obscureTextNumbers) {
203
- data = data.replace(/\d/g, '0');
204
- }
205
- if (this.options.obscureTextEmails) {
206
- data = data.replace(/([^\s]+)@([^\s]+)\.([^\s]+)/g, (...f) => (0, utils_js_1.stars)(f[1]) + '@' + (0, utils_js_1.stars)(f[2]) + '.' + (0, utils_js_1.stars)(f[3]));
207
- }
208
- return data;
209
- }
210
- sendNodeData(id, parentElement, data) {
211
- if (isInstance(parentElement, HTMLStyleElement) || isInstance(parentElement, SVGStyleElement)) {
212
- this.app.send(new index_js_1.SetCSSDataURLBased(id, data, this.app.getBaseHref()));
213
- return;
214
- }
215
- data = this.checkObscure(id, data);
216
- this.app.send(new index_js_1.SetNodeData(id, data));
217
- }
218
- /* end TODO: abstract sanitation */
219
- bindNode(node) {
220
- const r = this.app.nodes.registerNode(node);
221
- const id = r[0];
222
- this.recents[id] = r[1] || this.recents[id] || false;
223
- }
224
- bindTree(node) {
225
- if (!isObservable(node)) {
226
- return;
227
- }
228
- this.bindNode(node);
229
- const walker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT + NodeFilter.SHOW_TEXT, {
230
- acceptNode: (node) => isIgnored(node) || this.app.nodes.getID(node) !== undefined
231
- ? NodeFilter.FILTER_REJECT
232
- : NodeFilter.FILTER_ACCEPT,
233
- },
234
- // @ts-ignore
235
- false);
236
- while (walker.nextNode()) {
237
- this.bindNode(walker.currentNode);
238
- }
239
- }
240
- unbindNode(node) {
241
- const id = this.app.nodes.unregisterNode(node);
242
- if (id !== undefined && this.recents[id] === false) {
243
- this.app.send(new index_js_1.RemoveNode(id));
244
- }
245
- }
246
- _commitNode(id, node) {
247
- if (isRootNode(node)) {
248
- return true;
249
- }
250
- const parent = node.parentNode;
251
- let parentID;
252
- // Disable parent check for the upper context HTMLHtmlElement, because it is root there... (before)
253
- // TODO: get rid of "special" cases (there is an issue with CreateDocument altered behaviour though)
254
- // TODO: Clean the logic (though now it workd fine)
255
- if (!isInstance(node, HTMLHtmlElement) || !this.inUpperContext) {
256
- if (parent === null) {
257
- this.unbindNode(node);
258
- return false;
259
- }
260
- parentID = this.app.nodes.getID(parent);
261
- if (parentID === undefined) {
262
- this.unbindNode(node);
263
- return false;
264
- }
265
- if (!this.commitNode(parentID)) {
266
- this.unbindNode(node);
267
- return false;
268
- }
269
- if (this.textMasked.has(parentID) ||
270
- (isInstance(node, Element) && (0, utils_js_1.hasOpenreplayAttribute)(node, 'masked'))) {
271
- this.textMasked.add(id);
272
- }
273
- }
274
- let sibling = node.previousSibling;
275
- while (sibling !== null) {
276
- const siblingID = this.app.nodes.getID(sibling);
277
- if (siblingID !== undefined) {
278
- this.commitNode(siblingID);
279
- this.indexes[id] = this.indexes[siblingID] + 1;
280
- break;
281
- }
282
- sibling = sibling.previousSibling;
283
- }
284
- if (sibling === null) {
285
- this.indexes[id] = 0; //
286
- }
287
- const isNew = this.recents[id];
288
- const index = this.indexes[id];
289
- if (index === undefined) {
290
- throw 'commitNode: missing node index';
291
- }
292
- if (isNew === true) {
293
- if (isInstance(node, Element)) {
294
- if (parentID !== undefined) {
295
- this.app.send(new index_js_1.CreateElementNode(id, parentID, index, node.tagName, isSVGElement(node)));
296
- }
297
- for (let i = 0; i < node.attributes.length; i++) {
298
- const attr = node.attributes[i];
299
- this.sendNodeAttribute(id, node, attr.nodeName, attr.value);
300
- }
301
- }
302
- else if (isInstance(node, Text)) {
303
- // for text node id != 0, hence parentID !== undefined and parent is Element
304
- this.app.send(new index_js_1.CreateTextNode(id, parentID, index));
305
- this.sendNodeData(id, parent, node.data);
306
- }
307
- return true;
308
- }
309
- if (isNew === false && parentID !== undefined) {
310
- this.app.send(new index_js_1.MoveNode(id, parentID, index));
311
- }
312
- const attr = this.attributesList[id];
313
- if (attr !== undefined) {
314
- if (!isInstance(node, Element)) {
315
- throw 'commitNode: node is not an element';
316
- }
317
- for (const name of attr) {
318
- this.sendNodeAttribute(id, node, name, node.getAttribute(name));
319
- }
320
- }
321
- if (this.textSet.has(id)) {
322
- if (!isInstance(node, Text)) {
323
- throw 'commitNode: node is not a text';
324
- }
325
- // for text node id != 0, hence parent is Element
326
- this.sendNodeData(id, parent, node.data);
327
- }
328
- return true;
329
- }
330
- commitNode(id) {
331
- const node = this.app.nodes.getNode(id);
332
- if (node === undefined) {
333
- return false;
334
- }
335
- const cmt = this.commited[id];
336
- if (cmt !== undefined) {
337
- return cmt;
338
- }
339
- return (this.commited[id] = this._commitNode(id, node));
340
- }
341
- commitNodes() {
342
- let node;
343
- for (let id = 0; id < this.recents.length; id++) {
344
- // TODO: take a look at recents/ commitNode logic. why wasn't this check here before commitNode? only because of committed check inside?
345
- if (!this.recents[id]) {
346
- continue;
347
- }
348
- this.commitNode(id);
349
- if (node = this.app.nodes.getNode(id)) {
350
- this.app.nodes.callNodeCallbacks(node);
351
- }
352
- }
353
- this.clear();
354
- }
355
- // ISSSUE
356
- observeRoot(node, beforeCommit, nodeToBind = node) {
357
- this.observer.observe(node, {
358
- childList: true,
359
- attributes: true,
360
- characterData: true,
361
- subtree: true,
362
- attributeOldValue: false,
363
- characterDataOldValue: false,
364
- });
365
- this.bindTree(nodeToBind);
366
- beforeCommit(this.app.nodes.getID(node));
367
- this.commitNodes();
368
- }
369
- disconnect() {
370
- this.observer.disconnect();
371
- this.clear();
372
- }
373
- }
374
- exports.default = Observer;
@@ -1,4 +0,0 @@
1
- import Observer from "./observer.js";
2
- export default class ShadowRootObserver extends Observer {
3
- observe(el: Element): void;
4
- }
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const observer_js_1 = require("./observer.js");
4
- const index_js_1 = require("../../messages/index.js");
5
- class ShadowRootObserver extends observer_js_1.default {
6
- observe(el) {
7
- const shRoot = el.shadowRoot;
8
- const hostID = this.app.nodes.getID(el);
9
- if (!shRoot || hostID === undefined) {
10
- return;
11
- } // log
12
- this.observeRoot(shRoot, (rootID) => {
13
- if (rootID === undefined) {
14
- console.log("OpenReplay: Shadow Root was not bound");
15
- return;
16
- }
17
- this.app.send((0, index_js_1.CreateIFrameDocument)(hostID, rootID));
18
- });
19
- }
20
- }
21
- exports.default = ShadowRootObserver;
@@ -1,15 +0,0 @@
1
- import Observer from "./observer.js";
2
- import type { Options as BaseOptions } from "./observer.js";
3
- import App from "../index.js";
4
- export interface Options extends Partial<BaseOptions> {
5
- captureIFrames: boolean;
6
- }
7
- export default class TopObserver extends Observer<Options> {
8
- constructor(app: App, options: Partial<Options>);
9
- private iframeObservers;
10
- private handleIframe;
11
- private shadowRootObservers;
12
- private handleShadowRoot;
13
- observe(): void;
14
- disconnect(): void;
15
- }
@@ -1,82 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const observer_js_1 = require("./observer.js");
4
- const iframe_observer_js_1 = require("./iframe_observer.js");
5
- const shadow_root_observer_js_1 = require("./shadow_root_observer.js");
6
- const index_js_1 = require("../../messages/index.js");
7
- const attachShadowNativeFn = Element.prototype.attachShadow;
8
- class TopObserver extends observer_js_1.default {
9
- constructor(app, options) {
10
- super(app, Object.assign({
11
- captureIFrames: false
12
- }, options));
13
- this.iframeObservers = [];
14
- this.shadowRootObservers = [];
15
- // IFrames
16
- this.app.nodes.attachNodeCallback(node => {
17
- if ((0, observer_js_1.isInstance)(node, HTMLIFrameElement) &&
18
- (this.options.captureIFrames || node.getAttribute("data-openreplay-capture"))) {
19
- this.handleIframe(node);
20
- }
21
- });
22
- // ShadowDOM
23
- this.app.nodes.attachNodeCallback(node => {
24
- if ((0, observer_js_1.isInstance)(node, Element) && node.shadowRoot !== null) {
25
- this.handleShadowRoot(node.shadowRoot);
26
- }
27
- });
28
- }
29
- handleIframe(iframe) {
30
- let context = null;
31
- const handle = this.app.safe(() => {
32
- const id = this.app.nodes.getID(iframe);
33
- if (id === undefined) {
34
- return;
35
- } //log
36
- if (iframe.contentWindow === context) {
37
- return;
38
- } //Does this happen frequently?
39
- context = iframe.contentWindow;
40
- if (!context) {
41
- return;
42
- }
43
- const observer = new iframe_observer_js_1.default(this.app, this.options, context);
44
- this.iframeObservers.push(observer);
45
- observer.observe(iframe);
46
- });
47
- this.app.attachEventListener(iframe, "load", handle);
48
- handle();
49
- }
50
- handleShadowRoot(shRoot) {
51
- const observer = new shadow_root_observer_js_1.default(this.app, this.options, this.context);
52
- this.shadowRootObservers.push(observer);
53
- observer.observe(shRoot.host);
54
- }
55
- observe() {
56
- // Protection from several subsequent calls?
57
- const observer = this;
58
- Element.prototype.attachShadow = function () {
59
- const shadow = attachShadowNativeFn.apply(this, arguments);
60
- observer.handleShadowRoot(shadow);
61
- return shadow;
62
- };
63
- // Can observe documentElement (<html>) here, because it is not supposed to be changing.
64
- // However, it is possible in some exotic cases and may cause an ignorance of the newly created <html>
65
- // In this case context.document have to be observed, but this will cause
66
- // the change in the re-player behaviour caused by CreateDocument message:
67
- // the 0-node ("fRoot") will become #document rather than documentElement as it is now.
68
- // Alternatively - observe(#document) then bindNode(documentElement)
69
- this.observeRoot(this.context.document, () => {
70
- this.app.send(new index_js_1.CreateDocument());
71
- }, this.context.document.documentElement);
72
- }
73
- disconnect() {
74
- Element.prototype.attachShadow = attachShadowNativeFn;
75
- this.iframeObservers.forEach(o => o.disconnect());
76
- this.iframeObservers = [];
77
- this.shadowRootObservers.forEach(o => o.disconnect());
78
- this.shadowRootObservers = [];
79
- super.disconnect();
80
- }
81
- }
82
- exports.default = TopObserver;
@@ -1,4 +0,0 @@
1
- import Observer from "./observer.js";
2
- export default class IFrameObserver extends Observer {
3
- observe(iframe: HTMLIFrameElement): void;
4
- }
@@ -1,19 +0,0 @@
1
- import Observer from "./observer.js";
2
- import { CreateIFrameDocument } from "../../messages/index.js";
3
- export default class IFrameObserver extends Observer {
4
- observe(iframe) {
5
- const doc = iframe.contentDocument;
6
- const hostID = this.app.nodes.getID(iframe);
7
- if (!doc || hostID === undefined) {
8
- return;
9
- } //log TODO common app.logger
10
- // Have to observe document, because the inner <html> might be changed
11
- this.observeRoot(doc, (docID) => {
12
- if (docID === undefined) {
13
- console.log("OpenReplay: Iframe document not bound");
14
- return;
15
- }
16
- this.app.send(CreateIFrameDocument(hostID, docID));
17
- });
18
- }
19
- }
@@ -1,49 +0,0 @@
1
- import App from "../index.js";
2
- export interface Window extends globalThis.Window {
3
- HTMLInputElement: typeof HTMLInputElement;
4
- HTMLLinkElement: typeof HTMLLinkElement;
5
- HTMLStyleElement: typeof HTMLStyleElement;
6
- SVGStyleElement: typeof SVGStyleElement;
7
- HTMLIFrameElement: typeof HTMLIFrameElement;
8
- Text: typeof Text;
9
- Element: typeof Element;
10
- ShadowRoot: typeof ShadowRoot;
11
- }
12
- declare type WindowConstructor = Document | Element | Text | ShadowRoot | HTMLInputElement | HTMLLinkElement | HTMLStyleElement | HTMLIFrameElement;
13
- declare type Constructor<T> = {
14
- new (...args: any[]): T;
15
- name: string;
16
- };
17
- export declare function isInstance<T extends WindowConstructor>(node: Node, constr: Constructor<T>): node is T;
18
- export interface Options {
19
- obscureTextEmails: boolean;
20
- obscureTextNumbers: boolean;
21
- }
22
- export default abstract class Observer<AdditionalOptions = {}> {
23
- protected readonly app: App;
24
- protected readonly context: Window;
25
- private readonly observer;
26
- private readonly commited;
27
- private readonly recents;
28
- private readonly indexes;
29
- private readonly attributesList;
30
- private readonly textSet;
31
- private readonly textMasked;
32
- protected readonly options: Options & AdditionalOptions;
33
- private readonly inUpperContext;
34
- constructor(app: App, options: Partial<Options> & AdditionalOptions, context?: Window);
35
- private clear;
36
- private sendNodeAttribute;
37
- getInnerTextSecure(el: HTMLElement): string;
38
- private checkObscure;
39
- private sendNodeData;
40
- private bindNode;
41
- private bindTree;
42
- private unbindNode;
43
- private _commitNode;
44
- private commitNode;
45
- private commitNodes;
46
- protected observeRoot(node: Node, beforeCommit: (id?: number) => unknown, nodeToBind?: Node): void;
47
- disconnect(): void;
48
- }
49
- export {};
@@ -1,369 +0,0 @@
1
- import { stars, hasOpenreplayAttribute } from "../../utils.js";
2
- import { RemoveNodeAttribute, SetNodeAttribute, SetNodeAttributeURLBased, SetCSSDataURLBased, SetNodeData, CreateTextNode, CreateElementNode, MoveNode, RemoveNode, } from "../../messages/index.js";
3
- function isSVGElement(node) {
4
- return node.namespaceURI === 'http://www.w3.org/2000/svg';
5
- }
6
- // TODO: we need a type expert here so we won't have to ignore the lines
7
- // TODO: use it everywhere (static function; export from which file? <-- global Window typing required)
8
- export function isInstance(node, constr) {
9
- const doc = node.ownerDocument;
10
- if (!doc) { // null if Document
11
- return constr.name === 'Document';
12
- }
13
- let context =
14
- // @ts-ignore (for EI, Safary)
15
- doc.parentWindow ||
16
- doc.defaultView; // TODO: smart global typing for Window object
17
- while (context.parent && context.parent !== context) {
18
- // @ts-ignore
19
- if (node instanceof context[constr.name]) {
20
- return true;
21
- }
22
- // @ts-ignore
23
- context = context.parent;
24
- }
25
- // @ts-ignore
26
- return node instanceof context[constr.name];
27
- }
28
- function isIgnored(node) {
29
- if (isInstance(node, Text)) {
30
- return false;
31
- }
32
- if (!isInstance(node, Element)) {
33
- return true;
34
- }
35
- const tag = node.tagName.toUpperCase();
36
- if (tag === 'LINK') {
37
- const rel = node.getAttribute('rel');
38
- const as = node.getAttribute('as');
39
- return !((rel === null || rel === void 0 ? void 0 : rel.includes('stylesheet')) || as === "style" || as === "font");
40
- }
41
- return (tag === 'SCRIPT' ||
42
- tag === 'NOSCRIPT' ||
43
- tag === 'META' ||
44
- tag === 'TITLE' ||
45
- tag === 'BASE');
46
- }
47
- function isRootNode(node) {
48
- return isInstance(node, Document) || isInstance(node, ShadowRoot);
49
- }
50
- function isObservable(node) {
51
- if (isRootNode(node)) {
52
- return true;
53
- }
54
- return !isIgnored(node);
55
- }
56
- export default class Observer {
57
- constructor(app, options, context = window) {
58
- this.app = app;
59
- this.context = context;
60
- this.options = Object.assign({
61
- obscureTextEmails: true,
62
- obscureTextNumbers: false,
63
- }, options);
64
- this.inUpperContext = context.parent === context;
65
- this.observer = new MutationObserver(this.app.safe((mutations) => {
66
- for (const mutation of mutations) {
67
- const target = mutation.target;
68
- const type = mutation.type;
69
- // TODO TODO TODO: move to iframe_observer/remove??? (check if )
70
- // Special case
71
- // 'childList' on Document might happen in case of iframe.
72
- // TODO: generalize as much as possible
73
- // if (isInstance(target, Document) // Also ShadowRoot can be here
74
- // && type === 'childList'
75
- // //&& new Array(mutation.addedNodes).some(node => isInstance(node, HTMLHtmlElement))
76
- // ) {
77
- // const parentFrame = target.defaultView?.frameElement
78
- // if (!parentFrame) { continue }
79
- // this.bindTree(target.documentElement)
80
- // const frameID = this.app.nodes.getID(parentFrame)
81
- // const docID = this.app.nodes.getID(target.documentElement)
82
- // if (frameID === undefined || docID === undefined) { continue }
83
- // this.app.send(CreateIFrameDocument(frameID, docID));
84
- // continue;
85
- // }
86
- if (!isObservable(target) || !context.document.contains(target)) {
87
- continue;
88
- }
89
- if (type === 'childList') {
90
- for (let i = 0; i < mutation.removedNodes.length; i++) {
91
- this.bindTree(mutation.removedNodes[i]);
92
- }
93
- for (let i = 0; i < mutation.addedNodes.length; i++) {
94
- this.bindTree(mutation.addedNodes[i]);
95
- }
96
- continue;
97
- }
98
- const id = this.app.nodes.getID(target);
99
- if (id === undefined) {
100
- continue;
101
- }
102
- if (id >= this.recents.length) {
103
- this.recents[id] = undefined;
104
- }
105
- if (type === 'attributes') {
106
- const name = mutation.attributeName;
107
- if (name === null) {
108
- continue;
109
- }
110
- let attr = this.attributesList[id];
111
- if (attr === undefined) {
112
- this.attributesList[id] = attr = new Set();
113
- }
114
- attr.add(name);
115
- continue;
116
- }
117
- if (type === 'characterData') {
118
- this.textSet.add(id);
119
- continue;
120
- }
121
- }
122
- this.commitNodes();
123
- }));
124
- this.commited = [];
125
- this.recents = [];
126
- this.indexes = [0];
127
- this.attributesList = [];
128
- this.textSet = new Set();
129
- this.textMasked = new Set();
130
- }
131
- clear() {
132
- this.commited.length = 0;
133
- this.recents.length = 0;
134
- this.indexes.length = 1;
135
- this.attributesList.length = 0;
136
- this.textSet.clear();
137
- this.textMasked.clear();
138
- }
139
- sendNodeAttribute(id, node, name, value) {
140
- if (isSVGElement(node)) {
141
- if (name.substr(0, 6) === 'xlink:') {
142
- name = name.substr(6);
143
- }
144
- if (value === null) {
145
- this.app.send(new RemoveNodeAttribute(id, name));
146
- }
147
- else if (name === 'href') {
148
- if (value.length > 1e5) {
149
- value = '';
150
- }
151
- this.app.send(new SetNodeAttributeURLBased(id, name, value, this.app.getBaseHref()));
152
- }
153
- else {
154
- this.app.send(new SetNodeAttribute(id, name, value));
155
- }
156
- return;
157
- }
158
- if (name === 'src' ||
159
- name === 'srcset' ||
160
- name === 'integrity' ||
161
- name === 'crossorigin' ||
162
- name === 'autocomplete' ||
163
- name.substr(0, 2) === 'on') {
164
- return;
165
- }
166
- if (name === 'value' &&
167
- isInstance(node, HTMLInputElement) &&
168
- node.type !== 'button' &&
169
- node.type !== 'reset' &&
170
- node.type !== 'submit') {
171
- return;
172
- }
173
- if (value === null) {
174
- this.app.send(new RemoveNodeAttribute(id, name));
175
- return;
176
- }
177
- if (name === 'style' || name === 'href' && isInstance(node, HTMLLinkElement)) {
178
- this.app.send(new SetNodeAttributeURLBased(id, name, value, this.app.getBaseHref()));
179
- return;
180
- }
181
- if (name === 'href' || value.length > 1e5) {
182
- value = '';
183
- }
184
- this.app.send(new SetNodeAttribute(id, name, value));
185
- }
186
- /* TODO: abstract sanitation */
187
- getInnerTextSecure(el) {
188
- const id = this.app.nodes.getID(el);
189
- if (!id) {
190
- return '';
191
- }
192
- return this.checkObscure(id, el.innerText);
193
- }
194
- checkObscure(id, data) {
195
- if (this.textMasked.has(id)) {
196
- return data.replace(/[^\f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]/g, '█');
197
- }
198
- if (this.options.obscureTextNumbers) {
199
- data = data.replace(/\d/g, '0');
200
- }
201
- if (this.options.obscureTextEmails) {
202
- data = data.replace(/([^\s]+)@([^\s]+)\.([^\s]+)/g, (...f) => stars(f[1]) + '@' + stars(f[2]) + '.' + stars(f[3]));
203
- }
204
- return data;
205
- }
206
- sendNodeData(id, parentElement, data) {
207
- if (isInstance(parentElement, HTMLStyleElement) || isInstance(parentElement, SVGStyleElement)) {
208
- this.app.send(new SetCSSDataURLBased(id, data, this.app.getBaseHref()));
209
- return;
210
- }
211
- data = this.checkObscure(id, data);
212
- this.app.send(new SetNodeData(id, data));
213
- }
214
- /* end TODO: abstract sanitation */
215
- bindNode(node) {
216
- const r = this.app.nodes.registerNode(node);
217
- const id = r[0];
218
- this.recents[id] = r[1] || this.recents[id] || false;
219
- }
220
- bindTree(node) {
221
- if (!isObservable(node)) {
222
- return;
223
- }
224
- this.bindNode(node);
225
- const walker = document.createTreeWalker(node, NodeFilter.SHOW_ELEMENT + NodeFilter.SHOW_TEXT, {
226
- acceptNode: (node) => isIgnored(node) || this.app.nodes.getID(node) !== undefined
227
- ? NodeFilter.FILTER_REJECT
228
- : NodeFilter.FILTER_ACCEPT,
229
- },
230
- // @ts-ignore
231
- false);
232
- while (walker.nextNode()) {
233
- this.bindNode(walker.currentNode);
234
- }
235
- }
236
- unbindNode(node) {
237
- const id = this.app.nodes.unregisterNode(node);
238
- if (id !== undefined && this.recents[id] === false) {
239
- this.app.send(new RemoveNode(id));
240
- }
241
- }
242
- _commitNode(id, node) {
243
- if (isRootNode(node)) {
244
- return true;
245
- }
246
- const parent = node.parentNode;
247
- let parentID;
248
- // Disable parent check for the upper context HTMLHtmlElement, because it is root there... (before)
249
- // TODO: get rid of "special" cases (there is an issue with CreateDocument altered behaviour though)
250
- // TODO: Clean the logic (though now it workd fine)
251
- if (!isInstance(node, HTMLHtmlElement) || !this.inUpperContext) {
252
- if (parent === null) {
253
- this.unbindNode(node);
254
- return false;
255
- }
256
- parentID = this.app.nodes.getID(parent);
257
- if (parentID === undefined) {
258
- this.unbindNode(node);
259
- return false;
260
- }
261
- if (!this.commitNode(parentID)) {
262
- this.unbindNode(node);
263
- return false;
264
- }
265
- if (this.textMasked.has(parentID) ||
266
- (isInstance(node, Element) && hasOpenreplayAttribute(node, 'masked'))) {
267
- this.textMasked.add(id);
268
- }
269
- }
270
- let sibling = node.previousSibling;
271
- while (sibling !== null) {
272
- const siblingID = this.app.nodes.getID(sibling);
273
- if (siblingID !== undefined) {
274
- this.commitNode(siblingID);
275
- this.indexes[id] = this.indexes[siblingID] + 1;
276
- break;
277
- }
278
- sibling = sibling.previousSibling;
279
- }
280
- if (sibling === null) {
281
- this.indexes[id] = 0; //
282
- }
283
- const isNew = this.recents[id];
284
- const index = this.indexes[id];
285
- if (index === undefined) {
286
- throw 'commitNode: missing node index';
287
- }
288
- if (isNew === true) {
289
- if (isInstance(node, Element)) {
290
- if (parentID !== undefined) {
291
- this.app.send(new CreateElementNode(id, parentID, index, node.tagName, isSVGElement(node)));
292
- }
293
- for (let i = 0; i < node.attributes.length; i++) {
294
- const attr = node.attributes[i];
295
- this.sendNodeAttribute(id, node, attr.nodeName, attr.value);
296
- }
297
- }
298
- else if (isInstance(node, Text)) {
299
- // for text node id != 0, hence parentID !== undefined and parent is Element
300
- this.app.send(new CreateTextNode(id, parentID, index));
301
- this.sendNodeData(id, parent, node.data);
302
- }
303
- return true;
304
- }
305
- if (isNew === false && parentID !== undefined) {
306
- this.app.send(new MoveNode(id, parentID, index));
307
- }
308
- const attr = this.attributesList[id];
309
- if (attr !== undefined) {
310
- if (!isInstance(node, Element)) {
311
- throw 'commitNode: node is not an element';
312
- }
313
- for (const name of attr) {
314
- this.sendNodeAttribute(id, node, name, node.getAttribute(name));
315
- }
316
- }
317
- if (this.textSet.has(id)) {
318
- if (!isInstance(node, Text)) {
319
- throw 'commitNode: node is not a text';
320
- }
321
- // for text node id != 0, hence parent is Element
322
- this.sendNodeData(id, parent, node.data);
323
- }
324
- return true;
325
- }
326
- commitNode(id) {
327
- const node = this.app.nodes.getNode(id);
328
- if (node === undefined) {
329
- return false;
330
- }
331
- const cmt = this.commited[id];
332
- if (cmt !== undefined) {
333
- return cmt;
334
- }
335
- return (this.commited[id] = this._commitNode(id, node));
336
- }
337
- commitNodes() {
338
- let node;
339
- for (let id = 0; id < this.recents.length; id++) {
340
- // TODO: take a look at recents/ commitNode logic. why wasn't this check here before commitNode? only because of committed check inside?
341
- if (!this.recents[id]) {
342
- continue;
343
- }
344
- this.commitNode(id);
345
- if (node = this.app.nodes.getNode(id)) {
346
- this.app.nodes.callNodeCallbacks(node);
347
- }
348
- }
349
- this.clear();
350
- }
351
- // ISSSUE
352
- observeRoot(node, beforeCommit, nodeToBind = node) {
353
- this.observer.observe(node, {
354
- childList: true,
355
- attributes: true,
356
- characterData: true,
357
- subtree: true,
358
- attributeOldValue: false,
359
- characterDataOldValue: false,
360
- });
361
- this.bindTree(nodeToBind);
362
- beforeCommit(this.app.nodes.getID(node));
363
- this.commitNodes();
364
- }
365
- disconnect() {
366
- this.observer.disconnect();
367
- this.clear();
368
- }
369
- }
@@ -1,4 +0,0 @@
1
- import Observer from "./observer.js";
2
- export default class ShadowRootObserver extends Observer {
3
- observe(el: Element): void;
4
- }
@@ -1,18 +0,0 @@
1
- import Observer from "./observer.js";
2
- import { CreateIFrameDocument } from "../../messages/index.js";
3
- export default class ShadowRootObserver extends Observer {
4
- observe(el) {
5
- const shRoot = el.shadowRoot;
6
- const hostID = this.app.nodes.getID(el);
7
- if (!shRoot || hostID === undefined) {
8
- return;
9
- } // log
10
- this.observeRoot(shRoot, (rootID) => {
11
- if (rootID === undefined) {
12
- console.log("OpenReplay: Shadow Root was not bound");
13
- return;
14
- }
15
- this.app.send(CreateIFrameDocument(hostID, rootID));
16
- });
17
- }
18
- }
@@ -1,15 +0,0 @@
1
- import Observer from "./observer.js";
2
- import type { Options as BaseOptions } from "./observer.js";
3
- import App from "../index.js";
4
- export interface Options extends Partial<BaseOptions> {
5
- captureIFrames: boolean;
6
- }
7
- export default class TopObserver extends Observer<Options> {
8
- constructor(app: App, options: Partial<Options>);
9
- private iframeObservers;
10
- private handleIframe;
11
- private shadowRootObservers;
12
- private handleShadowRoot;
13
- observe(): void;
14
- disconnect(): void;
15
- }
@@ -1,79 +0,0 @@
1
- import Observer, { isInstance } from "./observer.js";
2
- import IFrameObserver from "./iframe_observer.js";
3
- import ShadowRootObserver from "./shadow_root_observer.js";
4
- import { CreateDocument } from "../../messages/index.js";
5
- const attachShadowNativeFn = Element.prototype.attachShadow;
6
- export default class TopObserver extends Observer {
7
- constructor(app, options) {
8
- super(app, Object.assign({
9
- captureIFrames: false
10
- }, options));
11
- this.iframeObservers = [];
12
- this.shadowRootObservers = [];
13
- // IFrames
14
- this.app.nodes.attachNodeCallback(node => {
15
- if (isInstance(node, HTMLIFrameElement) &&
16
- (this.options.captureIFrames || node.getAttribute("data-openreplay-capture"))) {
17
- this.handleIframe(node);
18
- }
19
- });
20
- // ShadowDOM
21
- this.app.nodes.attachNodeCallback(node => {
22
- if (isInstance(node, Element) && node.shadowRoot !== null) {
23
- this.handleShadowRoot(node.shadowRoot);
24
- }
25
- });
26
- }
27
- handleIframe(iframe) {
28
- let context = null;
29
- const handle = this.app.safe(() => {
30
- const id = this.app.nodes.getID(iframe);
31
- if (id === undefined) {
32
- return;
33
- } //log
34
- if (iframe.contentWindow === context) {
35
- return;
36
- } //Does this happen frequently?
37
- context = iframe.contentWindow;
38
- if (!context) {
39
- return;
40
- }
41
- const observer = new IFrameObserver(this.app, this.options, context);
42
- this.iframeObservers.push(observer);
43
- observer.observe(iframe);
44
- });
45
- this.app.attachEventListener(iframe, "load", handle);
46
- handle();
47
- }
48
- handleShadowRoot(shRoot) {
49
- const observer = new ShadowRootObserver(this.app, this.options, this.context);
50
- this.shadowRootObservers.push(observer);
51
- observer.observe(shRoot.host);
52
- }
53
- observe() {
54
- // Protection from several subsequent calls?
55
- const observer = this;
56
- Element.prototype.attachShadow = function () {
57
- const shadow = attachShadowNativeFn.apply(this, arguments);
58
- observer.handleShadowRoot(shadow);
59
- return shadow;
60
- };
61
- // Can observe documentElement (<html>) here, because it is not supposed to be changing.
62
- // However, it is possible in some exotic cases and may cause an ignorance of the newly created <html>
63
- // In this case context.document have to be observed, but this will cause
64
- // the change in the re-player behaviour caused by CreateDocument message:
65
- // the 0-node ("fRoot") will become #document rather than documentElement as it is now.
66
- // Alternatively - observe(#document) then bindNode(documentElement)
67
- this.observeRoot(this.context.document, () => {
68
- this.app.send(new CreateDocument());
69
- }, this.context.document.documentElement);
70
- }
71
- disconnect() {
72
- Element.prototype.attachShadow = attachShadowNativeFn;
73
- this.iframeObservers.forEach(o => o.disconnect());
74
- this.iframeObservers = [];
75
- this.shadowRootObservers.forEach(o => o.disconnect());
76
- this.shadowRootObservers = [];
77
- super.disconnect();
78
- }
79
- }