@gem-sdk/clarity-js 0.8.94

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 (127) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +1 -0
  3. package/build/clarity.extended.js +1 -0
  4. package/build/clarity.insight.js +1 -0
  5. package/build/clarity.js +7148 -0
  6. package/build/clarity.livechat.js +1 -0
  7. package/build/clarity.min.js +1 -0
  8. package/build/clarity.module.js +7144 -0
  9. package/build/clarity.performance.js +1 -0
  10. package/build/dynamic/clarity.crisp.js +1 -0
  11. package/build/dynamic/clarity.tidio.js +1 -0
  12. package/package.json +49 -0
  13. package/rollup.config.ts +161 -0
  14. package/src/clarity.ts +72 -0
  15. package/src/core/api.ts +8 -0
  16. package/src/core/config.ts +35 -0
  17. package/src/core/copy.ts +3 -0
  18. package/src/core/dynamic.ts +68 -0
  19. package/src/core/event.ts +53 -0
  20. package/src/core/hash.ts +19 -0
  21. package/src/core/history.ts +82 -0
  22. package/src/core/index.ts +81 -0
  23. package/src/core/measure.ts +19 -0
  24. package/src/core/report.ts +28 -0
  25. package/src/core/scrub.ts +201 -0
  26. package/src/core/task.ts +181 -0
  27. package/src/core/throttle.ts +46 -0
  28. package/src/core/time.ts +26 -0
  29. package/src/core/timeout.ts +10 -0
  30. package/src/core/version.ts +2 -0
  31. package/src/custom/README.md +44 -0
  32. package/src/custom/dialog.ts +69 -0
  33. package/src/custom/index.ts +6 -0
  34. package/src/data/baseline.ts +162 -0
  35. package/src/data/compress.ts +31 -0
  36. package/src/data/consent.ts +84 -0
  37. package/src/data/cookie.ts +90 -0
  38. package/src/data/custom.ts +23 -0
  39. package/src/data/dimension.ts +53 -0
  40. package/src/data/encode.ts +155 -0
  41. package/src/data/envelope.ts +53 -0
  42. package/src/data/extract.ts +217 -0
  43. package/src/data/gemx-upload.ts +51 -0
  44. package/src/data/gemx.ts +35 -0
  45. package/src/data/index.ts +57 -0
  46. package/src/data/limit.ts +44 -0
  47. package/src/data/metadata.ts +340 -0
  48. package/src/data/metric.ts +51 -0
  49. package/src/data/ping.ts +36 -0
  50. package/src/data/signal.ts +30 -0
  51. package/src/data/summary.ts +34 -0
  52. package/src/data/token.ts +39 -0
  53. package/src/data/upgrade.ts +44 -0
  54. package/src/data/upload.ts +375 -0
  55. package/src/data/util.ts +18 -0
  56. package/src/data/variable.ts +84 -0
  57. package/src/diagnostic/encode.ts +40 -0
  58. package/src/diagnostic/fraud.ts +37 -0
  59. package/src/diagnostic/index.ts +13 -0
  60. package/src/diagnostic/internal.ts +28 -0
  61. package/src/diagnostic/script.ts +35 -0
  62. package/src/dynamic/agent/blank.ts +2 -0
  63. package/src/dynamic/agent/crisp.ts +40 -0
  64. package/src/dynamic/agent/encode.ts +25 -0
  65. package/src/dynamic/agent/index.ts +8 -0
  66. package/src/dynamic/agent/livechat.ts +58 -0
  67. package/src/dynamic/agent/tidio.ts +44 -0
  68. package/src/global.ts +6 -0
  69. package/src/index.ts +10 -0
  70. package/src/insight/blank.ts +14 -0
  71. package/src/insight/encode.ts +64 -0
  72. package/src/insight/snapshot.ts +115 -0
  73. package/src/interaction/change.ts +38 -0
  74. package/src/interaction/click.ts +256 -0
  75. package/src/interaction/clipboard.ts +32 -0
  76. package/src/interaction/encode.ts +217 -0
  77. package/src/interaction/focus.ts +25 -0
  78. package/src/interaction/index.ts +66 -0
  79. package/src/interaction/input.ts +66 -0
  80. package/src/interaction/pageshow.ts +35 -0
  81. package/src/interaction/pointer.ts +137 -0
  82. package/src/interaction/resize.ts +50 -0
  83. package/src/interaction/scroll.ts +134 -0
  84. package/src/interaction/selection.ts +66 -0
  85. package/src/interaction/submit.ts +30 -0
  86. package/src/interaction/timeline.ts +69 -0
  87. package/src/interaction/unload.ts +32 -0
  88. package/src/interaction/visibility.ts +28 -0
  89. package/src/layout/animation.ts +133 -0
  90. package/src/layout/constants.ts +13 -0
  91. package/src/layout/custom.ts +48 -0
  92. package/src/layout/discover.ts +31 -0
  93. package/src/layout/document.ts +46 -0
  94. package/src/layout/dom.ts +444 -0
  95. package/src/layout/encode.ts +157 -0
  96. package/src/layout/gemx-snapshot.ts +84 -0
  97. package/src/layout/index.ts +47 -0
  98. package/src/layout/mutation.ts +424 -0
  99. package/src/layout/node.ts +302 -0
  100. package/src/layout/offset.ts +19 -0
  101. package/src/layout/region.ts +151 -0
  102. package/src/layout/schema.ts +63 -0
  103. package/src/layout/selector.ts +113 -0
  104. package/src/layout/style.ts +160 -0
  105. package/src/layout/target.ts +34 -0
  106. package/src/layout/traverse.ts +28 -0
  107. package/src/performance/blank.ts +10 -0
  108. package/src/performance/encode.ts +31 -0
  109. package/src/performance/index.ts +12 -0
  110. package/src/performance/interaction.ts +125 -0
  111. package/src/performance/navigation.ts +31 -0
  112. package/src/performance/observer.ts +112 -0
  113. package/src/queue.ts +33 -0
  114. package/test/consentv2.test.ts +1309 -0
  115. package/test/hash.test.ts +68 -0
  116. package/test/time.test.ts +42 -0
  117. package/tsconfig.json +12 -0
  118. package/tslint.json +33 -0
  119. package/types/agent.d.ts +39 -0
  120. package/types/core.d.ts +156 -0
  121. package/types/data.d.ts +611 -0
  122. package/types/diagnostic.d.ts +24 -0
  123. package/types/global.d.ts +43 -0
  124. package/types/index.d.ts +46 -0
  125. package/types/interaction.d.ts +187 -0
  126. package/types/layout.d.ts +271 -0
  127. package/types/performance.d.ts +34 -0
