@luma-ai/react 0.1.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/README.md +18 -12
- package/dist/chunks/src-C48p0wsr.js +7647 -0
- package/dist/chunks/src-C48p0wsr.js.map +1 -0
- package/dist/chunks/support-cursor-BU9gztRA.js +131 -0
- package/dist/chunks/support-cursor-BU9gztRA.js.map +1 -0
- package/dist/chunks/visitor-call-CoKCjk8c.js +1129 -0
- package/dist/chunks/visitor-call-CoKCjk8c.js.map +1 -0
- package/dist/chunks/visitor-cobrowse-Ba7H1VtS.js +1415 -0
- package/dist/chunks/visitor-cobrowse-Ba7H1VtS.js.map +1 -0
- package/dist/{config-CCT7m9eE.d.ts → config-BAC3xPNG.d.ts} +1 -6
- package/dist/index-ByvZMuLw.d.ts +415 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -2
- package/dist/next.d.ts +3 -5
- package/dist/next.js +2 -3
- package/dist/next.js.map +1 -1
- package/dist/react-router.d.ts +3 -5
- package/dist/react-router.js +2 -3
- package/dist/react-router.js.map +1 -1
- package/dist/server.d.ts +1 -3
- package/dist/server.js +11 -12
- package/dist/server.js.map +1 -1
- package/dist/tanstack.d.ts +3 -5
- package/dist/tanstack.js +2 -3
- package/dist/tanstack.js.map +1 -1
- package/docs/ai-agents.md +5 -5
- package/docs/appearance.md +92 -0
- package/docs/data-sources.md +18 -18
- package/docs/features.md +62 -0
- package/docs/hooks.md +22 -8
- package/docs/index.md +17 -7
- package/docs/options.md +58 -51
- package/docs/privacy.md +29 -6
- package/docs/server.md +11 -7
- package/docs/troubleshooting.md +16 -8
- package/package.json +28 -21
- package/skills/luma-ai-react/SKILL.md +32 -13
- package/dist/chunks/src-dnp1waFV.js +0 -3100
- package/dist/chunks/src-dnp1waFV.js.map +0 -1
- package/dist/index-BdkoFDyP.d.ts +0 -189
|
@@ -0,0 +1,1415 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { c as COBROWSE_LIMITS, u as pointOf } from "./src-C48p0wsr.js";
|
|
3
|
+
import { t as createSupportCursor } from "./support-cursor-BU9gztRA.js";
|
|
4
|
+
// src/core/page-outline.ts
|
|
5
|
+
/**
|
|
6
|
+
* Which fields are secret wherever the widget shows support the page
|
|
7
|
+
* (co-browsing masks them, `cobrowse/mask.ts`): password, payment and
|
|
8
|
+
* one-time-code fields, by their attributes and by their label.
|
|
9
|
+
*/
|
|
10
|
+
var SENSITIVE_AUTOCOMPLETE = /\b(cc-|current-password|new-password|one-time-code|transaction-)/i;
|
|
11
|
+
var SENSITIVE_NAME = /(pass(word|wd)?|pin\b|otp|cvv|cvc|cvv2|card|iban|sheba|شبا|کارت|رمز|expir|security.?code)/i;
|
|
12
|
+
/** A password, payment or one-time-code field, by its attributes. */
|
|
13
|
+
function sensitiveFacts(field) {
|
|
14
|
+
if (field.tag !== "input" && field.tag !== "select") return false;
|
|
15
|
+
if (field.tag === "input" && field.type?.toLowerCase() === "password") return true;
|
|
16
|
+
if (SENSITIVE_AUTOCOMPLETE.test(field.autocomplete ?? "")) return true;
|
|
17
|
+
return SENSITIVE_NAME.test(field.names.join(" "));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A field whose label must not leave the browser either. `label` is its
|
|
21
|
+
* resolved accessible name (a `<label for>`, `aria-labelledby`), which can
|
|
22
|
+
* be the only place that says "IBAN" or "card number".
|
|
23
|
+
*/
|
|
24
|
+
function isSensitiveField(element, label = "") {
|
|
25
|
+
return sensitiveFacts({
|
|
26
|
+
tag: element.tagName.toLowerCase(),
|
|
27
|
+
type: element.getAttribute("type") ?? void 0,
|
|
28
|
+
autocomplete: element.getAttribute("autocomplete") ?? void 0,
|
|
29
|
+
names: [
|
|
30
|
+
element.getAttribute("name") ?? "",
|
|
31
|
+
element.id,
|
|
32
|
+
element.getAttribute("aria-label") ?? "",
|
|
33
|
+
element.getAttribute("placeholder") ?? "",
|
|
34
|
+
label
|
|
35
|
+
]
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
// src/cobrowse/mask.ts
|
|
39
|
+
/**
|
|
40
|
+
* What co-browsing (phase 14) may copy of an element, decided in the
|
|
41
|
+
* visitor's browser before anything is sent. The rules, in short:
|
|
42
|
+
* - only what the page draws: no text or attributes of an element that is
|
|
43
|
+
* not rendered (`display: none`, `hidden`, `visibility: hidden`…), see
|
|
44
|
+
* `rendering`;
|
|
45
|
+
* - only the attributes drawing needs (an allowlist): no `data-*` but a few
|
|
46
|
+
* UI state keywords, no labels nobody sees, no tokens in attributes;
|
|
47
|
+
* - nothing the visitor enters: no `value`, `checked` or `selected`; text
|
|
48
|
+
* areas, selects and editable regions are copied empty; the widget never
|
|
49
|
+
* reads a field's value or listens to typing;
|
|
50
|
+
* - password, payment and one-time-code fields (by their attributes and by
|
|
51
|
+
* their resolved label) are empty boxes without even their placeholder;
|
|
52
|
+
* - private regions (`data-luma-private`, the site's own) are empty boxes;
|
|
53
|
+
* - no scripts, inline event handlers, `javascript:` or other non-web URLs;
|
|
54
|
+
* - no iframe content, canvas, video or audio (boxes of their size);
|
|
55
|
+
* - links lose their query and fragment (they can carry tokens).
|
|
56
|
+
*/
|
|
57
|
+
/** Removed with their whole subtree. */
|
|
58
|
+
var DROPPED = /* @__PURE__ */ new Set([
|
|
59
|
+
"script",
|
|
60
|
+
"noscript",
|
|
61
|
+
"template",
|
|
62
|
+
"meta",
|
|
63
|
+
"base",
|
|
64
|
+
"datalist",
|
|
65
|
+
"slot-fallback",
|
|
66
|
+
"animate",
|
|
67
|
+
"animatemotion",
|
|
68
|
+
"animatetransform",
|
|
69
|
+
"set",
|
|
70
|
+
"discard"
|
|
71
|
+
]);
|
|
72
|
+
var MEDIA = /* @__PURE__ */ new Set([
|
|
73
|
+
"video",
|
|
74
|
+
"audio",
|
|
75
|
+
"canvas",
|
|
76
|
+
"object",
|
|
77
|
+
"embed",
|
|
78
|
+
"applet"
|
|
79
|
+
]);
|
|
80
|
+
var FRAMES = /* @__PURE__ */ new Set([
|
|
81
|
+
"iframe",
|
|
82
|
+
"frame",
|
|
83
|
+
"frameset",
|
|
84
|
+
"portal",
|
|
85
|
+
"fencedframe"
|
|
86
|
+
]);
|
|
87
|
+
var FIELDS = /* @__PURE__ */ new Set([
|
|
88
|
+
"input",
|
|
89
|
+
"textarea",
|
|
90
|
+
"select"
|
|
91
|
+
]);
|
|
92
|
+
/** Inputs that are buttons (their type is not something typed into). */
|
|
93
|
+
var BUTTON_INPUTS = /* @__PURE__ */ new Set([
|
|
94
|
+
"submit",
|
|
95
|
+
"button",
|
|
96
|
+
"reset",
|
|
97
|
+
"image"
|
|
98
|
+
]);
|
|
99
|
+
/** Attributes holding an address: made absolute, kept only for the web. */
|
|
100
|
+
var URL_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
101
|
+
"href",
|
|
102
|
+
"src",
|
|
103
|
+
"xlink:href",
|
|
104
|
+
"background"
|
|
105
|
+
]);
|
|
106
|
+
/** On any element. */
|
|
107
|
+
var DRAWN = /* @__PURE__ */ new Set([
|
|
108
|
+
"class",
|
|
109
|
+
"style",
|
|
110
|
+
"id",
|
|
111
|
+
"dir",
|
|
112
|
+
"lang",
|
|
113
|
+
"role",
|
|
114
|
+
"slot",
|
|
115
|
+
"part",
|
|
116
|
+
"disabled",
|
|
117
|
+
"open",
|
|
118
|
+
"width",
|
|
119
|
+
"height",
|
|
120
|
+
"align",
|
|
121
|
+
"valign",
|
|
122
|
+
"bgcolor",
|
|
123
|
+
"background",
|
|
124
|
+
"border",
|
|
125
|
+
"cellpadding",
|
|
126
|
+
"cellspacing",
|
|
127
|
+
"nowrap",
|
|
128
|
+
"colspan",
|
|
129
|
+
"rowspan",
|
|
130
|
+
"span"
|
|
131
|
+
]);
|
|
132
|
+
/** Only on some elements. */
|
|
133
|
+
var DRAWN_ON = {
|
|
134
|
+
a: ["href"],
|
|
135
|
+
area: ["href"],
|
|
136
|
+
img: [
|
|
137
|
+
"src",
|
|
138
|
+
"srcset",
|
|
139
|
+
"sizes",
|
|
140
|
+
"alt"
|
|
141
|
+
],
|
|
142
|
+
link: [
|
|
143
|
+
"rel",
|
|
144
|
+
"href",
|
|
145
|
+
"media"
|
|
146
|
+
],
|
|
147
|
+
style: ["media"],
|
|
148
|
+
slot: ["name"],
|
|
149
|
+
ol: [
|
|
150
|
+
"start",
|
|
151
|
+
"reversed",
|
|
152
|
+
"type"
|
|
153
|
+
],
|
|
154
|
+
ul: ["type"],
|
|
155
|
+
li: ["type", "value"],
|
|
156
|
+
button: ["type"],
|
|
157
|
+
meter: [
|
|
158
|
+
"value",
|
|
159
|
+
"min",
|
|
160
|
+
"max",
|
|
161
|
+
"low",
|
|
162
|
+
"high",
|
|
163
|
+
"optimum"
|
|
164
|
+
],
|
|
165
|
+
progress: ["value", "max"],
|
|
166
|
+
input: [
|
|
167
|
+
"type",
|
|
168
|
+
"placeholder",
|
|
169
|
+
"size",
|
|
170
|
+
"readonly",
|
|
171
|
+
"required",
|
|
172
|
+
"value"
|
|
173
|
+
],
|
|
174
|
+
textarea: [
|
|
175
|
+
"placeholder",
|
|
176
|
+
"rows",
|
|
177
|
+
"cols",
|
|
178
|
+
"readonly",
|
|
179
|
+
"required"
|
|
180
|
+
],
|
|
181
|
+
select: [
|
|
182
|
+
"size",
|
|
183
|
+
"multiple",
|
|
184
|
+
"required"
|
|
185
|
+
]
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* UI state that component libraries style with (Radix, shadcn, Headless
|
|
189
|
+
* UI: open or closed, checked, the side a popover is on, the theme), kept
|
|
190
|
+
* only while the value is a short keyword.
|
|
191
|
+
*/
|
|
192
|
+
var STATE = /* @__PURE__ */ new Set([
|
|
193
|
+
"data-state",
|
|
194
|
+
"data-side",
|
|
195
|
+
"data-align",
|
|
196
|
+
"data-orientation",
|
|
197
|
+
"data-disabled",
|
|
198
|
+
"data-highlighted",
|
|
199
|
+
"data-selected",
|
|
200
|
+
"data-active",
|
|
201
|
+
"data-open",
|
|
202
|
+
"data-checked",
|
|
203
|
+
"data-slot",
|
|
204
|
+
"data-variant",
|
|
205
|
+
"data-size",
|
|
206
|
+
"data-theme",
|
|
207
|
+
"aria-expanded",
|
|
208
|
+
"aria-selected",
|
|
209
|
+
"aria-checked",
|
|
210
|
+
"aria-pressed",
|
|
211
|
+
"aria-current",
|
|
212
|
+
"aria-disabled",
|
|
213
|
+
"aria-hidden",
|
|
214
|
+
"aria-invalid",
|
|
215
|
+
"aria-busy",
|
|
216
|
+
"aria-orientation",
|
|
217
|
+
"aria-sort"
|
|
218
|
+
]);
|
|
219
|
+
var KEYWORD = /^[\w-]{0,32}$/;
|
|
220
|
+
/** SVG geometry and presentation (lower case; SVG keeps its case). */
|
|
221
|
+
var SVG_DRAWN = /* @__PURE__ */ new Set([
|
|
222
|
+
"href",
|
|
223
|
+
"xlink:href",
|
|
224
|
+
"viewbox",
|
|
225
|
+
"preserveaspectratio",
|
|
226
|
+
"d",
|
|
227
|
+
"points",
|
|
228
|
+
"pathlength",
|
|
229
|
+
"x",
|
|
230
|
+
"y",
|
|
231
|
+
"x1",
|
|
232
|
+
"y1",
|
|
233
|
+
"x2",
|
|
234
|
+
"y2",
|
|
235
|
+
"cx",
|
|
236
|
+
"cy",
|
|
237
|
+
"r",
|
|
238
|
+
"rx",
|
|
239
|
+
"ry",
|
|
240
|
+
"dx",
|
|
241
|
+
"dy",
|
|
242
|
+
"fx",
|
|
243
|
+
"fy",
|
|
244
|
+
"fr",
|
|
245
|
+
"rotate",
|
|
246
|
+
"transform",
|
|
247
|
+
"fill",
|
|
248
|
+
"fill-opacity",
|
|
249
|
+
"fill-rule",
|
|
250
|
+
"stroke",
|
|
251
|
+
"stroke-width",
|
|
252
|
+
"stroke-linecap",
|
|
253
|
+
"stroke-linejoin",
|
|
254
|
+
"stroke-dasharray",
|
|
255
|
+
"stroke-dashoffset",
|
|
256
|
+
"stroke-miterlimit",
|
|
257
|
+
"stroke-opacity",
|
|
258
|
+
"opacity",
|
|
259
|
+
"color",
|
|
260
|
+
"display",
|
|
261
|
+
"visibility",
|
|
262
|
+
"overflow",
|
|
263
|
+
"vector-effect",
|
|
264
|
+
"paint-order",
|
|
265
|
+
"shape-rendering",
|
|
266
|
+
"clip-path",
|
|
267
|
+
"clip-rule",
|
|
268
|
+
"clippathunits",
|
|
269
|
+
"mask",
|
|
270
|
+
"maskunits",
|
|
271
|
+
"maskcontentunits",
|
|
272
|
+
"filter",
|
|
273
|
+
"filterunits",
|
|
274
|
+
"primitiveunits",
|
|
275
|
+
"offset",
|
|
276
|
+
"stop-color",
|
|
277
|
+
"stop-opacity",
|
|
278
|
+
"gradientunits",
|
|
279
|
+
"gradienttransform",
|
|
280
|
+
"spreadmethod",
|
|
281
|
+
"patternunits",
|
|
282
|
+
"patterncontentunits",
|
|
283
|
+
"patterntransform",
|
|
284
|
+
"markerwidth",
|
|
285
|
+
"markerheight",
|
|
286
|
+
"markerunits",
|
|
287
|
+
"refx",
|
|
288
|
+
"refy",
|
|
289
|
+
"orient",
|
|
290
|
+
"marker-start",
|
|
291
|
+
"marker-mid",
|
|
292
|
+
"marker-end",
|
|
293
|
+
"text-anchor",
|
|
294
|
+
"dominant-baseline",
|
|
295
|
+
"alignment-baseline",
|
|
296
|
+
"baseline-shift",
|
|
297
|
+
"font-family",
|
|
298
|
+
"font-size",
|
|
299
|
+
"font-weight",
|
|
300
|
+
"font-style",
|
|
301
|
+
"letter-spacing",
|
|
302
|
+
"word-spacing",
|
|
303
|
+
"text-decoration",
|
|
304
|
+
"textlength",
|
|
305
|
+
"lengthadjust",
|
|
306
|
+
"startoffset",
|
|
307
|
+
"stddeviation",
|
|
308
|
+
"in",
|
|
309
|
+
"in2",
|
|
310
|
+
"result",
|
|
311
|
+
"values",
|
|
312
|
+
"type",
|
|
313
|
+
"mode",
|
|
314
|
+
"operator",
|
|
315
|
+
"flood-color",
|
|
316
|
+
"flood-opacity",
|
|
317
|
+
"lighting-color",
|
|
318
|
+
"color-interpolation-filters",
|
|
319
|
+
"scale"
|
|
320
|
+
]);
|
|
321
|
+
var SVG_NS$1 = "http://www.w3.org/2000/svg";
|
|
322
|
+
/** Inputs whose `value` is the button's label, not something typed. */
|
|
323
|
+
var LABEL_INPUTS = /* @__PURE__ */ new Set([
|
|
324
|
+
"submit",
|
|
325
|
+
"button",
|
|
326
|
+
"reset"
|
|
327
|
+
]);
|
|
328
|
+
/** Whether the copy may have this attribute of `element` at all. */
|
|
329
|
+
function copiesAttribute(element, name) {
|
|
330
|
+
const lower = name.toLowerCase();
|
|
331
|
+
if (DRAWN.has(lower) || STATE.has(lower)) return true;
|
|
332
|
+
if (element.namespaceURI === SVG_NS$1) return SVG_DRAWN.has(lower);
|
|
333
|
+
return DRAWN_ON[element.localName.toLowerCase()]?.includes(lower) ?? false;
|
|
334
|
+
}
|
|
335
|
+
var ATTRIBUTE_NAME = /^[a-zA-Z_:][-a-zA-Z0-9_:.]*$/;
|
|
336
|
+
var EDITABLE = "[contenteditable]:not([contenteditable=\"false\"])";
|
|
337
|
+
/** Elements whose `display: none` is how they work, not concealment. */
|
|
338
|
+
var NOT_BOXES = /* @__PURE__ */ new Set([
|
|
339
|
+
"html",
|
|
340
|
+
"head",
|
|
341
|
+
"title",
|
|
342
|
+
"style",
|
|
343
|
+
"link"
|
|
344
|
+
]);
|
|
345
|
+
var SVG_RESOURCES = /* @__PURE__ */ new Set([
|
|
346
|
+
"defs",
|
|
347
|
+
"symbol",
|
|
348
|
+
"lineargradient",
|
|
349
|
+
"radialgradient",
|
|
350
|
+
"stop",
|
|
351
|
+
"pattern",
|
|
352
|
+
"clippath",
|
|
353
|
+
"mask",
|
|
354
|
+
"marker",
|
|
355
|
+
"filter"
|
|
356
|
+
]);
|
|
357
|
+
/** Whether an element is inside, or is, an editable region. */
|
|
358
|
+
function isEditable(element) {
|
|
359
|
+
if (element.isContentEditable === true) return true;
|
|
360
|
+
const root = element.closest("[contenteditable]");
|
|
361
|
+
return Boolean(root && root.getAttribute("contenteditable") !== "false");
|
|
362
|
+
}
|
|
363
|
+
/** The resolved label of a field: its `<label>`s and `aria-labelledby`. */
|
|
364
|
+
function fieldLabel(element) {
|
|
365
|
+
const parts = [];
|
|
366
|
+
const labels = element.labels;
|
|
367
|
+
if (labels) for (const label of labels) parts.push(label.textContent ?? "");
|
|
368
|
+
for (const id of (element.getAttribute("aria-labelledby") ?? "").split(/\s+/)) if (id) parts.push(element.ownerDocument.getElementById(id)?.textContent ?? "");
|
|
369
|
+
return parts.join(" ").replace(/\s+/g, " ").trim();
|
|
370
|
+
}
|
|
371
|
+
/** How an element appears in the copy. */
|
|
372
|
+
function maskKind(element, privateSelector, skip) {
|
|
373
|
+
const tag = element.localName.toLowerCase();
|
|
374
|
+
if (skip && element === skip) return "drop";
|
|
375
|
+
if (DROPPED.has(tag)) return "drop";
|
|
376
|
+
if (tag === "input" && element.getAttribute("type")?.toLowerCase() === "hidden") return "drop";
|
|
377
|
+
if (tag === "link") {
|
|
378
|
+
const rel = (element.getAttribute("rel") ?? "").toLowerCase().split(/\s+/);
|
|
379
|
+
return rel.includes("stylesheet") && !rel.includes("alternate") ? "none" : "drop";
|
|
380
|
+
}
|
|
381
|
+
try {
|
|
382
|
+
if (element.matches(privateSelector)) return "private";
|
|
383
|
+
} catch {
|
|
384
|
+
if (element.matches("[data-luma-private]")) return "private";
|
|
385
|
+
}
|
|
386
|
+
if (FRAMES.has(tag)) return "frame";
|
|
387
|
+
if (MEDIA.has(tag)) return "media";
|
|
388
|
+
if (FIELDS.has(tag)) return "field";
|
|
389
|
+
if (element.getAttribute("contenteditable") !== null && isEditable(element)) return "private";
|
|
390
|
+
return "none";
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Whether an element is drawn: "shown", "box" (an empty box of its size:
|
|
394
|
+
* `visibility: hidden` or `collapse`, `content-visibility: hidden`) or
|
|
395
|
+
* "hidden" (no box at all: `display: none`, the `hidden` attribute, a
|
|
396
|
+
* closed dialog, a closed details element's content, a light DOM child no
|
|
397
|
+
* slot shows). The copy has the text and attributes of shown elements only.
|
|
398
|
+
*
|
|
399
|
+
* It is decided on the element itself, as the recorder copies from the top
|
|
400
|
+
* down (an element below a box or a hidden one is not copied at all):
|
|
401
|
+
* `visibility` is inherited, so the element's own computed value tells, and
|
|
402
|
+
* `checkVisibility()`, where the browser has it, also knows about a
|
|
403
|
+
* `display: none` above. Transparent (`opacity: 0`), clipped and off-screen
|
|
404
|
+
* content is drawn, so it is copied.
|
|
405
|
+
*/
|
|
406
|
+
function rendering(element) {
|
|
407
|
+
const tag = element.localName.toLowerCase();
|
|
408
|
+
if (NOT_BOXES.has(tag)) return "shown";
|
|
409
|
+
if (element.hasAttribute("hidden")) return "hidden";
|
|
410
|
+
const svg = element.namespaceURI === SVG_NS$1;
|
|
411
|
+
if (svg && (SVG_RESOURCES.has(tag) || tag.startsWith("fe"))) return "shown";
|
|
412
|
+
let style;
|
|
413
|
+
try {
|
|
414
|
+
style = element.ownerDocument.defaultView?.getComputedStyle(element);
|
|
415
|
+
} catch {}
|
|
416
|
+
const display = style?.display;
|
|
417
|
+
if (display === "none") return "hidden";
|
|
418
|
+
const check = element.checkVisibility;
|
|
419
|
+
const inSvg = svg && element.parentElement?.namespaceURI === SVG_NS$1;
|
|
420
|
+
if (typeof check === "function" && display !== "contents" && !inSvg && !check.call(element)) return "hidden";
|
|
421
|
+
const visibility = style?.visibility;
|
|
422
|
+
if (visibility === "hidden" || visibility === "collapse") return "box";
|
|
423
|
+
if (style?.getPropertyValue("content-visibility") === "hidden") return "box";
|
|
424
|
+
return "shown";
|
|
425
|
+
}
|
|
426
|
+
/** An absolute http(s) URL, or null for anything else. */
|
|
427
|
+
function webUrl(value, base) {
|
|
428
|
+
const trimmed = value.trim();
|
|
429
|
+
if (!trimmed) return null;
|
|
430
|
+
try {
|
|
431
|
+
const url = new URL(trimmed, base);
|
|
432
|
+
if (url.protocol === "http:" || url.protocol === "https:") return url.href;
|
|
433
|
+
if (url.protocol === "data:" && /^data:image\/(png|gif|jpe?g|webp|avif|svg\+xml)[;,]/i.test(trimmed) && trimmed.length <= COBROWSE_LIMITS.attributeChars) return trimmed;
|
|
434
|
+
} catch {}
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
/** A link's address without query and fragment (they can carry tokens). */
|
|
438
|
+
function linkUrl(value, base) {
|
|
439
|
+
const url = webUrl(value, base);
|
|
440
|
+
if (!url || url.startsWith("data:")) return null;
|
|
441
|
+
const parsed = new URL(url);
|
|
442
|
+
return parsed.origin + parsed.pathname;
|
|
443
|
+
}
|
|
444
|
+
/** `srcset` with every candidate made absolute; unsafe candidates dropped. */
|
|
445
|
+
function srcset(value, base) {
|
|
446
|
+
return value.split(",").map((candidate) => {
|
|
447
|
+
const [url, ...descriptor] = candidate.trim().split(/\s+/);
|
|
448
|
+
const absolute = url ? webUrl(url, base) : null;
|
|
449
|
+
return absolute && !absolute.startsWith("data:") ? [absolute, ...descriptor].join(" ") : null;
|
|
450
|
+
}).filter(Boolean).join(", ");
|
|
451
|
+
}
|
|
452
|
+
var CSS_URL = /url\(\s*(['"]?)([^'")]*)\1\s*\)/gi;
|
|
453
|
+
var CSS_IMPORT = /@import\s+(['"])([^'"]*)\1/gi;
|
|
454
|
+
/**
|
|
455
|
+
* CSS with every `url()` and `@import` made absolute against `base`; a URL
|
|
456
|
+
* that is not a web or image address becomes `url()` (nothing).
|
|
457
|
+
*/
|
|
458
|
+
function absoluteCss(css, base) {
|
|
459
|
+
return css.replace(CSS_URL, (_match, _quote, url) => {
|
|
460
|
+
if (url.startsWith("#")) return `url("${url}")`;
|
|
461
|
+
const absolute = webUrl(url, base);
|
|
462
|
+
return absolute ? `url("${absolute.replace(/"/g, "%22")}")` : "url()";
|
|
463
|
+
}).replace(CSS_IMPORT, (_match, _quote, url) => {
|
|
464
|
+
const absolute = webUrl(url, base);
|
|
465
|
+
return absolute ? `@import "${absolute.replace(/"/g, "%22")}"` : "";
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* The attributes the copy may have (the allowlist above): never an event
|
|
470
|
+
* handler, a value but a drawn one, a non-web URL; links without their
|
|
471
|
+
* query.
|
|
472
|
+
*/
|
|
473
|
+
function copyAttributes(element, base) {
|
|
474
|
+
const tag = element.localName.toLowerCase();
|
|
475
|
+
const type = (element.getAttribute("type") ?? "").toLowerCase();
|
|
476
|
+
const attributes = {};
|
|
477
|
+
for (const attribute of element.attributes) {
|
|
478
|
+
const name = attribute.name.toLowerCase();
|
|
479
|
+
let value = attribute.value;
|
|
480
|
+
if (!ATTRIBUTE_NAME.test(attribute.name)) continue;
|
|
481
|
+
if (!copiesAttribute(element, name)) continue;
|
|
482
|
+
if (value.length > COBROWSE_LIMITS.attributeChars) continue;
|
|
483
|
+
if ((STATE.has(name) || name === "role") && !KEYWORD.test(value.trim())) continue;
|
|
484
|
+
if (name === "value" && tag === "input" && !LABEL_INPUTS.has(type)) continue;
|
|
485
|
+
if (name === "style") value = absoluteCss(value, base);
|
|
486
|
+
else if (name === "srcset") {
|
|
487
|
+
value = srcset(value, base);
|
|
488
|
+
if (!value) continue;
|
|
489
|
+
} else if (URL_ATTRIBUTES.has(name)) {
|
|
490
|
+
if (value.trim().startsWith("#")) {
|
|
491
|
+
attributes[attribute.name] = value.trim();
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
if (tag === "use") continue;
|
|
495
|
+
const url = (tag === "a" || tag === "area") && name.endsWith("href") ? linkUrl(value, base) : webUrl(value, base);
|
|
496
|
+
if (!url) continue;
|
|
497
|
+
value = url;
|
|
498
|
+
}
|
|
499
|
+
attributes[attribute.name] = value;
|
|
500
|
+
}
|
|
501
|
+
return attributes;
|
|
502
|
+
}
|
|
503
|
+
/** Attributes a field keeps: how it looks, never what it holds. */
|
|
504
|
+
var FIELD_ATTRIBUTES = [
|
|
505
|
+
"class",
|
|
506
|
+
"style",
|
|
507
|
+
"type",
|
|
508
|
+
"placeholder",
|
|
509
|
+
"rows",
|
|
510
|
+
"cols",
|
|
511
|
+
"size",
|
|
512
|
+
"disabled",
|
|
513
|
+
"readonly",
|
|
514
|
+
"dir",
|
|
515
|
+
"lang",
|
|
516
|
+
"role",
|
|
517
|
+
"id",
|
|
518
|
+
"multiple",
|
|
519
|
+
"required"
|
|
520
|
+
];
|
|
521
|
+
/**
|
|
522
|
+
* A field's attributes. A password, payment or one-time-code field keeps
|
|
523
|
+
* only its look (class, style, a neutral type), not even a placeholder.
|
|
524
|
+
*/
|
|
525
|
+
function fieldAttributes(element, base) {
|
|
526
|
+
const sensitive = isSensitiveField(element, fieldLabel(element));
|
|
527
|
+
const all = copyAttributes(element, base);
|
|
528
|
+
const kept = {};
|
|
529
|
+
for (const name of sensitive ? [
|
|
530
|
+
"class",
|
|
531
|
+
"style",
|
|
532
|
+
"disabled"
|
|
533
|
+
] : FIELD_ATTRIBUTES) if (all[name] !== void 0) kept[name] = all[name];
|
|
534
|
+
const type = (element.getAttribute("type") ?? "").toLowerCase();
|
|
535
|
+
if (element.localName === "input") kept.type = sensitive ? type === "password" ? "password" : "text" : BUTTON_INPUTS.has(type) || !type ? type || "text" : type;
|
|
536
|
+
if (!sensitive && all.value !== void 0) kept.value = all.value;
|
|
537
|
+
return {
|
|
538
|
+
attributes: kept,
|
|
539
|
+
sensitive
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
/** A masked region: an empty box of its size, with its look. */
|
|
543
|
+
function placeholderAttributes(element, base) {
|
|
544
|
+
const all = copyAttributes(element, base);
|
|
545
|
+
const kept = {};
|
|
546
|
+
for (const name of [
|
|
547
|
+
"class",
|
|
548
|
+
"style",
|
|
549
|
+
"dir",
|
|
550
|
+
"id"
|
|
551
|
+
]) if (all[name] !== void 0) kept[name] = all[name];
|
|
552
|
+
const rect = element.getBoundingClientRect();
|
|
553
|
+
if (rect.width > 0 && rect.height > 0) kept.style = `${kept.style ? `${kept.style};` : ""}width:${Math.round(rect.width)}px;height:${Math.round(rect.height)}px`;
|
|
554
|
+
return kept;
|
|
555
|
+
}
|
|
556
|
+
// src/cobrowse/recorder.ts
|
|
557
|
+
/**
|
|
558
|
+
* The co-browsing recorder (phase 14): a copy of the page's structure and
|
|
559
|
+
* text, sent as a snapshot and then as batches of changes, with every
|
|
560
|
+
* masking rule of ./mask.ts applied before anything leaves the browser.
|
|
561
|
+
* It never reads a field's value, never listens to typing, and never takes
|
|
562
|
+
* a picture of the page.
|
|
563
|
+
*
|
|
564
|
+
* A changed child list is sent whole: children the copy has are named by
|
|
565
|
+
* id (`{ r }`), new ones are copied, and the rest are gone. That covers
|
|
566
|
+
* inserts, removals and moves in any order without positions.
|
|
567
|
+
*
|
|
568
|
+
* Only what the page draws is copied (`rendering` in ./mask.ts): an element
|
|
569
|
+
* that is not rendered is an empty, hidden element in the copy, one with
|
|
570
|
+
* `visibility: hidden` an empty box of its size. Whether an element is drawn
|
|
571
|
+
* can change without a change to it, so every batch checks again, against
|
|
572
|
+
* the copy, the elements at and under each change (an attribute, a child
|
|
573
|
+
* list, a text's element, a popover or details element that toggled), and
|
|
574
|
+
* the whole page when a style sheet or the viewport changed (a media
|
|
575
|
+
* query); an element whose answer changed is copied again.
|
|
576
|
+
*
|
|
577
|
+
* Not seen until one of those happens: what `:hover`, `:focus`, `:checked`,
|
|
578
|
+
* `:defined` or `:has()` rules, animations, transitions or a style sheet
|
|
579
|
+
* `<link>` that loads late show or hide. An element they show stays empty in the copy
|
|
580
|
+
* until then; one they hide keeps what was sent while it was drawn, and a
|
|
581
|
+
* change inside it is checked at its own element first (where the browser
|
|
582
|
+
* has `checkVisibility()`, that sees a `display: none` above too).
|
|
583
|
+
*/
|
|
584
|
+
var HEAD_KEPT = /* @__PURE__ */ new Set([
|
|
585
|
+
"title",
|
|
586
|
+
"style",
|
|
587
|
+
"link"
|
|
588
|
+
]);
|
|
589
|
+
/** A copied element that is not drawn: no text, no attributes of its own. */
|
|
590
|
+
var HIDDEN = { style: "display:none!important" };
|
|
591
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
592
|
+
var encoder = new TextEncoder();
|
|
593
|
+
/** A frame's size on the wire: its JSON in UTF-8 (Persian text is 2 bytes a letter). */
|
|
594
|
+
function frameBytes(frame) {
|
|
595
|
+
return encoder.encode(JSON.stringify(frame)).length;
|
|
596
|
+
}
|
|
597
|
+
var Recorder = class {
|
|
598
|
+
/** This page load; support's cursor names its node ids with it. */
|
|
599
|
+
page = crypto.randomUUID().replaceAll("-", "").slice(0, 16);
|
|
600
|
+
ids = /* @__PURE__ */ new WeakMap();
|
|
601
|
+
nodes = /* @__PURE__ */ new Map();
|
|
602
|
+
kinds = /* @__PURE__ */ new WeakMap();
|
|
603
|
+
styles = /* @__PURE__ */ new Map();
|
|
604
|
+
/** Open shadow roots copied, so their private regions are checked too. */
|
|
605
|
+
shadows = /* @__PURE__ */ new Set();
|
|
606
|
+
next = 1;
|
|
607
|
+
count = 0;
|
|
608
|
+
bytes = 0;
|
|
609
|
+
tooBig = false;
|
|
610
|
+
options;
|
|
611
|
+
doc;
|
|
612
|
+
observer = null;
|
|
613
|
+
records = [];
|
|
614
|
+
scrolled = /* @__PURE__ */ new Set();
|
|
615
|
+
timer = null;
|
|
616
|
+
keepAlive = null;
|
|
617
|
+
sending = false;
|
|
618
|
+
needSnapshot = true;
|
|
619
|
+
seq = 0;
|
|
620
|
+
lastSent = 0;
|
|
621
|
+
sentLog = [];
|
|
622
|
+
slow = false;
|
|
623
|
+
stopped = false;
|
|
624
|
+
viewport = {
|
|
625
|
+
w: 0,
|
|
626
|
+
h: 0
|
|
627
|
+
};
|
|
628
|
+
url = "";
|
|
629
|
+
title = "";
|
|
630
|
+
adoptedCount = 0;
|
|
631
|
+
adoptedId = 0;
|
|
632
|
+
/** The constructed sheets' node is in the copy. */
|
|
633
|
+
adoptedSent = false;
|
|
634
|
+
wasRestricted = false;
|
|
635
|
+
/** Sends that failed in a row (the wait before the next one grows). */
|
|
636
|
+
failures = 0;
|
|
637
|
+
/** The widget's areas as last sent (a signature), and their check. */
|
|
638
|
+
areasSent = "";
|
|
639
|
+
areaCheck = null;
|
|
640
|
+
onScroll = (event) => {
|
|
641
|
+
const target = event.target;
|
|
642
|
+
if (target === this.doc || target === this.doc.documentElement) this.scrolled.add(0);
|
|
643
|
+
else if (target instanceof Element) {
|
|
644
|
+
const id = this.ids.get(target);
|
|
645
|
+
if (id) this.scrolled.add(id);
|
|
646
|
+
}
|
|
647
|
+
this.schedule();
|
|
648
|
+
};
|
|
649
|
+
onResize = () => this.schedule();
|
|
650
|
+
/** Popovers and details elements that opened or closed (a popover's
|
|
651
|
+
* opening is no mutation). */
|
|
652
|
+
toggled = /* @__PURE__ */ new Set();
|
|
653
|
+
onToggle = (event) => {
|
|
654
|
+
if (event.target instanceof Element) this.toggled.add(event.target);
|
|
655
|
+
this.schedule();
|
|
656
|
+
};
|
|
657
|
+
constructor(options) {
|
|
658
|
+
this.options = options;
|
|
659
|
+
this.doc = options.document;
|
|
660
|
+
}
|
|
661
|
+
get now() {
|
|
662
|
+
return this.options.now?.() ?? Date.now();
|
|
663
|
+
}
|
|
664
|
+
/** The live node an id names (for support's cursor), or null. */
|
|
665
|
+
node(id) {
|
|
666
|
+
const node = this.nodes.get(id);
|
|
667
|
+
return node?.isConnected ? node : null;
|
|
668
|
+
}
|
|
669
|
+
id(node) {
|
|
670
|
+
let id = this.ids.get(node);
|
|
671
|
+
if (!id) {
|
|
672
|
+
id = this.next++;
|
|
673
|
+
this.ids.set(node, id);
|
|
674
|
+
}
|
|
675
|
+
this.nodes.set(id, node);
|
|
676
|
+
return id;
|
|
677
|
+
}
|
|
678
|
+
known(node) {
|
|
679
|
+
const id = this.ids.get(node);
|
|
680
|
+
return id !== void 0 && this.nodes.get(id) === node;
|
|
681
|
+
}
|
|
682
|
+
forget(node) {
|
|
683
|
+
const id = this.ids.get(node);
|
|
684
|
+
if (id !== void 0) {
|
|
685
|
+
this.nodes.delete(id);
|
|
686
|
+
this.ids.delete(node);
|
|
687
|
+
}
|
|
688
|
+
if (node instanceof Element) {
|
|
689
|
+
this.kinds.delete(node);
|
|
690
|
+
if (node instanceof HTMLStyleElement) this.styles.delete(node);
|
|
691
|
+
const shadow = node.shadowRoot;
|
|
692
|
+
if (shadow) this.forget(shadow);
|
|
693
|
+
}
|
|
694
|
+
for (const child of node.childNodes) this.forget(child);
|
|
695
|
+
}
|
|
696
|
+
/** The copy of `node`, or null when it is not copied at all. */
|
|
697
|
+
serialize(node, inSvg = false) {
|
|
698
|
+
if (this.tooBig) return null;
|
|
699
|
+
if (this.known(node)) this.forget(node);
|
|
700
|
+
if (node.nodeType === 3) {
|
|
701
|
+
if (++this.count > COBROWSE_LIMITS.maxNodes) {
|
|
702
|
+
this.tooBig = true;
|
|
703
|
+
return null;
|
|
704
|
+
}
|
|
705
|
+
const x = node.data.slice(0, COBROWSE_LIMITS.textChars);
|
|
706
|
+
this.bytes += x.length + 14;
|
|
707
|
+
return {
|
|
708
|
+
i: this.id(node),
|
|
709
|
+
x
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
if (node.nodeType !== 1) return null;
|
|
713
|
+
const element = node;
|
|
714
|
+
const tag = element.localName.toLowerCase();
|
|
715
|
+
const parent = element.parentNode;
|
|
716
|
+
if (parent instanceof Element && parent.localName === "head" && !HEAD_KEPT.has(tag)) return null;
|
|
717
|
+
const kind = this.kindNow(element);
|
|
718
|
+
if (kind === "drop") return null;
|
|
719
|
+
if (++this.count > COBROWSE_LIMITS.maxNodes) {
|
|
720
|
+
this.tooBig = true;
|
|
721
|
+
return null;
|
|
722
|
+
}
|
|
723
|
+
this.kinds.set(element, kind);
|
|
724
|
+
const svg = inSvg || element.namespaceURI === SVG_NS;
|
|
725
|
+
const base = this.doc.baseURI;
|
|
726
|
+
const out = {
|
|
727
|
+
i: this.id(element),
|
|
728
|
+
t: tag
|
|
729
|
+
};
|
|
730
|
+
if (svg && !inSvg) out.svg = 1;
|
|
731
|
+
if (kind !== "none") {
|
|
732
|
+
if (kind === "hidden") out.a = { ...HIDDEN };
|
|
733
|
+
else if (kind === "box") out.a = placeholderAttributes(element, base);
|
|
734
|
+
else {
|
|
735
|
+
out.p = kind;
|
|
736
|
+
out.a = kind === "field" ? fieldAttributes(element, base).attributes : placeholderAttributes(element, base);
|
|
737
|
+
}
|
|
738
|
+
this.bytes += JSON.stringify(out).length;
|
|
739
|
+
return out;
|
|
740
|
+
}
|
|
741
|
+
const attributes = copyAttributes(element, base);
|
|
742
|
+
if (Object.keys(attributes).length) out.a = attributes;
|
|
743
|
+
this.bytes += JSON.stringify(out).length;
|
|
744
|
+
if (tag === "style") {
|
|
745
|
+
out.css = this.styleText(element);
|
|
746
|
+
this.bytes += out.css.length;
|
|
747
|
+
return out;
|
|
748
|
+
}
|
|
749
|
+
const children = this.children(element, svg && tag !== "foreignobject");
|
|
750
|
+
if (tag === "head") {
|
|
751
|
+
const adopted = this.adoptedNode();
|
|
752
|
+
if (adopted) children.push(adopted);
|
|
753
|
+
}
|
|
754
|
+
if (children.length) out.c = children;
|
|
755
|
+
const shadow = element.shadowRoot;
|
|
756
|
+
if (shadow) {
|
|
757
|
+
out.si = this.id(shadow);
|
|
758
|
+
out.sh = this.children(shadow, false);
|
|
759
|
+
this.shadows.add(shadow);
|
|
760
|
+
this.observe(shadow);
|
|
761
|
+
}
|
|
762
|
+
if (element.scrollTop || element.scrollLeft) this.scrolled.add(out.i);
|
|
763
|
+
return out;
|
|
764
|
+
}
|
|
765
|
+
children(parent, inSvg) {
|
|
766
|
+
const list = [];
|
|
767
|
+
for (const child of parent.childNodes) {
|
|
768
|
+
const copy = this.serialize(child, inSvg);
|
|
769
|
+
if (copy) list.push(copy);
|
|
770
|
+
}
|
|
771
|
+
return list;
|
|
772
|
+
}
|
|
773
|
+
/** A style element's rules as the page has them (CSS-in-JS included). */
|
|
774
|
+
styleText(style) {
|
|
775
|
+
let css = "";
|
|
776
|
+
try {
|
|
777
|
+
const rules = style.sheet?.cssRules;
|
|
778
|
+
if (rules?.length) css = Array.from(rules, (rule) => rule.cssText).join("\n");
|
|
779
|
+
this.styles.set(style, rules?.length ?? 0);
|
|
780
|
+
} catch {
|
|
781
|
+
this.styles.set(style, 0);
|
|
782
|
+
}
|
|
783
|
+
if (!css) css = style.textContent ?? "";
|
|
784
|
+
return absoluteCss(css, this.doc.baseURI).slice(0, COBROWSE_LIMITS.cssChars);
|
|
785
|
+
}
|
|
786
|
+
adoptedSheets() {
|
|
787
|
+
return this.doc.adoptedStyleSheets ?? [];
|
|
788
|
+
}
|
|
789
|
+
adoptedRuleCount() {
|
|
790
|
+
let count = 0;
|
|
791
|
+
for (const sheet of this.adoptedSheets()) try {
|
|
792
|
+
count += sheet.cssRules.length;
|
|
793
|
+
} catch {}
|
|
794
|
+
return count;
|
|
795
|
+
}
|
|
796
|
+
/** The document's constructed style sheets, as one style element. */
|
|
797
|
+
adoptedNode() {
|
|
798
|
+
const sheets = this.adoptedSheets();
|
|
799
|
+
this.adoptedCount = this.adoptedRuleCount();
|
|
800
|
+
if (!sheets.length) return null;
|
|
801
|
+
let css = "";
|
|
802
|
+
for (const sheet of sheets) try {
|
|
803
|
+
css += Array.from(sheet.cssRules, (rule) => rule.cssText).join("\n");
|
|
804
|
+
} catch {}
|
|
805
|
+
this.adoptedId ||= this.next++;
|
|
806
|
+
this.adoptedSent = true;
|
|
807
|
+
return {
|
|
808
|
+
i: this.adoptedId,
|
|
809
|
+
t: "style",
|
|
810
|
+
css: absoluteCss(css, this.doc.baseURI).slice(0, COBROWSE_LIMITS.cssChars)
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
observe(target) {
|
|
814
|
+
this.observer?.observe(target, {
|
|
815
|
+
subtree: true,
|
|
816
|
+
childList: true,
|
|
817
|
+
attributes: true,
|
|
818
|
+
characterData: true
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
scrollOf(id) {
|
|
822
|
+
if (id === 0) {
|
|
823
|
+
const view = this.doc.defaultView;
|
|
824
|
+
return [
|
|
825
|
+
0,
|
|
826
|
+
Math.round(view?.scrollX ?? 0),
|
|
827
|
+
Math.round(view?.scrollY ?? 0)
|
|
828
|
+
];
|
|
829
|
+
}
|
|
830
|
+
const node = this.nodes.get(id);
|
|
831
|
+
if (!(node instanceof Element) || !node.isConnected) return null;
|
|
832
|
+
return [
|
|
833
|
+
id,
|
|
834
|
+
Math.round(node.scrollLeft),
|
|
835
|
+
Math.round(node.scrollTop)
|
|
836
|
+
];
|
|
837
|
+
}
|
|
838
|
+
readViewport() {
|
|
839
|
+
const view = this.doc.defaultView;
|
|
840
|
+
return {
|
|
841
|
+
w: Math.max(1, Math.round(view?.innerWidth ?? 1)),
|
|
842
|
+
h: Math.max(1, Math.round(view?.innerHeight ?? 1))
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
pageUrl() {
|
|
846
|
+
const location = this.doc.location;
|
|
847
|
+
return location ? location.origin + location.pathname : "";
|
|
848
|
+
}
|
|
849
|
+
/** The whole page: a snapshot frame, or null when it is too big. */
|
|
850
|
+
snapshot() {
|
|
851
|
+
this.count = 0;
|
|
852
|
+
this.bytes = 0;
|
|
853
|
+
this.tooBig = false;
|
|
854
|
+
this.ids = /* @__PURE__ */ new WeakMap();
|
|
855
|
+
this.kinds = /* @__PURE__ */ new WeakMap();
|
|
856
|
+
this.nodes.clear();
|
|
857
|
+
this.styles.clear();
|
|
858
|
+
this.shadows.clear();
|
|
859
|
+
this.scrolled = /* @__PURE__ */ new Set([0]);
|
|
860
|
+
this.adoptedId = 0;
|
|
861
|
+
this.adoptedSent = false;
|
|
862
|
+
this.observer?.disconnect();
|
|
863
|
+
this.observer = new MutationObserver((records) => {
|
|
864
|
+
this.records.push(...records);
|
|
865
|
+
this.schedule();
|
|
866
|
+
});
|
|
867
|
+
this.observe(this.doc);
|
|
868
|
+
const root = this.serialize(this.doc.documentElement);
|
|
869
|
+
this.records = [];
|
|
870
|
+
if (!root || this.tooBig || this.bytes > COBROWSE_LIMITS.snapshotBytes) return null;
|
|
871
|
+
this.viewport = this.readViewport();
|
|
872
|
+
this.url = this.pageUrl();
|
|
873
|
+
this.title = this.doc.title.slice(0, 300);
|
|
874
|
+
const scrolls = [...this.scrolled].map((id) => this.scrollOf(id)).filter((item) => item !== null);
|
|
875
|
+
this.scrolled.clear();
|
|
876
|
+
const frame = {
|
|
877
|
+
type: "snapshot",
|
|
878
|
+
page: this.page,
|
|
879
|
+
url: this.url,
|
|
880
|
+
title: this.title,
|
|
881
|
+
origin: this.doc.location?.origin ?? "",
|
|
882
|
+
viewport: this.viewport,
|
|
883
|
+
root,
|
|
884
|
+
scrolls,
|
|
885
|
+
bytes: 0,
|
|
886
|
+
seq: this.seq,
|
|
887
|
+
...this.widgetAreas(true)
|
|
888
|
+
};
|
|
889
|
+
frame.bytes = frameBytes(frame);
|
|
890
|
+
return frame.bytes > COBROWSE_LIMITS.snapshotBytes ? null : frame;
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* The widget's areas for a frame: always in a snapshot, in a change
|
|
894
|
+
* batch only when they moved (the panel opened, closed or resized).
|
|
895
|
+
*/
|
|
896
|
+
widgetAreas(always) {
|
|
897
|
+
if (!this.options.areas) return {};
|
|
898
|
+
const areas = this.options.areas();
|
|
899
|
+
const signature = JSON.stringify(areas);
|
|
900
|
+
if (!always && signature === this.areasSent) return {};
|
|
901
|
+
this.areasSent = signature;
|
|
902
|
+
return { widget: areas };
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Whether a node sits inside something the copy keeps empty, or outside
|
|
906
|
+
* the copy: below an element that was never copied (a script, a dropped
|
|
907
|
+
* head element) a node that was copied elsewhere before is not sent.
|
|
908
|
+
*/
|
|
909
|
+
masked(node) {
|
|
910
|
+
let current = node.parentNode;
|
|
911
|
+
while (current) if (current instanceof Element) {
|
|
912
|
+
if (!this.known(current)) return true;
|
|
913
|
+
const kind = this.kinds.get(current);
|
|
914
|
+
if (kind && kind !== "none") return true;
|
|
915
|
+
const tag = current.localName;
|
|
916
|
+
if (tag === "textarea" || tag === "select") return true;
|
|
917
|
+
current = current.parentNode;
|
|
918
|
+
} else if (current instanceof ShadowRoot) {
|
|
919
|
+
if (!this.known(current)) return true;
|
|
920
|
+
current = current.host;
|
|
921
|
+
} else current = current.parentNode;
|
|
922
|
+
return false;
|
|
923
|
+
}
|
|
924
|
+
/** How an element is copied now: masked, not drawn, or as it is. */
|
|
925
|
+
kindNow(element) {
|
|
926
|
+
const kind = maskKind(element, this.options.privateSelector, this.options.skip);
|
|
927
|
+
if (kind === "drop") return kind;
|
|
928
|
+
const drawn = rendering(element);
|
|
929
|
+
return drawn === "shown" ? kind : drawn;
|
|
930
|
+
}
|
|
931
|
+
/**
|
|
932
|
+
* Copies again every element at or under `roots` whose copy no longer
|
|
933
|
+
* matches the page (it became hidden or shown, private or not), each
|
|
934
|
+
* element checked once a batch.
|
|
935
|
+
*/
|
|
936
|
+
recheck(roots, remask) {
|
|
937
|
+
const seen = /* @__PURE__ */ new Set();
|
|
938
|
+
const stack = [...roots];
|
|
939
|
+
while (stack.length) {
|
|
940
|
+
const node = stack.pop();
|
|
941
|
+
if (seen.has(node) || !this.known(node) || !node.isConnected) continue;
|
|
942
|
+
seen.add(node);
|
|
943
|
+
if (node instanceof Element) {
|
|
944
|
+
const kind = this.kinds.get(node);
|
|
945
|
+
if (kind === void 0) continue;
|
|
946
|
+
if (this.kindNow(node) !== kind) {
|
|
947
|
+
remask(node);
|
|
948
|
+
continue;
|
|
949
|
+
}
|
|
950
|
+
if (kind !== "none") continue;
|
|
951
|
+
const shadow = node.shadowRoot;
|
|
952
|
+
if (shadow) stack.push(shadow);
|
|
953
|
+
}
|
|
954
|
+
for (const child of node.children) stack.push(child);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
/**
|
|
958
|
+
* Every element that must be copied empty now, in the document and in
|
|
959
|
+
* every open shadow root copied (a selector can start to match after a
|
|
960
|
+
* class, an attribute or a child changes, `:has()` included).
|
|
961
|
+
*/
|
|
962
|
+
maskedNow() {
|
|
963
|
+
const editable = `${EDITABLE},[data-luma-private]`;
|
|
964
|
+
const found = [];
|
|
965
|
+
const scopes = [this.doc];
|
|
966
|
+
for (const shadow of this.shadows) if (shadow.host.isConnected) scopes.push(shadow);
|
|
967
|
+
else this.shadows.delete(shadow);
|
|
968
|
+
for (const scope of scopes) try {
|
|
969
|
+
found.push(...scope.querySelectorAll(`${this.options.privateSelector},${editable}`));
|
|
970
|
+
} catch {
|
|
971
|
+
found.push(...scope.querySelectorAll(editable));
|
|
972
|
+
}
|
|
973
|
+
return found;
|
|
974
|
+
}
|
|
975
|
+
/** The changes since the last frame, or null when nothing changed. */
|
|
976
|
+
changes() {
|
|
977
|
+
const records = [...this.records, ...this.observer?.takeRecords() ?? []];
|
|
978
|
+
this.records = [];
|
|
979
|
+
const lists = /* @__PURE__ */ new Set();
|
|
980
|
+
const attrs = /* @__PURE__ */ new Set();
|
|
981
|
+
/** Elements with a change to an attribute the copy has. */
|
|
982
|
+
const copied = /* @__PURE__ */ new Set();
|
|
983
|
+
const texts = /* @__PURE__ */ new Set();
|
|
984
|
+
const css = /* @__PURE__ */ new Set();
|
|
985
|
+
const removed = [];
|
|
986
|
+
for (const record of records) {
|
|
987
|
+
const target = record.target;
|
|
988
|
+
if (record.type === "childList") {
|
|
989
|
+
if (target instanceof HTMLStyleElement) css.add(target);
|
|
990
|
+
else if (target instanceof Element || target instanceof ShadowRoot) lists.add(target);
|
|
991
|
+
removed.push(...record.removedNodes);
|
|
992
|
+
} else if (record.type === "attributes" && target instanceof Element) {
|
|
993
|
+
attrs.add(target);
|
|
994
|
+
if (record.attributeName && copiesAttribute(target, record.attributeName)) copied.add(target);
|
|
995
|
+
} else if (record.type === "characterData") {
|
|
996
|
+
const parent = target.parentNode;
|
|
997
|
+
if (parent instanceof HTMLStyleElement) css.add(parent);
|
|
998
|
+
else if (target instanceof Text) texts.add(target);
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
for (const [style, count] of this.styles) try {
|
|
1002
|
+
if ((style.sheet?.cssRules.length ?? 0) !== count) css.add(style);
|
|
1003
|
+
} catch {}
|
|
1004
|
+
const viewport = this.readViewport();
|
|
1005
|
+
const resized = viewport.w !== this.viewport.w || viewport.h !== this.viewport.h;
|
|
1006
|
+
const remask = (element) => {
|
|
1007
|
+
const parent = element.parentNode;
|
|
1008
|
+
this.forget(element);
|
|
1009
|
+
if (parent instanceof Element || parent instanceof ShadowRoot) lists.add(parent);
|
|
1010
|
+
};
|
|
1011
|
+
const toggled = [...this.toggled];
|
|
1012
|
+
this.toggled.clear();
|
|
1013
|
+
const head = this.doc.head;
|
|
1014
|
+
const restyled = css.size > 0 || resized || head !== null && lists.has(head) || this.adoptedId !== 0 && this.adoptedRuleCount() !== this.adoptedCount;
|
|
1015
|
+
const roots = restyled ? [this.doc.documentElement, ...this.shadows] : [
|
|
1016
|
+
...attrs,
|
|
1017
|
+
...lists,
|
|
1018
|
+
...toggled
|
|
1019
|
+
];
|
|
1020
|
+
if (!restyled) {
|
|
1021
|
+
for (const text of texts) if (text.parentNode instanceof Element) roots.push(text.parentNode);
|
|
1022
|
+
}
|
|
1023
|
+
this.recheck(roots, remask);
|
|
1024
|
+
if (records.length) {
|
|
1025
|
+
for (const element of this.maskedNow()) if (this.known(element) && this.kinds.get(element) === "none") remask(element);
|
|
1026
|
+
}
|
|
1027
|
+
this.count = this.nodes.size;
|
|
1028
|
+
this.bytes = 0;
|
|
1029
|
+
const frame = {
|
|
1030
|
+
type: "changes",
|
|
1031
|
+
page: this.page,
|
|
1032
|
+
seq: this.seq + 1
|
|
1033
|
+
};
|
|
1034
|
+
const listOut = [];
|
|
1035
|
+
for (const parent of lists) {
|
|
1036
|
+
const host = parent instanceof ShadowRoot ? parent.host : parent;
|
|
1037
|
+
if (!this.known(parent) || !host.isConnected || this.masked(host)) continue;
|
|
1038
|
+
if (parent instanceof Element) {
|
|
1039
|
+
const kind = this.kinds.get(parent);
|
|
1040
|
+
if (kind && kind !== "none" || parent.localName === "style") continue;
|
|
1041
|
+
}
|
|
1042
|
+
const inSvg = parent instanceof Element && parent.namespaceURI === SVG_NS && parent.localName.toLowerCase() !== "foreignobject";
|
|
1043
|
+
const children = [];
|
|
1044
|
+
for (const child of parent.childNodes) if (this.known(child)) children.push({ r: this.ids.get(child) });
|
|
1045
|
+
else {
|
|
1046
|
+
const copy = this.serialize(child, inSvg);
|
|
1047
|
+
if (copy) children.push(copy);
|
|
1048
|
+
}
|
|
1049
|
+
if (parent instanceof Element && parent.localName === "head") {
|
|
1050
|
+
if (this.adoptedId && this.adoptedSent) children.push({ r: this.adoptedId });
|
|
1051
|
+
else {
|
|
1052
|
+
const adopted = this.adoptedNode();
|
|
1053
|
+
if (adopted) children.push(adopted);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
listOut.push({
|
|
1057
|
+
i: this.ids.get(parent),
|
|
1058
|
+
c: children
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1061
|
+
if (this.tooBig) return null;
|
|
1062
|
+
for (const node of removed) if (!node.isConnected && this.known(node)) this.forget(node);
|
|
1063
|
+
if (listOut.length) frame.lists = listOut;
|
|
1064
|
+
const base = this.doc.baseURI;
|
|
1065
|
+
const attrOut = [];
|
|
1066
|
+
for (const element of copied) {
|
|
1067
|
+
if (!this.known(element) || !element.isConnected || this.masked(element)) continue;
|
|
1068
|
+
const kind = this.kinds.get(element);
|
|
1069
|
+
if (kind === "hidden") continue;
|
|
1070
|
+
attrOut.push({
|
|
1071
|
+
i: this.ids.get(element),
|
|
1072
|
+
a: kind === "field" ? fieldAttributes(element, base).attributes : kind && kind !== "none" ? placeholderAttributes(element, base) : copyAttributes(element, base)
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
if (attrOut.length) frame.attrs = attrOut;
|
|
1076
|
+
const textOut = [];
|
|
1077
|
+
for (const text of texts) if (this.known(text) && text.isConnected && !this.masked(text)) textOut.push({
|
|
1078
|
+
i: this.ids.get(text),
|
|
1079
|
+
x: text.data.slice(0, COBROWSE_LIMITS.textChars)
|
|
1080
|
+
});
|
|
1081
|
+
if (textOut.length) frame.texts = textOut;
|
|
1082
|
+
const cssOut = [];
|
|
1083
|
+
for (const style of css) if (this.known(style) && style.isConnected && !this.masked(style)) cssOut.push({
|
|
1084
|
+
i: this.ids.get(style),
|
|
1085
|
+
css: this.styleText(style)
|
|
1086
|
+
});
|
|
1087
|
+
if (this.adoptedId && this.adoptedRuleCount() !== this.adoptedCount) {
|
|
1088
|
+
const adopted = this.adoptedNode();
|
|
1089
|
+
if (adopted?.css !== void 0) cssOut.push({
|
|
1090
|
+
i: this.adoptedId,
|
|
1091
|
+
css: adopted.css
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
if (cssOut.length) frame.css = cssOut;
|
|
1095
|
+
const scrolls = [...this.scrolled].map((id) => this.scrollOf(id)).filter((item) => item !== null);
|
|
1096
|
+
this.scrolled.clear();
|
|
1097
|
+
if (scrolls.length) frame.scrolls = scrolls;
|
|
1098
|
+
if (resized) {
|
|
1099
|
+
this.viewport = viewport;
|
|
1100
|
+
frame.viewport = viewport;
|
|
1101
|
+
}
|
|
1102
|
+
const url = this.pageUrl();
|
|
1103
|
+
if (url !== this.url) {
|
|
1104
|
+
this.url = url;
|
|
1105
|
+
frame.url = url;
|
|
1106
|
+
}
|
|
1107
|
+
const title = this.doc.title.slice(0, 300);
|
|
1108
|
+
if (title !== this.title) {
|
|
1109
|
+
this.title = title;
|
|
1110
|
+
frame.title = title;
|
|
1111
|
+
}
|
|
1112
|
+
Object.assign(frame, this.widgetAreas(false));
|
|
1113
|
+
if (Object.keys(frame).length <= 3) return null;
|
|
1114
|
+
this.seq = frame.seq;
|
|
1115
|
+
return frame;
|
|
1116
|
+
}
|
|
1117
|
+
/** Whether something changed that no mutation record reports. */
|
|
1118
|
+
quietChange() {
|
|
1119
|
+
if ((this.options.restricted?.() ?? false) !== this.wasRestricted) return true;
|
|
1120
|
+
const viewport = this.readViewport();
|
|
1121
|
+
if (viewport.w !== this.viewport.w || viewport.h !== this.viewport.h) return true;
|
|
1122
|
+
if (this.pageUrl() !== this.url) return true;
|
|
1123
|
+
if (this.doc.title.slice(0, 300) !== this.title) return true;
|
|
1124
|
+
for (const [style, count] of this.styles) try {
|
|
1125
|
+
if ((style.sheet?.cssRules.length ?? 0) !== count) return true;
|
|
1126
|
+
} catch {}
|
|
1127
|
+
if (this.adoptedId && this.adoptedRuleCount() !== this.adoptedCount) return true;
|
|
1128
|
+
return Boolean(this.options.areas && JSON.stringify(this.options.areas()) !== this.areasSent);
|
|
1129
|
+
}
|
|
1130
|
+
/** Whether the last snapshot or change ran past the node limit. */
|
|
1131
|
+
get overLimit() {
|
|
1132
|
+
return this.tooBig;
|
|
1133
|
+
}
|
|
1134
|
+
/** Starts sending: the snapshot, then changes as they happen. */
|
|
1135
|
+
start() {
|
|
1136
|
+
this.doc.addEventListener("scroll", this.onScroll, {
|
|
1137
|
+
capture: true,
|
|
1138
|
+
passive: true
|
|
1139
|
+
});
|
|
1140
|
+
this.doc.defaultView?.addEventListener("resize", this.onResize);
|
|
1141
|
+
this.doc.addEventListener("toggle", this.onToggle, { capture: true });
|
|
1142
|
+
this.areaCheck = setInterval(() => {
|
|
1143
|
+
if (this.quietChange()) this.schedule();
|
|
1144
|
+
}, 500);
|
|
1145
|
+
this.needSnapshot = true;
|
|
1146
|
+
this.schedule(0);
|
|
1147
|
+
}
|
|
1148
|
+
/** A fresh snapshot goes out next (the owner's view asked for one). */
|
|
1149
|
+
resync() {
|
|
1150
|
+
this.needSnapshot = true;
|
|
1151
|
+
this.schedule(0);
|
|
1152
|
+
}
|
|
1153
|
+
/** Stops observing and sending, for good. */
|
|
1154
|
+
destroy() {
|
|
1155
|
+
this.stopped = true;
|
|
1156
|
+
if (this.timer) clearTimeout(this.timer);
|
|
1157
|
+
if (this.keepAlive) clearTimeout(this.keepAlive);
|
|
1158
|
+
this.timer = null;
|
|
1159
|
+
this.keepAlive = null;
|
|
1160
|
+
if (this.areaCheck) clearInterval(this.areaCheck);
|
|
1161
|
+
this.areaCheck = null;
|
|
1162
|
+
this.observer?.disconnect();
|
|
1163
|
+
this.observer = null;
|
|
1164
|
+
this.records = [];
|
|
1165
|
+
this.doc.removeEventListener("scroll", this.onScroll, { capture: true });
|
|
1166
|
+
this.doc.defaultView?.removeEventListener("resize", this.onResize);
|
|
1167
|
+
this.doc.removeEventListener("toggle", this.onToggle, { capture: true });
|
|
1168
|
+
this.toggled.clear();
|
|
1169
|
+
}
|
|
1170
|
+
/** Bytes sent in the last minute. */
|
|
1171
|
+
minuteBytes() {
|
|
1172
|
+
const since = this.now - 6e4;
|
|
1173
|
+
this.sentLog = this.sentLog.filter(([at]) => at >= since);
|
|
1174
|
+
return this.sentLog.reduce((sum, [, bytes]) => sum + bytes, 0);
|
|
1175
|
+
}
|
|
1176
|
+
schedule(delay) {
|
|
1177
|
+
if (this.stopped || this.timer || this.sending) return;
|
|
1178
|
+
const gap = this.slow ? COBROWSE_LIMITS.slowFlushMs : COBROWSE_LIMITS.flushMs;
|
|
1179
|
+
const wait = delay ?? Math.max(0, gap - (this.now - this.lastSent));
|
|
1180
|
+
this.timer = setTimeout(() => {
|
|
1181
|
+
this.timer = null;
|
|
1182
|
+
this.flush();
|
|
1183
|
+
}, wait);
|
|
1184
|
+
}
|
|
1185
|
+
nextFrame() {
|
|
1186
|
+
const restricted = this.options.restricted?.() ?? false;
|
|
1187
|
+
if (restricted !== this.wasRestricted) {
|
|
1188
|
+
this.wasRestricted = restricted;
|
|
1189
|
+
this.needSnapshot = true;
|
|
1190
|
+
}
|
|
1191
|
+
if (restricted) {
|
|
1192
|
+
this.records = [];
|
|
1193
|
+
this.observer?.takeRecords();
|
|
1194
|
+
this.scrolled.clear();
|
|
1195
|
+
return this.needSnapshot ? {
|
|
1196
|
+
type: "restricted",
|
|
1197
|
+
page: this.page,
|
|
1198
|
+
url: this.doc.location?.origin ?? ""
|
|
1199
|
+
} : null;
|
|
1200
|
+
}
|
|
1201
|
+
if (this.needSnapshot) return this.snapshot() ?? "too_big";
|
|
1202
|
+
const changes = this.changes();
|
|
1203
|
+
if (changes === null && this.tooBig) return "too_big";
|
|
1204
|
+
if (changes && frameBytes(changes) > COBROWSE_LIMITS.batchBytes) return this.snapshot() ?? "too_big";
|
|
1205
|
+
return changes;
|
|
1206
|
+
}
|
|
1207
|
+
async flush() {
|
|
1208
|
+
if (this.stopped) return;
|
|
1209
|
+
if (this.sending) return;
|
|
1210
|
+
const frame = this.nextFrame();
|
|
1211
|
+
if (frame === "too_big") {
|
|
1212
|
+
this.end("too_big");
|
|
1213
|
+
return;
|
|
1214
|
+
}
|
|
1215
|
+
if (frame === null) {
|
|
1216
|
+
this.armKeepAlive();
|
|
1217
|
+
return;
|
|
1218
|
+
}
|
|
1219
|
+
let size = frameBytes(frame);
|
|
1220
|
+
const used = this.minuteBytes() + size;
|
|
1221
|
+
if (used > COBROWSE_LIMITS.minuteBytes) {
|
|
1222
|
+
this.end("busy");
|
|
1223
|
+
return;
|
|
1224
|
+
}
|
|
1225
|
+
const slow = used > COBROWSE_LIMITS.slowMinuteBytes;
|
|
1226
|
+
if (slow !== this.slow) {
|
|
1227
|
+
this.slow = slow;
|
|
1228
|
+
this.options.onSlow?.(slow);
|
|
1229
|
+
if (frame.type === "changes") {
|
|
1230
|
+
frame.slow = slow;
|
|
1231
|
+
size = frameBytes(frame);
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
this.sending = true;
|
|
1235
|
+
this.needSnapshot = false;
|
|
1236
|
+
const sentAt = this.now;
|
|
1237
|
+
const result = await this.options.send(frame);
|
|
1238
|
+
this.sending = false;
|
|
1239
|
+
if (this.stopped) return;
|
|
1240
|
+
if (result === "refused") {
|
|
1241
|
+
this.end("refused");
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
this.lastSent = sentAt;
|
|
1245
|
+
if (result !== true) {
|
|
1246
|
+
if (frame.type === "snapshot" && typeof result === "string") this.reportTrouble(result, size);
|
|
1247
|
+
this.needSnapshot = true;
|
|
1248
|
+
this.failures += 1;
|
|
1249
|
+
if (this.timer) clearTimeout(this.timer);
|
|
1250
|
+
this.timer = null;
|
|
1251
|
+
this.schedule(Math.min(COBROWSE_LIMITS.keepAliveMs, COBROWSE_LIMITS.slowFlushMs * 2 ** (this.failures - 1)));
|
|
1252
|
+
this.armKeepAlive();
|
|
1253
|
+
return;
|
|
1254
|
+
}
|
|
1255
|
+
this.failures = 0;
|
|
1256
|
+
this.sentLog.push([sentAt, size]);
|
|
1257
|
+
if (this.needSnapshot || this.records.length || this.scrolled.size) this.schedule();
|
|
1258
|
+
this.armKeepAlive();
|
|
1259
|
+
}
|
|
1260
|
+
/**
|
|
1261
|
+
* A copy that did not get through: support's view says why instead of
|
|
1262
|
+
* waiting for it (a small frame, which gets through where a big one is
|
|
1263
|
+
* cut). The server reports a copy it refuses itself (its origin).
|
|
1264
|
+
*/
|
|
1265
|
+
reportTrouble(reason, bytes) {
|
|
1266
|
+
if (reason === "origin") return;
|
|
1267
|
+
this.options.send({
|
|
1268
|
+
type: "trouble",
|
|
1269
|
+
page: this.page,
|
|
1270
|
+
reason,
|
|
1271
|
+
bytes
|
|
1272
|
+
});
|
|
1273
|
+
}
|
|
1274
|
+
armKeepAlive() {
|
|
1275
|
+
if (this.keepAlive) clearTimeout(this.keepAlive);
|
|
1276
|
+
this.keepAlive = setTimeout(() => {
|
|
1277
|
+
this.keepAlive = null;
|
|
1278
|
+
if (this.stopped) return;
|
|
1279
|
+
if (!this.sending) this.options.send({
|
|
1280
|
+
type: "alive",
|
|
1281
|
+
page: this.page
|
|
1282
|
+
}).then((result) => {
|
|
1283
|
+
if (result === "refused") this.end("refused");
|
|
1284
|
+
});
|
|
1285
|
+
this.armKeepAlive();
|
|
1286
|
+
}, COBROWSE_LIMITS.keepAliveMs);
|
|
1287
|
+
}
|
|
1288
|
+
end(reason) {
|
|
1289
|
+
if (this.stopped) return;
|
|
1290
|
+
this.destroy();
|
|
1291
|
+
this.options.onLimit(reason);
|
|
1292
|
+
}
|
|
1293
|
+
};
|
|
1294
|
+
// src/cobrowse/visitor-cobrowse.ts
|
|
1295
|
+
/** The box of a live node: an element's, or a text node's text. */
|
|
1296
|
+
function nodeRect(node) {
|
|
1297
|
+
if (!node?.isConnected) return null;
|
|
1298
|
+
if (node instanceof Element) return node.getBoundingClientRect();
|
|
1299
|
+
if (node.nodeType === 3) {
|
|
1300
|
+
const range = node.ownerDocument?.createRange();
|
|
1301
|
+
if (!range) return null;
|
|
1302
|
+
range.selectNodeContents(node);
|
|
1303
|
+
return range.getBoundingClientRect();
|
|
1304
|
+
}
|
|
1305
|
+
return null;
|
|
1306
|
+
}
|
|
1307
|
+
/** The element a lasting pointer names on this page, or null. */
|
|
1308
|
+
function pointedElement(recorder, at) {
|
|
1309
|
+
if (at.page !== recorder.page) return null;
|
|
1310
|
+
const node = recorder.node(at.node);
|
|
1311
|
+
if (!node?.isConnected) return null;
|
|
1312
|
+
return node instanceof Element ? node : node.parentElement;
|
|
1313
|
+
}
|
|
1314
|
+
/** Where an anchor points on this page now, or null (another page). */
|
|
1315
|
+
function anchorTarget(recorder, at) {
|
|
1316
|
+
return () => {
|
|
1317
|
+
if ("screen" in at || at.page !== recorder.page) return null;
|
|
1318
|
+
const rect = nodeRect(recorder.node(at.node));
|
|
1319
|
+
if (!rect || rect.width === 0 && rect.height === 0) return null;
|
|
1320
|
+
return pointOf(rect, at.fx, at.fy);
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
/**
|
|
1324
|
+
* What the widget covers on the visitor's screen: its open panel and its
|
|
1325
|
+
* launcher (marked `data-luma-widget-area` in its shadow root), as boxes
|
|
1326
|
+
* of the viewport. A closed (hidden) panel covers nothing.
|
|
1327
|
+
*/
|
|
1328
|
+
function widgetAreas(layer) {
|
|
1329
|
+
const root = layer.getRootNode();
|
|
1330
|
+
const areas = [];
|
|
1331
|
+
for (const element of root.querySelectorAll("[data-luma-widget-area]")) {
|
|
1332
|
+
const kind = element.getAttribute("data-luma-widget-area");
|
|
1333
|
+
if (kind !== "panel" && kind !== "launcher") continue;
|
|
1334
|
+
const style = getComputedStyle(element);
|
|
1335
|
+
if (style.display === "none" || style.visibility === "hidden" || Number(style.opacity) < .1) continue;
|
|
1336
|
+
const rect = element.getBoundingClientRect();
|
|
1337
|
+
if (rect.width < 1 || rect.height < 1) continue;
|
|
1338
|
+
areas.push({
|
|
1339
|
+
kind,
|
|
1340
|
+
x: Math.round(rect.left),
|
|
1341
|
+
y: Math.round(rect.top),
|
|
1342
|
+
w: Math.round(rect.width),
|
|
1343
|
+
h: Math.round(rect.height)
|
|
1344
|
+
});
|
|
1345
|
+
}
|
|
1346
|
+
return areas.slice(0, 4);
|
|
1347
|
+
}
|
|
1348
|
+
function startCobrowse(options) {
|
|
1349
|
+
let ended = false;
|
|
1350
|
+
const cursor = createSupportCursor(options.layer, options.lang);
|
|
1351
|
+
const recorder = new Recorder({
|
|
1352
|
+
document,
|
|
1353
|
+
privateSelector: options.privateSelector,
|
|
1354
|
+
skip: options.skip,
|
|
1355
|
+
restricted: options.restricted,
|
|
1356
|
+
send: options.send,
|
|
1357
|
+
onSlow: options.onSlow,
|
|
1358
|
+
areas: () => widgetAreas(options.layer),
|
|
1359
|
+
onLimit: (reason) => {
|
|
1360
|
+
if (ended) return;
|
|
1361
|
+
ended = true;
|
|
1362
|
+
cursor.destroy();
|
|
1363
|
+
options.send({
|
|
1364
|
+
type: "end",
|
|
1365
|
+
reason
|
|
1366
|
+
});
|
|
1367
|
+
options.onEnd(reason);
|
|
1368
|
+
}
|
|
1369
|
+
});
|
|
1370
|
+
recorder.start();
|
|
1371
|
+
const destroy = () => {
|
|
1372
|
+
if (ended) return;
|
|
1373
|
+
ended = true;
|
|
1374
|
+
recorder.destroy();
|
|
1375
|
+
cursor.destroy();
|
|
1376
|
+
};
|
|
1377
|
+
return {
|
|
1378
|
+
command(command) {
|
|
1379
|
+
if (ended) return;
|
|
1380
|
+
switch (command.type) {
|
|
1381
|
+
case "cursor":
|
|
1382
|
+
cursor.move(command.at && !("screen" in command.at) ? anchorTarget(recorder, command.at) : null);
|
|
1383
|
+
return;
|
|
1384
|
+
case "ripple":
|
|
1385
|
+
if (!("screen" in command.at)) cursor.ripple(anchorTarget(recorder, command.at));
|
|
1386
|
+
return;
|
|
1387
|
+
case "point":
|
|
1388
|
+
options.onPoint?.(command.at ? {
|
|
1389
|
+
id: command.id,
|
|
1390
|
+
target: pointedElement(recorder, command.at),
|
|
1391
|
+
text: command.text
|
|
1392
|
+
} : null);
|
|
1393
|
+
return;
|
|
1394
|
+
case "resync":
|
|
1395
|
+
recorder.resync();
|
|
1396
|
+
return;
|
|
1397
|
+
case "end":
|
|
1398
|
+
destroy();
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1401
|
+
},
|
|
1402
|
+
stop(reason) {
|
|
1403
|
+
if (ended) return;
|
|
1404
|
+
destroy();
|
|
1405
|
+
options.send({
|
|
1406
|
+
type: "end",
|
|
1407
|
+
reason
|
|
1408
|
+
});
|
|
1409
|
+
},
|
|
1410
|
+
destroy
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
export { startCobrowse };
|
|
1414
|
+
|
|
1415
|
+
//# sourceMappingURL=visitor-cobrowse-Ba7H1VtS.js.map
|