clarity-js 0.7.5 → 0.7.6
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.
- package/build/clarity.insight.js +1 -1
- package/build/clarity.js +81 -52
- package/build/clarity.min.js +1 -1
- package/build/clarity.module.js +81 -52
- package/package.json +1 -1
- package/rollup.config.ts +5 -5
- package/src/core/scrub.ts +39 -9
- package/src/core/version.ts +1 -1
- package/src/data/upload.ts +2 -1
- package/src/{core → insight}/blank.ts +3 -1
- package/src/insight/encode.ts +61 -0
- package/src/insight/snapshot.ts +112 -0
- package/src/interaction/click.ts +15 -2
- package/src/interaction/encode.ts +1 -1
- package/src/interaction/pointer.ts +1 -1
- package/src/layout/document.ts +2 -2
- package/src/layout/encode.ts +1 -1
- package/src/layout/index.ts +1 -1
- package/src/layout/offset.ts +1 -1
- package/src/layout/target.ts +0 -13
- package/src/layout/traverse.ts +4 -4
- package/types/core.d.ts +2 -1
- package/types/data.d.ts +2 -1
- package/types/layout.d.ts +1 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { OffsetDistance, Privacy } from "@clarity-types/core";
|
|
2
|
+
import { Event } from "@clarity-types/data";
|
|
3
|
+
import { Constant, NodeInfo, NodeValue, TargetMetadata } from "@clarity-types/layout";
|
|
4
|
+
import * as doc from "@src/layout/document";
|
|
5
|
+
import encode from "@src/insight/encode";
|
|
6
|
+
import * as interaction from "@src/interaction";
|
|
7
|
+
export let values: NodeValue[] = [];
|
|
8
|
+
let index: number = 1;
|
|
9
|
+
let idMap: WeakMap<Node, number> = null; // Maps node => id.
|
|
10
|
+
|
|
11
|
+
export function start(): void {
|
|
12
|
+
reset();
|
|
13
|
+
doc.start();
|
|
14
|
+
getId(document.documentElement); // Pre-discover ID for page root
|
|
15
|
+
interaction.observe(document);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function stop(): void {
|
|
19
|
+
reset();
|
|
20
|
+
doc.stop();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function compute(): void { /* Intentionally Blank */ }
|
|
24
|
+
export function iframe(): boolean { return false; }
|
|
25
|
+
export function offset(): OffsetDistance { return { x: 0, y: 0 }; }
|
|
26
|
+
export function hashText(): void { /* Intentionally Blank */ }
|
|
27
|
+
|
|
28
|
+
export function target(evt: UIEvent): Node {
|
|
29
|
+
let path = evt.composed && evt.composedPath ? evt.composedPath() : null;
|
|
30
|
+
let node = (path && path.length > 0 ? path[0] : evt.target) as Node;
|
|
31
|
+
return node.nodeType === Node.DOCUMENT_NODE ? (node as Document).documentElement : node;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function metadata(node: Node): TargetMetadata {
|
|
35
|
+
let output: TargetMetadata = { id: 0, hash: null, privacy: Privacy.Snapshot, node };
|
|
36
|
+
if (node) { output.id = idMap.has(node) ? idMap.get(node) : getId(node); }
|
|
37
|
+
return output;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function snapshot(): void {
|
|
41
|
+
values = [];
|
|
42
|
+
traverse(document);
|
|
43
|
+
encode(Event.Snapshot);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function reset(): void {
|
|
47
|
+
idMap = new WeakMap();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function traverse(root: Node): void {
|
|
51
|
+
let queue = [root];
|
|
52
|
+
while (queue.length > 0) {
|
|
53
|
+
let attributes = null, tag = null, value = null;
|
|
54
|
+
let node = queue.shift();
|
|
55
|
+
let next = node.firstChild;
|
|
56
|
+
let parent = node.parentElement ? node.parentElement : (node.parentNode ? node.parentNode : null);
|
|
57
|
+
|
|
58
|
+
while (next) {
|
|
59
|
+
queue.push(next);
|
|
60
|
+
next = next.nextSibling;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Process the node
|
|
64
|
+
let type = node.nodeType;
|
|
65
|
+
switch (type) {
|
|
66
|
+
case Node.DOCUMENT_TYPE_NODE:
|
|
67
|
+
let doctype = node as DocumentType;
|
|
68
|
+
tag = Constant.DocumentTag;
|
|
69
|
+
attributes = { name: doctype.name, publicId: doctype.publicId, systemId: doctype.systemId }
|
|
70
|
+
break;
|
|
71
|
+
case Node.TEXT_NODE:
|
|
72
|
+
value = node.nodeValue;
|
|
73
|
+
tag = idMap.get(parent) ? Constant.TextTag : tag;
|
|
74
|
+
break;
|
|
75
|
+
case Node.ELEMENT_NODE:
|
|
76
|
+
let element = node as HTMLElement;
|
|
77
|
+
attributes = getAttributes(element);
|
|
78
|
+
tag = ["NOSCRIPT", "SCRIPT", "STYLE"].indexOf(element.tagName) < 0 ? element.tagName : tag;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
add(node, parent, { tag, attributes, value });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* Helper Functions - Snapshot Traversal */
|
|
86
|
+
function getAttributes(element: HTMLElement): { [key: string]: string } {
|
|
87
|
+
let output = {};
|
|
88
|
+
let attributes = element.attributes;
|
|
89
|
+
if (attributes && attributes.length > 0) {
|
|
90
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
91
|
+
output[attributes[i].name] = attributes[i].value;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return output;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function getId(node: Node): number {
|
|
98
|
+
if (node === null) { return null; }
|
|
99
|
+
if (idMap.has(node)) { return idMap.get(node); }
|
|
100
|
+
idMap.set(node, index);
|
|
101
|
+
return index++;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function add(node: Node, parent: Node, data: NodeInfo): void {
|
|
105
|
+
if (node && data && data.tag) {
|
|
106
|
+
let id = getId(node);
|
|
107
|
+
let parentId = parent ? idMap.get(parent) : null;
|
|
108
|
+
let previous = node.previousSibling ? idMap.get(node.previousSibling) : null;
|
|
109
|
+
let metadata = { active: true, suspend: false, privacy: Privacy.Snapshot, position: null, fraud: null, size: null };
|
|
110
|
+
values.push({ id, parent: parentId, previous, children: [], data, selector: null, hash: null, region: null, metadata });
|
|
111
|
+
}
|
|
112
|
+
}
|
package/src/interaction/click.ts
CHANGED
|
@@ -5,8 +5,8 @@ import { bind } from "@src/core/event";
|
|
|
5
5
|
import { schedule } from "@src/core/task";
|
|
6
6
|
import { time } from "@src/core/time";
|
|
7
7
|
import { iframe } from "@src/layout/dom";
|
|
8
|
-
import offset from "@src/layout/offset";
|
|
9
|
-
import {
|
|
8
|
+
import { offset } from "@src/layout/offset";
|
|
9
|
+
import { target } from "@src/layout/target";
|
|
10
10
|
import encode from "./encode";
|
|
11
11
|
|
|
12
12
|
const UserInputTags = ["input", "textarea", "radio", "button", "canvas"];
|
|
@@ -73,6 +73,19 @@ function handler(event: Event, root: Node, evt: MouseEvent): void {
|
|
|
73
73
|
}
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
function link(node: Node): HTMLAnchorElement {
|
|
77
|
+
while (node && node !== document) {
|
|
78
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
79
|
+
let element = node as HTMLElement;
|
|
80
|
+
if (element.tagName === "A") {
|
|
81
|
+
return element as HTMLAnchorElement;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
node = node.parentNode;
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
76
89
|
function text(element: Node): string {
|
|
77
90
|
let output = null;
|
|
78
91
|
if (element) {
|
|
@@ -47,7 +47,7 @@ export default async function (type: Event, ts: number = null): Promise<void> {
|
|
|
47
47
|
for (let entry of click.state) {
|
|
48
48
|
let cTarget = metadata(entry.data.target as Node, entry.event, entry.data.text);
|
|
49
49
|
tokens = [entry.time, entry.event];
|
|
50
|
-
let cHash = cTarget.hash.join(Constant.Dot);
|
|
50
|
+
let cHash = cTarget.hash ? cTarget.hash.join(Constant.Dot) : Constant.Empty;
|
|
51
51
|
tokens.push(cTarget.id);
|
|
52
52
|
tokens.push(entry.data.x);
|
|
53
53
|
tokens.push(entry.data.y);
|
|
@@ -5,7 +5,7 @@ import { schedule } from "@src/core/task";
|
|
|
5
5
|
import { time } from "@src/core/time";
|
|
6
6
|
import { clearTimeout, setTimeout } from "@src/core/timeout";
|
|
7
7
|
import { iframe } from "@src/layout/dom";
|
|
8
|
-
import offset from "@src/layout/offset";
|
|
8
|
+
import { offset } from "@src/layout/offset";
|
|
9
9
|
import { target } from "@src/layout/target";
|
|
10
10
|
import encode from "./encode";
|
|
11
11
|
|
package/src/layout/document.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Event } from "@clarity-types/data";
|
|
2
2
|
import { DocumentData } from "@clarity-types/layout";
|
|
3
|
-
import encode from "
|
|
3
|
+
import encode from "@src/layout/encode";
|
|
4
4
|
|
|
5
5
|
export let data: DocumentData;
|
|
6
6
|
|
|
@@ -41,6 +41,6 @@ export function compute(): void {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export function
|
|
44
|
+
export function stop(): void {
|
|
45
45
|
reset();
|
|
46
46
|
}
|
package/src/layout/encode.ts
CHANGED
|
@@ -106,5 +106,5 @@ function str(input: number): string {
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
function attribute(key: string, value: string, privacy: Privacy): string {
|
|
109
|
-
return `${key}=${scrub.text(value, key, privacy)}`;
|
|
109
|
+
return `${key}=${scrub.text(value, key.indexOf(Constant.DataAttribute) === 0 ? Constant.DataAttribute : key, privacy)}`;
|
|
110
110
|
}
|
package/src/layout/index.ts
CHANGED
package/src/layout/offset.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { OffsetDistance } from "@clarity-types/core";
|
|
2
2
|
import { iframe } from "@src/layout/dom";
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export function offset(element: HTMLElement): OffsetDistance {
|
|
5
5
|
let output: OffsetDistance = { x: 0, y: 0 };
|
|
6
6
|
|
|
7
7
|
// Walk up the chain to ensure we compute offset distance correctly
|
package/src/layout/target.ts
CHANGED
|
@@ -13,19 +13,6 @@ export function target(evt: UIEvent): Node {
|
|
|
13
13
|
return node.nodeType === Node.DOCUMENT_NODE ? (node as Document).documentElement : node;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export function link(node: Node): HTMLAnchorElement {
|
|
17
|
-
while (node && node !== document) {
|
|
18
|
-
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
19
|
-
let element = node as HTMLElement;
|
|
20
|
-
if (element.tagName === "A") {
|
|
21
|
-
return element as HTMLAnchorElement;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
node = node.parentNode;
|
|
25
|
-
}
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
16
|
export function metadata(node: Node, event: Event, text: string = null): TargetMetadata {
|
|
30
17
|
// If the node is null, we return a reserved value for id: 0. Valid assignment of id begins from 1+.
|
|
31
18
|
let output: TargetMetadata = { id: 0, hash: null, privacy: Privacy.Text, node };
|
package/src/layout/traverse.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Task, Timer } from "@clarity-types/core";
|
|
2
2
|
import { Source } from "@clarity-types/layout";
|
|
3
3
|
import * as task from "@src/core/task";
|
|
4
|
-
import
|
|
4
|
+
import node from "@src/layout/node";
|
|
5
5
|
|
|
6
6
|
export default async function(root: Node, timer: Timer, source: Source): Promise<void> {
|
|
7
7
|
let queue = [root];
|
|
8
8
|
while (queue.length > 0) {
|
|
9
|
-
let
|
|
10
|
-
let next =
|
|
9
|
+
let entry = queue.shift();
|
|
10
|
+
let next = entry.firstChild;
|
|
11
11
|
|
|
12
12
|
while (next) {
|
|
13
13
|
queue.push(next);
|
|
@@ -22,7 +22,7 @@ export default async function(root: Node, timer: Timer, source: Source): Promise
|
|
|
22
22
|
// Check if processing a node gives us a pointer to one of its sub nodes for traversal
|
|
23
23
|
// E.g. an element node may give us a pointer to traverse shadowDom if shadowRoot property is set
|
|
24
24
|
// Or, an iframe from the same origin could give a pointer to it's document for traversing contents of iframe.
|
|
25
|
-
let subnode =
|
|
25
|
+
let subnode = node(entry, source);
|
|
26
26
|
if (subnode) { queue.push(subnode); }
|
|
27
27
|
}
|
|
28
28
|
}
|
package/types/core.d.ts
CHANGED
package/types/data.d.ts
CHANGED