@@ -0,0 +1,47 @@
1
+ import * as discover from "@src/layout/discover";
2
+ import * as gemxSnapshot from "@src/layout/gemx-snapshot";
3
+ import * as doc from "@src/layout/document";
4
+ import * as dom from "@src/layout/dom";
5
+ import * as mutation from "@src/layout/mutation";
6
+ import * as region from "@src/layout/region";
7
+ import * as style from "@src/layout/style";
8
+ import * as animation from "@src/layout/animation";
9
+ import * as custom from "@src/layout/custom";
10
+ import { bind } from "@src/core/event";
11
+ import config from "@src/core/config";
12
+
13
+ export { hashText } from "@src/layout/dom";
14
+
15
+ export function start(): void {
16
+ // The order below is important
17
+ // and is determined by interdependencies of modules
18
+ doc.start();
19
+ region.start();
20
+ dom.start();
21
+ if (config.delayDom) {
22
+ // Lazy load layout module as part of page load time performance improvements experiment
23
+ bind(window, 'load', () => {
24
+ mutation.start();
25
+ });
26
+ } else {
27
+ mutation.start();
28
+ }
29
+ // IMPORTANT: Start custom element detection BEFORE discover
30
+ // This ensures pre-existing custom elements are registered before DOM traversal
31
+ custom.start();
32
+ gemxSnapshot.start(); // activates upload buffer before discover traverses
33
+ discover.start();
34
+ style.start();
35
+ animation.start();
36
+ }
37
+
38
+ export function stop(): void {
39
+ region.stop();
40
+ dom.stop();
41
+ mutation.stop();
42
+ doc.stop();
43
+ style.stop();
44
+ animation.stop();
45
+ custom.stop();
46
+ gemxSnapshot.stop();
47
+ }
@@ -0,0 +1,424 @@
1
+ import { Priority, Task, Timer } from "@clarity-types/core";
2
+ import { Code, Event, Metric, Severity } from "@clarity-types/data";
3
+ import { Constant, MutationHistory, MutationRecordWithTime, MutationQueue, Setting, Source } from "@clarity-types/layout";
4
+ import api from "@src/core/api";
5
+ import * as core from "@src/core";
6
+ import * as event from "@src/core/event";
7
+ import measure from "@src/core/measure";
8
+ import * as task from "@src/core/task";
9
+ import { time } from "@src/core/time";
10
+ import { clearTimeout, setTimeout } from "@src/core/timeout";
11
+ import { id } from "@src/data/metadata";
12
+ import * as summary from "@src/data/summary";
13
+ import * as internal from "@src/diagnostic/internal";
14
+ import * as doc from "@src/layout/document";
15
+ import * as dom from "@src/layout/dom";
16
+ import * as metric from "@src/data/metric";
17
+ import encode from "@src/layout/encode";
18
+ import * as region from "@src/layout/region";
19
+ import traverse from "@src/layout/traverse";
20
+ import processNode from "./node";
21
+ import config from "@src/core/config";
22
+ import * as gemx from "@src/data/gemx";
23
+ import * as gemxUpload from "@src/data/gemx-upload";
24
+
25
+
26
+ let observers: Set<MutationObserver> = new Set();
27
+ let mutations: MutationQueue[] = [];
28
+ let throttledMutations: { [key: number]: MutationRecordWithTime } = {};
29
+ let queue: Node[] = [];
30
+ let timeout: number = null;
31
+ let throttleDelay: number = null;
32
+ let activePeriod = null;
33
+ let history: MutationHistory = {};
34
+ let observedNodes: WeakMap<Node, MutationObserver> = new WeakMap<Node, MutationObserver>();
35
+
36
+ // We ignore mutations if these attributes are updated
37
+ const IGNORED_ATTRIBUTES = ["data-google-query-id", "data-load-complete", "data-google-container-id"];
38
+
39
+ export function start(): void {
40
+ observers = new Set();
41
+ queue = [];
42
+ timeout = null;
43
+ activePeriod = 0;
44
+ history = {};
45
+ observedNodes = new WeakMap<Node, MutationObserver>();
46
+ proxyStyleRules(window);
47
+ }
48
+
49
+ export function observe(node: Document | ShadowRoot): void {
50
+ // Create a new observer for every time a new DOM tree (e.g. root document or shadowdom root) is discovered on the page
51
+ // In the case of shadow dom, any mutations that happen within the shadow dom are not bubbled up to the host document
52
+ // For this reason, we need to wire up mutations every time we see a new shadow dom.
53
+ // Also, wrap it inside a try / catch. In certain browsers (e.g. legacy Edge), observer on shadow dom can throw errors
54
+ try {
55
+
56
+ let m = api(Constant.MutationObserver);
57
+ let observer = m in window ? new window[m](measure(handle) as MutationCallback) : null;
58
+ if (observer) {
59
+ observer.observe(node, { attributes: true, childList: true, characterData: true, subtree: true });
60
+ observedNodes.set(node, observer);
61
+ observers.add(observer);
62
+ }
63
+ if (node['defaultView']) {
64
+ proxyStyleRules(node['defaultView']);
65
+ }
66
+
67
+ } catch (e) {
68
+ internal.log(Code.MutationObserver, Severity.Info, e ? e.name : null);
69
+ }
70
+ }
71
+
72
+ export function monitor(frame: HTMLIFrameElement): void {
73
+ // Bind to iframe's onload event so we get notified anytime there's an update to iframe content.
74
+ // This includes cases where iframe location is updated without explicitly updating src attribute
75
+ // E.g. iframe.contentWindow.location.href = "new-location";
76
+ if (dom.has(frame) === false) {
77
+ event.bind(frame, Constant.LoadEvent, generate.bind(this, frame, Constant.ChildList), true);
78
+ }
79
+ }
80
+
81
+ export function stop(): void {
82
+ for (let observer of Array.from(observers)) {
83
+ if (observer) {
84
+ observer.disconnect();
85
+ }
86
+ }
87
+ observers = new Set();
88
+ history = {};
89
+ mutations = [];
90
+ throttledMutations = {};
91
+ queue = [];
92
+ activePeriod = 0;
93
+ timeout = null;
94
+ observedNodes = new WeakMap();
95
+ }
96
+
97
+ export function active(): void {
98
+ activePeriod = time() + Setting.MutationActivePeriod;
99
+ }
100
+
101
+ export function disconnect(n: Node): void {
102
+ const ob = observedNodes.get(n);
103
+ if (ob) {
104
+ ob.disconnect();
105
+ observers.delete(ob);
106
+ observedNodes.delete(n);
107
+ }
108
+ }
109
+
110
+ function handle(m: MutationRecord[]): void {
111
+ // Queue up mutation records for asynchronous processing
112
+ let now = time();
113
+ summary.track(Event.Mutation, now);
114
+ mutations.push({ time: now, mutations: m });
115
+ task.schedule(process, Priority.High).then((): void => {
116
+ setTimeout(doc.compute);
117
+ measure(region.compute)();
118
+ });
119
+ }
120
+
121
+ async function processMutation(timer: Timer, mutation: MutationRecord, instance: number, timestamp: number): Promise<void> {
122
+ let state = task.state(timer);
123
+
124
+ if (state === Task.Wait) {
125
+ state = await task.suspend(timer);
126
+ }
127
+ if (state === Task.Stop) {
128
+ return;
129
+ }
130
+
131
+ let target = mutation.target;
132
+
133
+ const ignoredClasses = gemx.get().ignoreMutationClassNames;
134
+ // Before processed snapshot, we need to check if the mutation is ignored
135
+ const isSnapshotProcessed = gemxUpload.isActive();
136
+ if (!isSnapshotProcessed && ignoredClasses.length > 0 && target instanceof Element) {
137
+ const el = target as Element;
138
+ if (ignoredClasses.some(cls => el.classList.contains(cls))) { return; }
139
+ }
140
+
141
+ let type = config.throttleDom ? track(mutation, timer, instance, timestamp) : mutation.type;
142
+
143
+ if (type && target && target.ownerDocument) {
144
+ dom.parse(target.ownerDocument);
145
+ }
146
+
147
+ if (type && target && target.nodeType == Node.DOCUMENT_FRAGMENT_NODE && (target as ShadowRoot).host) {
148
+ dom.parse(target as ShadowRoot);
149
+ }
150
+
151
+ switch (type) {
152
+ case Constant.Attributes:
153
+ if (IGNORED_ATTRIBUTES.indexOf(mutation.attributeName) < 0) {
154
+ processNode(target, Source.Attributes, timestamp);
155
+ }
156
+ break;
157
+ case Constant.CharacterData:
158
+ processNode(target, Source.CharacterData, timestamp);
159
+ break;
160
+ case Constant.ChildList:
161
+ processNodeList(mutation.addedNodes, Source.ChildListAdd, timer, timestamp);
162
+ processNodeList(mutation.removedNodes, Source.ChildListRemove, timer, timestamp);
163
+ break;
164
+ case Constant.Throttle:
165
+ default:
166
+ break;
167
+ }
168
+ }
169
+ async function process(): Promise<void> {
170
+ let timer: Timer = { id: id(), cost: Metric.LayoutCost };
171
+ task.start(timer);
172
+ while (mutations.length > 0) {
173
+ let record = mutations.shift();
174
+ let instance = time();
175
+ for (let mutation of record.mutations) {
176
+ await processMutation(timer, mutation, instance, record.time);
177
+ }
178
+ await encode(Event.Mutation, timer, record.time);
179
+ }
180
+
181
+ let processedMutations = false;
182
+ for (var key of Object.keys(throttledMutations)) {
183
+ let throttledMutationToProcess: MutationRecordWithTime = throttledMutations[key];
184
+ delete throttledMutations[key];
185
+ await processMutation(timer, throttledMutationToProcess.mutation, time(), throttledMutationToProcess.timestamp);
186
+ processedMutations = true;
187
+ }
188
+
189
+ if (Object.keys(throttledMutations).length > 0) {
190
+ processThrottledMutations();
191
+ }
192
+
193
+ // ensure we encode the previously throttled mutations once we have finished them
194
+ if (Object.keys(throttledMutations).length === 0 && processedMutations) {
195
+ await encode(Event.Mutation, timer, time());
196
+ }
197
+
198
+ cleanHistory();
199
+
200
+ task.stop(timer);
201
+ }
202
+
203
+ function cleanHistory(): void {
204
+ let now = time();
205
+ if (Object.keys(history).length > Setting.MaxMutationHistoryCount) {
206
+ history = {};
207
+ metric.count(Metric.HistoryClear);
208
+ }
209
+
210
+ for (let key of Object.keys(history)) {
211
+ let h = history[key];
212
+ if (now > h[1] + Setting.MaxMutationHistoryTime) {
213
+ delete history[key];
214
+ }
215
+ }
216
+ }
217
+
218
+ function track(m: MutationRecord, timer: Timer, instance: number, timestamp: number): string {
219
+ let value = m.target ? dom.get(m.target.parentNode) : null;
220
+
221
+ // Check if the parent is already discovered and that the parent is not the document root
222
+ if (value && value.data.tag !== Constant.HTML) {
223
+ // calculate inactive period based on the timestamp of the mutation not when the mutation is processed
224
+ let inactive = timestamp > activePeriod;
225
+
226
+ // Calculate critical period based on when mutation is processed
227
+ let target = dom.get(m.target);
228
+ let element = target && target.selector ? target.selector.join() : m.target.nodeName;
229
+ let parent = value.selector ? value.selector.join() : Constant.Empty;
230
+
231
+ // We use selector, instead of id, to determine the key (signature for the mutation) because in some cases
232
+ // repeated mutations can cause elements to be destroyed and then recreated as new DOM nodes
233
+ // In those cases, IDs will change however the selector (which is relative to DOM xPath) remains the same
234
+ let key = [parent, element, m.attributeName, names(m.addedNodes), names(m.removedNodes)].join();
235
+
236
+ // Initialize an entry if it doesn't already exist
237
+ history[key] = key in history ? history[key] : [0, instance];
238
+ let h = history[key];
239
+
240
+ // Lookup any pending nodes queued up for removal, and process them now if we suspended a mutation before
241
+ if (inactive === false && h[0] >= Setting.MutationSuspendThreshold) {
242
+ processNodeList(h[2], Source.ChildListRemove, timer, timestamp);
243
+ }
244
+
245
+ // Update the counter, do not reset counter if its critical period
246
+ h[0] = inactive ? (h[1] === instance ? h[0] : h[0] + 1) : 1;
247
+ h[1] = instance;
248
+
249
+ // Return updated mutation type based on,
250
+ // 1. if we have already hit the threshold or not
251
+ // 2. if its a low priority mutation happening during critical time period
252
+ if (h[0] >= Setting.MutationSuspendThreshold) {
253
+ // Store a reference to removedNodes so we can process them later
254
+ // when we resume mutations again on user interactions
255
+ h[2] = m.removedNodes;
256
+ if (instance > timestamp + Setting.MutationActivePeriod) {
257
+ return m.type;
258
+ }
259
+
260
+ // we only store the most recent mutation for a given key if it is being throttled
261
+ throttledMutations[key] = { mutation: m, timestamp };
262
+
263
+ return Constant.Throttle;
264
+ }
265
+ }
266
+ return m.type;
267
+ }
268
+
269
+ function names(nodes: NodeList): string {
270
+ let output: string[] = [];
271
+ for (let i = 0; nodes && i < nodes.length; i++) {
272
+ output.push(nodes[i].nodeName);
273
+ }
274
+ return output.join();
275
+ }
276
+
277
+ async function processNodeList(list: NodeList, source: Source, timer: Timer, timestamp: number): Promise<void> {
278
+ let length = list ? list.length : 0;
279
+ for (let i = 0; i < length; i++) {
280
+ const node = list[i];
281
+ if (source === Source.ChildListAdd) {
282
+ traverse(node, timer, source, timestamp);
283
+ } else {
284
+ let state = task.state(timer);
285
+ if (state === Task.Wait) {
286
+ state = await task.suspend(timer);
287
+ }
288
+ if (state === Task.Stop) {
289
+ break;
290
+ }
291
+ processNode(node, source, timestamp);
292
+ }
293
+ }
294
+ }
295
+
296
+ function processThrottledMutations(): void {
297
+ if (throttleDelay) {
298
+ clearTimeout(throttleDelay);
299
+ }
300
+ throttleDelay = setTimeout(() => {
301
+ task.schedule(process, Priority.High);
302
+ }, Setting.LookAhead);
303
+ }
304
+
305
+ export function schedule(node: Node): Node {
306
+ // Only schedule manual trigger for this node if it's not already in the queue
307
+ if (queue.indexOf(node) < 0) {
308
+ queue.push(node);
309
+ }
310
+
311
+ // Cancel any previous trigger before scheduling a new one.
312
+ // It's common for a webpage to call multiple synchronous "insertRule" / "deleteRule" calls.
313
+ // And in those cases we do not wish to monitor changes multiple times for the same node.
314
+ if (timeout) {
315
+ clearTimeout(timeout);
316
+ }
317
+ timeout = setTimeout(() => {
318
+ trigger();
319
+ }, Setting.LookAhead);
320
+
321
+ return node;
322
+ }
323
+
324
+ function trigger(): void {
325
+ for (let node of queue) {
326
+ // Generate a mutation for this node only if it still exists
327
+ if (node) {
328
+ let shadowRoot = node.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
329
+ // Skip re-processing shadowRoot if it was already discovered
330
+ if (shadowRoot && dom.has(node)) {
331
+ continue;
332
+ }
333
+ generate(node, shadowRoot ? Constant.ChildList : Constant.CharacterData);
334
+ }
335
+ }
336
+ queue = [];
337
+ }
338
+
339
+ function generate(target: Node, type: MutationRecordType): void {
340
+ measure(handle)([
341
+ {
342
+ addedNodes: [target],
343
+ attributeName: null,
344
+ attributeNamespace: null,
345
+ nextSibling: null,
346
+ oldValue: null,
347
+ previousSibling: null,
348
+ removedNodes: [],
349
+ target,
350
+ type,
351
+ },
352
+ ]);
353
+ }
354
+
355
+
356
+ function proxyStyleRules(win: any): void {
357
+ if (win === null || win === undefined) {
358
+ return;
359
+ }
360
+
361
+ win.clarityOverrides = win.clarityOverrides || {};
362
+
363
+ // Some popular open source libraries, like styled-components, optimize performance
364
+ // by injecting CSS using insertRule API vs. appending text node. A side effect of
365
+ // using javascript API is that it doesn't trigger DOM mutation and therefore we
366
+ // need to override the insertRule API and listen for changes manually.
367
+ if ("CSSStyleSheet" in win && win.CSSStyleSheet && win.CSSStyleSheet.prototype && win.clarityOverrides.InsertRule === undefined) {
368
+ win.clarityOverrides.InsertRule = win.CSSStyleSheet.prototype.insertRule;
369
+ win.CSSStyleSheet.prototype.insertRule = function (): number {
370
+ if (core.active()) {
371
+ schedule(this.ownerNode);
372
+ }
373
+ return win.clarityOverrides.InsertRule.apply(this, arguments);
374
+ };
375
+ }
376
+
377
+ if ("CSSMediaRule" in win && win.CSSMediaRule && win.CSSMediaRule.prototype && win.clarityOverrides.MediaInsertRule === undefined) {
378
+ win.clarityOverrides.MediaInsertRule = win.CSSMediaRule.prototype.insertRule;
379
+ win.CSSMediaRule.prototype.insertRule = function (): number {
380
+ if (core.active()) {
381
+ schedule(this.parentStyleSheet.ownerNode);
382
+ }
383
+ return win.clarityOverrides.MediaInsertRule.apply(this, arguments);
384
+ };
385
+ }
386
+
387
+ if ("CSSStyleSheet" in win && win.CSSStyleSheet && win.CSSStyleSheet.prototype && win.clarityOverrides.DeleteRule === undefined) {
388
+ win.clarityOverrides.DeleteRule = win.CSSStyleSheet.prototype.deleteRule;
389
+ win.CSSStyleSheet.prototype.deleteRule = function (): void {
390
+ if (core.active()) {
391
+ schedule(this.ownerNode);
392
+ }
393
+ return win.clarityOverrides.DeleteRule.apply(this, arguments);
394
+ };
395
+ }
396
+
397
+ if ("CSSMediaRule" in win && win.CSSMediaRule && win.CSSMediaRule.prototype && win.clarityOverrides.MediaDeleteRule === undefined) {
398
+ win.clarityOverrides.MediaDeleteRule = win.CSSMediaRule.prototype.deleteRule;
399
+ win.CSSMediaRule.prototype.deleteRule = function (): void {
400
+ if (core.active()) {
401
+ schedule(this.parentStyleSheet.ownerNode);
402
+ }
403
+ return win.clarityOverrides.MediaDeleteRule.apply(this, arguments);
404
+ };
405
+ }
406
+
407
+ // Add a hook to attachShadow API calls
408
+ // In case we are unable to add a hook and browser throws an exception,
409
+ // reset attachShadow variable and resume processing like before
410
+ if ("Element" in win && win.Element && win.Element.prototype && win.clarityOverrides.AttachShadow === undefined) {
411
+ win.clarityOverrides.AttachShadow = win.Element.prototype.attachShadow;
412
+ try {
413
+ win.Element.prototype.attachShadow = function (): ShadowRoot {
414
+ if (core.active()) {
415
+ return schedule(win.clarityOverrides.AttachShadow.apply(this, arguments)) as ShadowRoot;
416
+ } else {
417
+ return win.clarityOverrides.AttachShadow.apply(this, arguments);
418
+ }
419
+ };
420
+ } catch {
421
+ win.clarityOverrides.AttachShadow = null;
422
+ }
423
+ }
424
+ }