@gemx-dev/clarity-js 0.8.39
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/README.md +1 -0
- package/build/clarity.extended.js +1 -0
- package/build/clarity.insight.js +1 -0
- package/build/clarity.js +6433 -0
- package/build/clarity.livechat.js +1 -0
- package/build/clarity.min.js +1 -0
- package/build/clarity.module.js +6429 -0
- package/build/clarity.performance.js +1 -0
- package/build/dynamic/clarity.crisp.js +1 -0
- package/build/dynamic/clarity.tidio.js +1 -0
- package/package.json +55 -0
- package/rollup.config.ts +161 -0
- package/src/clarity.ts +71 -0
- package/src/core/api.ts +8 -0
- package/src/core/config.ts +29 -0
- package/src/core/copy.ts +3 -0
- package/src/core/dynamic.ts +57 -0
- package/src/core/event.ts +53 -0
- package/src/core/hash.ts +19 -0
- package/src/core/history.ts +71 -0
- package/src/core/index.ts +81 -0
- package/src/core/measure.ts +19 -0
- package/src/core/report.ts +35 -0
- package/src/core/scrub.ts +202 -0
- package/src/core/task.ts +181 -0
- package/src/core/throttle.ts +46 -0
- package/src/core/time.ts +26 -0
- package/src/core/timeout.ts +10 -0
- package/src/core/version.ts +2 -0
- package/src/data/baseline.ts +162 -0
- package/src/data/compress.ts +31 -0
- package/src/data/consent.ts +75 -0
- package/src/data/custom.ts +23 -0
- package/src/data/dimension.ts +53 -0
- package/src/data/encode.ts +155 -0
- package/src/data/envelope.ts +53 -0
- package/src/data/extract.ts +211 -0
- package/src/data/index.ts +50 -0
- package/src/data/limit.ts +44 -0
- package/src/data/metadata.ts +408 -0
- package/src/data/metric.ts +51 -0
- package/src/data/ping.ts +36 -0
- package/src/data/signal.ts +30 -0
- package/src/data/summary.ts +34 -0
- package/src/data/token.ts +39 -0
- package/src/data/upgrade.ts +44 -0
- package/src/data/upload.ts +333 -0
- package/src/data/variable.ts +84 -0
- package/src/diagnostic/encode.ts +40 -0
- package/src/diagnostic/fraud.ts +37 -0
- package/src/diagnostic/index.ts +13 -0
- package/src/diagnostic/internal.ts +28 -0
- package/src/diagnostic/script.ts +35 -0
- package/src/dynamic/agent/blank.ts +2 -0
- package/src/dynamic/agent/crisp.ts +40 -0
- package/src/dynamic/agent/encode.ts +25 -0
- package/src/dynamic/agent/index.ts +8 -0
- package/src/dynamic/agent/livechat.ts +58 -0
- package/src/dynamic/agent/tidio.ts +44 -0
- package/src/global.ts +6 -0
- package/src/index.ts +9 -0
- package/src/insight/blank.ts +14 -0
- package/src/insight/encode.ts +61 -0
- package/src/insight/snapshot.ts +115 -0
- package/src/interaction/change.ts +38 -0
- package/src/interaction/click.ts +163 -0
- package/src/interaction/clipboard.ts +32 -0
- package/src/interaction/encode.ts +207 -0
- package/src/interaction/focus.ts +25 -0
- package/src/interaction/index.ts +60 -0
- package/src/interaction/input.ts +58 -0
- package/src/interaction/pointer.ts +137 -0
- package/src/interaction/resize.ts +50 -0
- package/src/interaction/scroll.ts +129 -0
- package/src/interaction/selection.ts +66 -0
- package/src/interaction/submit.ts +30 -0
- package/src/interaction/timeline.ts +69 -0
- package/src/interaction/unload.ts +26 -0
- package/src/interaction/visibility.ts +28 -0
- package/src/layout/animation.ts +133 -0
- package/src/layout/custom.ts +43 -0
- package/src/layout/discover.ts +31 -0
- package/src/layout/document.ts +46 -0
- package/src/layout/dom.ts +439 -0
- package/src/layout/encode.ts +154 -0
- package/src/layout/index.ts +42 -0
- package/src/layout/mutation.ts +412 -0
- package/src/layout/node.ts +294 -0
- package/src/layout/offset.ts +19 -0
- package/src/layout/region.ts +151 -0
- package/src/layout/schema.ts +63 -0
- package/src/layout/selector.ts +82 -0
- package/src/layout/style.ts +160 -0
- package/src/layout/target.ts +32 -0
- package/src/layout/traverse.ts +28 -0
- package/src/performance/blank.ts +10 -0
- package/src/performance/encode.ts +31 -0
- package/src/performance/index.ts +12 -0
- package/src/performance/interaction.ts +125 -0
- package/src/performance/navigation.ts +31 -0
- package/src/performance/observer.ts +112 -0
- package/src/queue.ts +33 -0
- package/test/core.test.ts +139 -0
- package/test/helper.ts +167 -0
- package/test/html/core.html +34 -0
- package/test/stub.test.ts +7 -0
- package/test/tsconfig.test.json +6 -0
- package/tsconfig.json +21 -0
- package/tslint.json +33 -0
- package/types/agent.d.ts +39 -0
- package/types/core.d.ts +150 -0
- package/types/data.d.ts +568 -0
- package/types/diagnostic.d.ts +24 -0
- package/types/global.d.ts +30 -0
- package/types/index.d.ts +40 -0
- package/types/interaction.d.ts +174 -0
- package/types/layout.d.ts +277 -0
- package/types/performance.d.ts +31 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Action,
|
|
3
|
+
Visibility,
|
|
4
|
+
Availability,
|
|
5
|
+
VisibilityEvent,
|
|
6
|
+
AvailabilityEvent,
|
|
7
|
+
NewEvent,
|
|
8
|
+
AuthorType,
|
|
9
|
+
} from "@clarity-types/agent";
|
|
10
|
+
import encode from "./encode";
|
|
11
|
+
|
|
12
|
+
function visible(event: VisibilityEvent): void {
|
|
13
|
+
const visibility = event.visibility;
|
|
14
|
+
switch (visibility) {
|
|
15
|
+
case Visibility.Maximized:
|
|
16
|
+
encode(Action.AgentMaximized);
|
|
17
|
+
break;
|
|
18
|
+
case Visibility.Minimized:
|
|
19
|
+
encode(Action.AgentMinimized);
|
|
20
|
+
break;
|
|
21
|
+
case Visibility.Hidden:
|
|
22
|
+
encode(Action.AgentHidden);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function avail(event: AvailabilityEvent): void {
|
|
28
|
+
const isOnline = event.availability === Availability.Online;
|
|
29
|
+
encode(isOnline ? Action.AgentOnline : Action.AgentOffline);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function message(event: NewEvent): void {
|
|
33
|
+
if (event.author.type === AuthorType.Agent) {
|
|
34
|
+
encode(Action.AgentMessage);
|
|
35
|
+
} else {
|
|
36
|
+
encode(Action.HumanMessage);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function start(): void {
|
|
41
|
+
if (window.LiveChatWidget) {
|
|
42
|
+
window.LiveChatWidget.on("visibility_changed", visible);
|
|
43
|
+
window.LiveChatWidget.on("new_event", message);
|
|
44
|
+
window.LiveChatWidget.on("availability_changed", avail);
|
|
45
|
+
}
|
|
46
|
+
// Register stop callback with main Clarity
|
|
47
|
+
if (typeof window !== "undefined" && (window as any).clarity) {
|
|
48
|
+
(window as any).clarity("register", stop);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function stop(): void {
|
|
53
|
+
if (window.LiveChatWidget) {
|
|
54
|
+
window.LiveChatWidget.off("visibility_changed", visible);
|
|
55
|
+
window.LiveChatWidget.off("availability_changed", avail);
|
|
56
|
+
window.LiveChatWidget.off("new_event", message);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Action } from "@clarity-types/agent";
|
|
2
|
+
import encode from "./encode";
|
|
3
|
+
|
|
4
|
+
let isActive = false;
|
|
5
|
+
|
|
6
|
+
function open() {
|
|
7
|
+
if (!isActive) return;
|
|
8
|
+
encode(Action.AgentMaximized);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function close() {
|
|
12
|
+
if (!isActive) return;
|
|
13
|
+
encode(Action.AgentMinimized);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function human() {
|
|
17
|
+
if (!isActive) return;
|
|
18
|
+
encode(Action.HumanMessage);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function agent() {
|
|
22
|
+
if (!isActive) return;
|
|
23
|
+
encode(Action.AgentMessage);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function start(): void {
|
|
27
|
+
if (window.tidioChatApi) {
|
|
28
|
+
window.tidioChatApi.on("open", open);
|
|
29
|
+
window.tidioChatApi.on("close", close);
|
|
30
|
+
window.tidioChatApi.on("messageFromVisitor", human);
|
|
31
|
+
window.tidioChatApi.on("messageFromOperator", agent);
|
|
32
|
+
isActive = true;
|
|
33
|
+
}
|
|
34
|
+
// Register stop callback with main Clarity
|
|
35
|
+
if (typeof window !== "undefined" && (window as any).clarity) {
|
|
36
|
+
(window as any).clarity("register", stop);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function stop(): void {
|
|
41
|
+
if (window.tidioChatApi) {
|
|
42
|
+
isActive = false;
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/global.ts
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as clarity from "./clarity";
|
|
2
|
+
import hash from "./core/hash";
|
|
3
|
+
import * as selector from "./layout/selector";
|
|
4
|
+
import { get, getNode, lookup } from "./layout/dom";
|
|
5
|
+
|
|
6
|
+
const helper = { hash, selector, get, getNode, lookup };
|
|
7
|
+
const version = clarity.version;
|
|
8
|
+
|
|
9
|
+
export { clarity, version, helper };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export let state = [];
|
|
2
|
+
export let sheetAdoptionState = [];
|
|
3
|
+
export let sheetUpdateState = [];
|
|
4
|
+
export let data = null;
|
|
5
|
+
|
|
6
|
+
/* Intentionally blank module with empty code */
|
|
7
|
+
|
|
8
|
+
export function start(): void {}
|
|
9
|
+
export function reset(): void {}
|
|
10
|
+
export function stop(): void {}
|
|
11
|
+
export function log(): void {}
|
|
12
|
+
export function observe(): void {}
|
|
13
|
+
export function compute(): void {}
|
|
14
|
+
export function checkDocumentStyles(_d: Document): void {}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Privacy } from "@clarity-types/core";
|
|
2
|
+
import { Event, Token } from "@clarity-types/data";
|
|
3
|
+
import { Constant, NodeInfo } from "@clarity-types/layout";
|
|
4
|
+
import * as scrub from "@src/core/scrub";
|
|
5
|
+
import { time } from "@src/core/time";
|
|
6
|
+
import * as baseline from "@src/data/baseline";
|
|
7
|
+
import tokenize from "@src/data/token";
|
|
8
|
+
import { queue } from "@src/data/upload";
|
|
9
|
+
import * as snapshot from "@src/insight/snapshot";
|
|
10
|
+
import * as doc from "@src/layout/document";
|
|
11
|
+
|
|
12
|
+
export default async function (type: Event): Promise<void> {
|
|
13
|
+
let eventTime = time()
|
|
14
|
+
let tokens: Token[] = [eventTime, type];
|
|
15
|
+
switch (type) {
|
|
16
|
+
case Event.Document:
|
|
17
|
+
let d = doc.data;
|
|
18
|
+
tokens.push(d.width);
|
|
19
|
+
tokens.push(d.height);
|
|
20
|
+
baseline.track(type, d.width, d.height);
|
|
21
|
+
queue(tokens);
|
|
22
|
+
break;
|
|
23
|
+
case Event.Snapshot:
|
|
24
|
+
let values = snapshot.values;
|
|
25
|
+
// Only encode and queue DOM updates if we have valid updates to report back
|
|
26
|
+
if (values.length > 0) {
|
|
27
|
+
for (let value of values) {
|
|
28
|
+
let privacy = value.metadata.privacy;
|
|
29
|
+
let data: NodeInfo = value.data;
|
|
30
|
+
for (let key of ["tag", "attributes", "value"]) {
|
|
31
|
+
if (data[key]) {
|
|
32
|
+
switch (key) {
|
|
33
|
+
case "tag":
|
|
34
|
+
tokens.push(value.id);
|
|
35
|
+
if (value.parent) { tokens.push(value.parent); }
|
|
36
|
+
if (value.previous) { tokens.push(value.previous); }
|
|
37
|
+
tokens.push(data[key]);
|
|
38
|
+
break;
|
|
39
|
+
case "attributes":
|
|
40
|
+
for (let attr in data[key]) {
|
|
41
|
+
if (data[key][attr] !== undefined) {
|
|
42
|
+
tokens.push(attribute(attr, data[key][attr], privacy));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
break;
|
|
46
|
+
case "value":
|
|
47
|
+
tokens.push(scrub.text(data[key], data.tag, privacy));
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
queue(tokenize(tokens), true);
|
|
54
|
+
}
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function attribute(key: string, value: string, privacy: Privacy): string {
|
|
60
|
+
return `${key}=${scrub.text(value, key.indexOf(Constant.DataAttribute) === 0 ? Constant.DataAttribute : key, privacy)}`;
|
|
61
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
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
|
+
import config from "@src/core/config";
|
|
8
|
+
export let values: NodeValue[] = [];
|
|
9
|
+
let index: number = 1;
|
|
10
|
+
let idMap: WeakMap<Node, number> = null; // Maps node => id.
|
|
11
|
+
|
|
12
|
+
export function start(): void {
|
|
13
|
+
reset();
|
|
14
|
+
doc.start();
|
|
15
|
+
getId(document.documentElement); // Pre-discover ID for page root
|
|
16
|
+
interaction.observe(document);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function stop(): void {
|
|
20
|
+
reset();
|
|
21
|
+
doc.stop();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function compute(): void { /* Intentionally Blank */ }
|
|
25
|
+
export function iframe(): boolean { return false; }
|
|
26
|
+
export function offset(): OffsetDistance { return { x: 0, y: 0 }; }
|
|
27
|
+
export function hashText(): void { /* Intentionally Blank */ }
|
|
28
|
+
|
|
29
|
+
export function target(evt: UIEvent): Node {
|
|
30
|
+
let path = evt.composed && evt.composedPath ? evt.composedPath() : null;
|
|
31
|
+
let node = (path && path.length > 0 ? path[0] : evt.target) as Node;
|
|
32
|
+
return node.nodeType === Node.DOCUMENT_NODE ? (node as Document).documentElement : node;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function metadata(node: Node): TargetMetadata {
|
|
36
|
+
let output: TargetMetadata = { id: 0, hash: null, privacy: config.conversions ? Privacy.Sensitive : Privacy.Snapshot };
|
|
37
|
+
if (node) { output.id = idMap.has(node) ? idMap.get(node) : getId(node); }
|
|
38
|
+
return output;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function snapshot(): void {
|
|
42
|
+
values = [];
|
|
43
|
+
traverse(document);
|
|
44
|
+
encode(Event.Snapshot);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function reset(): void {
|
|
48
|
+
idMap = new WeakMap();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function traverse(root: Node): void {
|
|
52
|
+
let queue = [root];
|
|
53
|
+
while (queue.length > 0) {
|
|
54
|
+
let attributes = null, tag = null, value = null;
|
|
55
|
+
let node = queue.shift();
|
|
56
|
+
let next = node.firstChild;
|
|
57
|
+
let parent = node.parentElement ? node.parentElement : (node.parentNode ? node.parentNode : null);
|
|
58
|
+
|
|
59
|
+
while (next) {
|
|
60
|
+
queue.push(next);
|
|
61
|
+
next = next.nextSibling;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Process the node
|
|
65
|
+
let type = node.nodeType;
|
|
66
|
+
switch (type) {
|
|
67
|
+
case Node.DOCUMENT_TYPE_NODE:
|
|
68
|
+
let doctype = node as DocumentType;
|
|
69
|
+
tag = Constant.DocumentTag;
|
|
70
|
+
attributes = { name: doctype.name, publicId: doctype.publicId, systemId: doctype.systemId }
|
|
71
|
+
break;
|
|
72
|
+
case Node.TEXT_NODE:
|
|
73
|
+
value = node.nodeValue;
|
|
74
|
+
tag = idMap.get(parent) ? Constant.TextTag : tag;
|
|
75
|
+
break;
|
|
76
|
+
case Node.ELEMENT_NODE:
|
|
77
|
+
let element = node as HTMLElement;
|
|
78
|
+
attributes = getAttributes(element);
|
|
79
|
+
tag = ["NOSCRIPT", "SCRIPT", "STYLE"].indexOf(element.tagName) < 0 ? element.tagName : tag;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
add(node, parent, { tag, attributes, value });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/* Helper Functions - Snapshot Traversal */
|
|
87
|
+
function getAttributes(element: HTMLElement): { [key: string]: string } {
|
|
88
|
+
let output = {};
|
|
89
|
+
let attributes = element.attributes;
|
|
90
|
+
if (attributes && attributes.length > 0) {
|
|
91
|
+
for (let i = 0; i < attributes.length; i++) {
|
|
92
|
+
output[attributes[i].name] = attributes[i].value;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return output;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function getId(node: Node): number {
|
|
99
|
+
if (node === null) { return null; }
|
|
100
|
+
if (idMap.has(node)) { return idMap.get(node); }
|
|
101
|
+
idMap.set(node, index);
|
|
102
|
+
return index++;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function add(node: Node, parent: Node, data: NodeInfo): void {
|
|
106
|
+
if (node && data && data.tag) {
|
|
107
|
+
let id = getId(node);
|
|
108
|
+
let parentId = parent ? idMap.get(parent) : null;
|
|
109
|
+
let previous = node.previousSibling ? idMap.get(node.previousSibling) : null;
|
|
110
|
+
let metadata = { active: true, suspend: false, privacy: Privacy.Snapshot, position: null, fraud: null, size: null };
|
|
111
|
+
values.push({ id, parent: parentId, previous, children: [], data, selector: null, hash: null, region: null, metadata });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function get(_node: Node): NodeValue { return null; }
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Constant, Event, Setting } from "@clarity-types/data";
|
|
2
|
+
import { ChangeState } from "@clarity-types/interaction";
|
|
3
|
+
import config from "@src/core/config";
|
|
4
|
+
import { bind } from "@src/core/event";
|
|
5
|
+
import hash from "@src/core/hash";
|
|
6
|
+
import { schedule } from "@src/core/task";
|
|
7
|
+
import { time } from "@src/core/time";
|
|
8
|
+
import { target } from "@src/layout/target";
|
|
9
|
+
import encode from "./encode";
|
|
10
|
+
import { Mask } from "@clarity-types/layout";
|
|
11
|
+
|
|
12
|
+
export let state: ChangeState[] = [];
|
|
13
|
+
|
|
14
|
+
export function start(): void {
|
|
15
|
+
reset();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function observe(root: Node): void {
|
|
19
|
+
bind(root, "change", recompute, true);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function recompute(evt: UIEvent): void {
|
|
23
|
+
let element = target(evt) as HTMLInputElement;
|
|
24
|
+
if (element) {
|
|
25
|
+
let value = element.value;
|
|
26
|
+
let checksum = value && value.length >= Setting.WordLength && config.fraud && Mask.Exclude.indexOf(element.type) === -1 ? hash(value, Setting.ChecksumPrecision) : Constant.Empty;
|
|
27
|
+
state.push({ time: time(evt), event: Event.Change, data: { target: target(evt), type: element.type, value, checksum } });
|
|
28
|
+
schedule(encode.bind(this, Event.Change));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function reset(): void {
|
|
33
|
+
state = [];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function stop(): void {
|
|
37
|
+
reset();
|
|
38
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { BooleanFlag, Constant, Event, Setting } from "@clarity-types/data";
|
|
2
|
+
import { BrowsingContext, ClickState, TextInfo } from "@clarity-types/interaction";
|
|
3
|
+
import { Box } from "@clarity-types/layout";
|
|
4
|
+
import { bind } from "@src/core/event";
|
|
5
|
+
import { schedule } from "@src/core/task";
|
|
6
|
+
import { time } from "@src/core/time";
|
|
7
|
+
import { iframe } from "@src/layout/dom";
|
|
8
|
+
import { offset } from "@src/layout/offset";
|
|
9
|
+
import { target } from "@src/layout/target";
|
|
10
|
+
import encode from "./encode";
|
|
11
|
+
|
|
12
|
+
const UserInputTags = ["input", "textarea", "radio", "button", "canvas", "select"];
|
|
13
|
+
export let state: ClickState[] = [];
|
|
14
|
+
|
|
15
|
+
export function start(): void {
|
|
16
|
+
reset();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function observe(root: Node): void {
|
|
20
|
+
bind(root, "click", handler.bind(this, Event.Click, root), true);
|
|
21
|
+
bind(root, "contextmenu", handler.bind(this, Event.ContextMenu, root), true);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function handler(event: Event, root: Node, evt: MouseEvent): void {
|
|
25
|
+
let frame = iframe(root);
|
|
26
|
+
let d = frame && frame.contentDocument ? frame.contentDocument.documentElement : document.documentElement;
|
|
27
|
+
let x = "pageX" in evt ? Math.round(evt.pageX) : ("clientX" in evt ? Math.round(evt["clientX"] + d.scrollLeft) : null);
|
|
28
|
+
let y = "pageY" in evt ? Math.round(evt.pageY) : ("clientY" in evt ? Math.round(evt["clientY"] + d.scrollTop) : null);
|
|
29
|
+
// In case of iframe, we adjust (x,y) to be relative to top parent's origin
|
|
30
|
+
if (frame) {
|
|
31
|
+
let distance = offset(frame);
|
|
32
|
+
x = x ? x + Math.round(distance.x) : x;
|
|
33
|
+
y = y ? y + Math.round(distance.y) : y;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let t = target(evt);
|
|
37
|
+
// Find nearest anchor tag (<a/>) parent if current target node is part of one
|
|
38
|
+
// If present, we use the returned link element to populate text and link properties below
|
|
39
|
+
let a = link(t);
|
|
40
|
+
|
|
41
|
+
// Get layout rectangle for the target element
|
|
42
|
+
let l = layout(t as Element);
|
|
43
|
+
|
|
44
|
+
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail
|
|
45
|
+
// This property helps differentiate between a keyboard navigation vs. pointer click
|
|
46
|
+
// In case of a keyboard navigation, we use center of target element as (x,y)
|
|
47
|
+
if (evt.detail === 0 && l) {
|
|
48
|
+
x = Math.round(l.x + (l.w / 2));
|
|
49
|
+
y = Math.round(l.y + (l.h / 2));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let eX = l ? Math.max(Math.floor(((x - l.x) / l.w) * Setting.ClickPrecision), 0) : 0;
|
|
53
|
+
let eY = l ? Math.max(Math.floor(((y - l.y) / l.h) * Setting.ClickPrecision), 0) : 0;
|
|
54
|
+
|
|
55
|
+
// Check for null values before processing this event
|
|
56
|
+
if (x !== null && y !== null) {
|
|
57
|
+
const textInfo = text(t);
|
|
58
|
+
state.push({
|
|
59
|
+
time: time(evt), event, data: {
|
|
60
|
+
target: t,
|
|
61
|
+
x,
|
|
62
|
+
y,
|
|
63
|
+
eX,
|
|
64
|
+
eY,
|
|
65
|
+
button: evt.button,
|
|
66
|
+
reaction: reaction(t),
|
|
67
|
+
context: context(a),
|
|
68
|
+
text: textInfo.text,
|
|
69
|
+
link: a ? a.href : null,
|
|
70
|
+
hash: null,
|
|
71
|
+
trust: evt.isTrusted ? BooleanFlag.True : BooleanFlag.False,
|
|
72
|
+
isFullText: textInfo.isFullText,
|
|
73
|
+
w: l ? l.w : 0,
|
|
74
|
+
h: l ? l.h : 0
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
schedule(encode.bind(this, event));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function link(node: Node): HTMLAnchorElement {
|
|
82
|
+
while (node && node !== document) {
|
|
83
|
+
if (node.nodeType === Node.ELEMENT_NODE) {
|
|
84
|
+
let element = node as HTMLElement;
|
|
85
|
+
if (element.tagName === "A") {
|
|
86
|
+
return element as HTMLAnchorElement;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
node = node.parentNode;
|
|
90
|
+
}
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function text(element: Node): TextInfo {
|
|
95
|
+
let output = null;
|
|
96
|
+
let isFullText = false;
|
|
97
|
+
if (element) {
|
|
98
|
+
// Grab text using "textContent" for most HTMLElements, however, use "value" for HTMLInputElements and "alt" for HTMLImageElement.
|
|
99
|
+
let t = element.textContent || String((element as HTMLInputElement).value || '') || (element as HTMLImageElement).alt;
|
|
100
|
+
if (t) {
|
|
101
|
+
// Replace multiple occurrence of space characters with a single white space
|
|
102
|
+
// Also, trim any spaces at the beginning or at the end of string
|
|
103
|
+
const trimmedText = t.replace(/\s+/g, Constant.Space).trim();
|
|
104
|
+
// Finally, send only first few characters as specified by the Setting
|
|
105
|
+
output = trimmedText.substring(0, Setting.ClickText);
|
|
106
|
+
isFullText = output.length === trimmedText.length;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return { text: output, isFullText: isFullText ? BooleanFlag.True : BooleanFlag.False };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function reaction(element: Node): BooleanFlag {
|
|
114
|
+
if (element.nodeType === Node.ELEMENT_NODE) {
|
|
115
|
+
let tag = (element as HTMLElement).tagName.toLowerCase();
|
|
116
|
+
if (UserInputTags.indexOf(tag) >= 0) {
|
|
117
|
+
return BooleanFlag.False;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return BooleanFlag.True;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function layout(element: Element): Box {
|
|
124
|
+
let box: Box = null;
|
|
125
|
+
let de = document.documentElement;
|
|
126
|
+
if (typeof element.getBoundingClientRect === "function") {
|
|
127
|
+
// getBoundingClientRect returns rectangle relative positioning to viewport
|
|
128
|
+
let rect = element.getBoundingClientRect();
|
|
129
|
+
|
|
130
|
+
if (rect && rect.width > 0 && rect.height > 0) {
|
|
131
|
+
// Add viewport's scroll position to rectangle to get position relative to document origin
|
|
132
|
+
// Also: using Math.floor() instead of Math.round() because in Edge,
|
|
133
|
+
// getBoundingClientRect returns partial pixel values (e.g. 162.5px) and Chrome already
|
|
134
|
+
// floors the value (e.g. 162px). This keeps consistent behavior across browsers.
|
|
135
|
+
box = {
|
|
136
|
+
x: Math.floor(rect.left + ("pageXOffset" in window ? window.pageXOffset : de.scrollLeft)),
|
|
137
|
+
y: Math.floor(rect.top + ("pageYOffset" in window ? window.pageYOffset : de.scrollTop)),
|
|
138
|
+
w: Math.floor(rect.width),
|
|
139
|
+
h: Math.floor(rect.height)
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return box;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function context(a: HTMLAnchorElement): BrowsingContext {
|
|
147
|
+
if (a && a.hasAttribute(Constant.Target)) {
|
|
148
|
+
switch (a.getAttribute(Constant.Target)) {
|
|
149
|
+
case Constant.Blank: return BrowsingContext.Blank;
|
|
150
|
+
case Constant.Parent: return BrowsingContext.Parent;
|
|
151
|
+
case Constant.Top: return BrowsingContext.Top;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return BrowsingContext.Self;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function reset(): void {
|
|
158
|
+
state = [];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function stop(): void {
|
|
162
|
+
reset();
|
|
163
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Event } from "@clarity-types/data";
|
|
2
|
+
import { Clipboard, ClipboardState } from "@clarity-types/interaction";
|
|
3
|
+
import { bind } from "@src/core/event";
|
|
4
|
+
import { schedule } from "@src/core/task";
|
|
5
|
+
import { time } from "@src/core/time";
|
|
6
|
+
import encode from "./encode";
|
|
7
|
+
import { target } from "@src/layout/target";
|
|
8
|
+
|
|
9
|
+
export let state: ClipboardState[] = [];
|
|
10
|
+
|
|
11
|
+
export function start(): void {
|
|
12
|
+
reset();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function observe(root: Node): void {
|
|
16
|
+
bind(root, "cut", recompute.bind(this, Clipboard.Cut), true);
|
|
17
|
+
bind(root, "copy", recompute.bind(this, Clipboard.Copy), true);
|
|
18
|
+
bind(root, "paste", recompute.bind(this, Clipboard.Paste), true);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function recompute(action: Clipboard, evt: UIEvent): void {
|
|
22
|
+
state.push({ time: time(evt), event: Event.Clipboard, data: { target: target(evt), action } });
|
|
23
|
+
schedule(encode.bind(this, Event.Clipboard));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function reset(): void {
|
|
27
|
+
state = [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function stop(): void {
|
|
31
|
+
reset();
|
|
32
|
+
}
|