@sedum-dev/core 0.1.0-alpha.0
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/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/index.d.ts +2396 -0
- package/dist/index.js +6277 -0
- package/dist/page-script/index.global.js +611 -0
- package/dist/run-result.schema.json +1702 -0
- package/package.json +32 -0
|
@@ -0,0 +1,611 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var SedumPageScript = (() => {
|
|
3
|
+
// src/page-protocol.ts
|
|
4
|
+
var PAGE_PROTOCOL = 1;
|
|
5
|
+
var CANDIDATE_LIMIT = 128;
|
|
6
|
+
var PEER_LIMIT = 80;
|
|
7
|
+
var DIGEST_LIMIT = 4096;
|
|
8
|
+
var SAFE_ROLES = /* @__PURE__ */ new Set([
|
|
9
|
+
"button",
|
|
10
|
+
"link",
|
|
11
|
+
"checkbox",
|
|
12
|
+
"radio",
|
|
13
|
+
"switch",
|
|
14
|
+
"tab",
|
|
15
|
+
"menuitem",
|
|
16
|
+
"option",
|
|
17
|
+
"textbox",
|
|
18
|
+
"searchbox",
|
|
19
|
+
"combobox",
|
|
20
|
+
"spinbutton",
|
|
21
|
+
"slider",
|
|
22
|
+
"heading"
|
|
23
|
+
]);
|
|
24
|
+
function isSafeRole(value) {
|
|
25
|
+
return SAFE_ROLES.has(value);
|
|
26
|
+
}
|
|
27
|
+
function isWeakPeer(value) {
|
|
28
|
+
const text = value.trim();
|
|
29
|
+
return /^[\p{Sc}\d.,\s%+-]+$/u.test(text) || /^(USD|EUR|GBP|JPY|CAD|AUD|CHF)$/i.test(text);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/page-script/index.ts
|
|
33
|
+
if (!window.__sedum) {
|
|
34
|
+
let version = function() {
|
|
35
|
+
if (route !== location.href) {
|
|
36
|
+
route = location.href;
|
|
37
|
+
revision++;
|
|
38
|
+
}
|
|
39
|
+
return { document: documentId, revision, route };
|
|
40
|
+
}, same = function(a, b) {
|
|
41
|
+
return a.document === b.document && a.revision === b.revision && a.route === b.route;
|
|
42
|
+
}, clearRefs = function() {
|
|
43
|
+
for (const [element, { old, ref }] of owned) {
|
|
44
|
+
if (element.getAttribute("data-sedum-ref") !== ref) continue;
|
|
45
|
+
if (old === null) element.removeAttribute("data-sedum-ref");
|
|
46
|
+
else element.setAttribute("data-sedum-ref", old);
|
|
47
|
+
}
|
|
48
|
+
owned.clear();
|
|
49
|
+
snapshot = void 0;
|
|
50
|
+
}, visible = function(element) {
|
|
51
|
+
if (element.closest(
|
|
52
|
+
"[hidden],[inert],[aria-hidden='true'],dialog:not([open])"
|
|
53
|
+
))
|
|
54
|
+
return false;
|
|
55
|
+
const dialog = element.closest("dialog,[role='dialog']");
|
|
56
|
+
if (dialog) {
|
|
57
|
+
const bounds = dialog.getBoundingClientRect();
|
|
58
|
+
if (bounds.right <= 0 || bounds.bottom <= 0 || bounds.left >= innerWidth || bounds.top >= innerHeight)
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
for (let node = element; node; node = node.parentElement) {
|
|
62
|
+
const style2 = getComputedStyle(node);
|
|
63
|
+
if (Number.parseFloat(style2.opacity) === 0 || style2.contentVisibility === "hidden")
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
const style = getComputedStyle(element);
|
|
67
|
+
if (style.display === "none" || style.visibility === "hidden" || style.visibility === "collapse")
|
|
68
|
+
return false;
|
|
69
|
+
if (element.getClientRects().length > 0) return true;
|
|
70
|
+
if (style.display !== "contents") return false;
|
|
71
|
+
const range = document.createRange();
|
|
72
|
+
range.selectNodeContents(element);
|
|
73
|
+
return Array.from(range.getClientRects()).some(
|
|
74
|
+
(rect) => rect.width > 0 && rect.height > 0
|
|
75
|
+
);
|
|
76
|
+
}, modal = function() {
|
|
77
|
+
for (const dialog of modalOrder.keys())
|
|
78
|
+
if (!dialog.isConnected || !dialog.open) modalOrder.delete(dialog);
|
|
79
|
+
const dialogs = Array.from(
|
|
80
|
+
document.querySelectorAll(
|
|
81
|
+
"dialog:modal,[role='dialog'][aria-modal='true']"
|
|
82
|
+
)
|
|
83
|
+
).filter(visible);
|
|
84
|
+
if (!dialogs.length) return null;
|
|
85
|
+
const native = dialogs.filter(
|
|
86
|
+
(element) => element.matches("dialog:modal")
|
|
87
|
+
);
|
|
88
|
+
if (native.length) {
|
|
89
|
+
const ordered = [...native].sort(
|
|
90
|
+
(a, b) => (modalOrder.get(b) ?? 0) - (modalOrder.get(a) ?? 0)
|
|
91
|
+
);
|
|
92
|
+
if ((modalOrder.get(ordered[0]) ?? 0) > 0) return ordered[0];
|
|
93
|
+
return native.find((element) => element.contains(document.activeElement)) ?? (native.length === 1 ? native[0] : void 0);
|
|
94
|
+
}
|
|
95
|
+
return dialogs.length === 1 ? dialogs[0] : void 0;
|
|
96
|
+
}, excludedTextAncestor = function(element, includeActions = false) {
|
|
97
|
+
for (let node = element; node; node = node.parentElement) {
|
|
98
|
+
if (node.matches(
|
|
99
|
+
"input,textarea,select,[contenteditable],script,style,noscript"
|
|
100
|
+
) || ["textbox", "searchbox", "combobox", "spinbutton"].includes(
|
|
101
|
+
role(node)
|
|
102
|
+
) || includeActions && (node.matches("button,a[href]") || role(node) === "button" || role(node) === "link"))
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
106
|
+
}, publicText = function(element) {
|
|
107
|
+
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT);
|
|
108
|
+
const parts = [];
|
|
109
|
+
while (walker.nextNode()) {
|
|
110
|
+
const parent = walker.currentNode.parentElement;
|
|
111
|
+
if (parent && visible(parent) && !excludedTextAncestor(parent))
|
|
112
|
+
parts.push(walker.currentNode.textContent ?? "");
|
|
113
|
+
}
|
|
114
|
+
return parts.join(" ").replace(/\s+/g, " ").trim();
|
|
115
|
+
}, label = function(element) {
|
|
116
|
+
const aria = element.getAttribute("aria-label");
|
|
117
|
+
if (aria) return aria.trim();
|
|
118
|
+
const labelledby = element.getAttribute("aria-labelledby");
|
|
119
|
+
if (labelledby)
|
|
120
|
+
return labelledby.split(/\s+/).map((id) => {
|
|
121
|
+
const named = document.getElementById(id);
|
|
122
|
+
return named ? publicText(named) : "";
|
|
123
|
+
}).filter(Boolean).join(" ");
|
|
124
|
+
if (element instanceof HTMLElement && "labels" in element) {
|
|
125
|
+
const labels = element.labels;
|
|
126
|
+
if (labels?.length) return Array.from(labels).map(publicText).join(" ");
|
|
127
|
+
}
|
|
128
|
+
if (element instanceof HTMLInputElement && ["button", "submit", "reset"].includes(element.type))
|
|
129
|
+
return element.value.trim();
|
|
130
|
+
return publicText(element);
|
|
131
|
+
}, role = function(element) {
|
|
132
|
+
const explicit = element.getAttribute("role");
|
|
133
|
+
const canonical = explicit?.split(/\s+/).find(isSafeRole);
|
|
134
|
+
if (canonical) return canonical;
|
|
135
|
+
if (element instanceof HTMLButtonElement) return "button";
|
|
136
|
+
if (element instanceof HTMLAnchorElement) return "link";
|
|
137
|
+
if (element instanceof HTMLInputElement)
|
|
138
|
+
return ["button", "submit", "reset"].includes(element.type) ? "button" : element.type === "checkbox" ? "checkbox" : element.type === "radio" ? "radio" : element.type === "range" ? "slider" : element.type === "number" ? "spinbutton" : element.type === "search" ? "searchbox" : "textbox";
|
|
139
|
+
if (element instanceof HTMLTextAreaElement) return "textbox";
|
|
140
|
+
if (element instanceof HTMLSelectElement) return "combobox";
|
|
141
|
+
return "";
|
|
142
|
+
}, interactive = function(element) {
|
|
143
|
+
if (element.parentElement?.closest("a[href]")) return false;
|
|
144
|
+
return element.matches(
|
|
145
|
+
"button,a[href],input,textarea,select,[contenteditable]"
|
|
146
|
+
) || [
|
|
147
|
+
"button",
|
|
148
|
+
"link",
|
|
149
|
+
"checkbox",
|
|
150
|
+
"radio",
|
|
151
|
+
"switch",
|
|
152
|
+
"tab",
|
|
153
|
+
"menuitem",
|
|
154
|
+
"option",
|
|
155
|
+
"textbox",
|
|
156
|
+
"searchbox",
|
|
157
|
+
"combobox",
|
|
158
|
+
"spinbutton",
|
|
159
|
+
"slider"
|
|
160
|
+
].includes(role(element));
|
|
161
|
+
}, readable = function(element) {
|
|
162
|
+
if (interactive(element) || excludedTextAncestor(element, true))
|
|
163
|
+
return false;
|
|
164
|
+
if (element.matches("h1,h2,h3,h4,h5,h6,p,blockquote,td,th,[role='heading']"))
|
|
165
|
+
return true;
|
|
166
|
+
return element.children.length === 0 && element.matches("span,strong,em,div,li") && publicText(element).length > 0;
|
|
167
|
+
}, editable = function(element) {
|
|
168
|
+
if (element instanceof HTMLInputElement)
|
|
169
|
+
return FILLABLE_INPUT_TYPES.has(element.type) && !disabled(element);
|
|
170
|
+
if (element instanceof HTMLTextAreaElement) return !disabled(element);
|
|
171
|
+
return element instanceof HTMLElement && element.isContentEditable && !disabled(element);
|
|
172
|
+
}, disabled = function(element) {
|
|
173
|
+
return element.matches(":disabled,[aria-disabled='true']") || !!element.closest("[inert]");
|
|
174
|
+
}, path = function(element) {
|
|
175
|
+
const steps = [];
|
|
176
|
+
for (let node = element; node && steps.length < 12; node = node.parentElement) {
|
|
177
|
+
const parent = node.parentElement;
|
|
178
|
+
const siblings = parent ? Array.from(parent.children).filter(
|
|
179
|
+
(child) => child.tagName === node?.tagName
|
|
180
|
+
) : [node];
|
|
181
|
+
steps.unshift(`${node.tagName.toLowerCase()}:${siblings.indexOf(node)}`);
|
|
182
|
+
}
|
|
183
|
+
return steps.join("/");
|
|
184
|
+
}, inArticleBody = function(element) {
|
|
185
|
+
return !!element.closest("main,article,[role='main']") && !element.closest("aside,nav,table,[role='navigation'],.infobox,.navbox");
|
|
186
|
+
}, itemContext = function(region) {
|
|
187
|
+
const walker = document.createTreeWalker(region, NodeFilter.SHOW_TEXT);
|
|
188
|
+
const pieces = [];
|
|
189
|
+
let length = 0;
|
|
190
|
+
while (walker.nextNode()) {
|
|
191
|
+
const parent = walker.currentNode.parentElement;
|
|
192
|
+
if (!parent || !visible(parent) || excludedTextAncestor(parent, true))
|
|
193
|
+
continue;
|
|
194
|
+
const part = (walker.currentNode.textContent ?? "").replace(/\s+/g, " ").trim();
|
|
195
|
+
if (!part) continue;
|
|
196
|
+
length += Array.from(part).length + (pieces.length ? 1 : 0);
|
|
197
|
+
if (length > PEER_LIMIT) return { text: "", complete: false };
|
|
198
|
+
pieces.push(part);
|
|
199
|
+
}
|
|
200
|
+
return { text: pieces.join(" "), complete: true };
|
|
201
|
+
}, peers = function(element, name) {
|
|
202
|
+
const row = element.closest("tr");
|
|
203
|
+
const preceding = row?.previousElementSibling;
|
|
204
|
+
const rank = preceding?.querySelector(".rank");
|
|
205
|
+
const title = preceding?.querySelector(".titleline > a");
|
|
206
|
+
const rankedPeer = rank && title ? Array.from(`${publicText(rank)} ${publicText(title)}`.trim()).slice(0, PEER_LIMIT).join("") : "";
|
|
207
|
+
let region = element.parentElement;
|
|
208
|
+
let lastUnique = null;
|
|
209
|
+
while (region && region !== document.body) {
|
|
210
|
+
const possible = region.querySelectorAll(
|
|
211
|
+
"button,a[href],[role='button'],input[type='button'],input[type='submit']"
|
|
212
|
+
);
|
|
213
|
+
if (possible.length > 32) {
|
|
214
|
+
region = lastUnique;
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
const sameLabel = Array.from(possible).filter(
|
|
218
|
+
(item) => visible(item) && label(item) === name
|
|
219
|
+
);
|
|
220
|
+
if (sameLabel.length > 1) {
|
|
221
|
+
region = lastUnique;
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
lastUnique = region;
|
|
225
|
+
if (region.matches("article,li,[data-product],[role='listitem']")) break;
|
|
226
|
+
region = region.parentElement;
|
|
227
|
+
}
|
|
228
|
+
region = region === document.body ? lastUnique : region;
|
|
229
|
+
if (!region)
|
|
230
|
+
return { texts: rankedPeer ? [rankedPeer] : [], contextComplete: false };
|
|
231
|
+
const result = [];
|
|
232
|
+
if (rankedPeer) result.push(rankedPeer);
|
|
233
|
+
const context = itemContext(region);
|
|
234
|
+
if (context.text && result.length < 2) result.push(context.text);
|
|
235
|
+
const named = Array.from(
|
|
236
|
+
region.querySelectorAll(
|
|
237
|
+
"[data-product-name],h1,h2,h3,h4,h5,h6,[role='heading']"
|
|
238
|
+
)
|
|
239
|
+
);
|
|
240
|
+
const meaningfulLeaves = Array.from(
|
|
241
|
+
region.querySelectorAll("div,span,strong,p")
|
|
242
|
+
).filter(
|
|
243
|
+
(child) => child.children.length === 0 && !isWeakPeer(publicText(child))
|
|
244
|
+
).sort(
|
|
245
|
+
(a, b) => Number(b.tagName === "DIV") - Number(a.tagName === "DIV")
|
|
246
|
+
);
|
|
247
|
+
const nearby = Array.from(region.querySelectorAll("strong,span,p"));
|
|
248
|
+
for (const child of [...named, ...meaningfulLeaves, ...nearby]) {
|
|
249
|
+
if (result.length === 2) break;
|
|
250
|
+
if (child === element || child.contains(element) || element.contains(child) || !visible(child))
|
|
251
|
+
continue;
|
|
252
|
+
const text = Array.from(publicText(child)).slice(0, PEER_LIMIT).join("");
|
|
253
|
+
if (text && text !== name && !result.includes(text)) result.push(text);
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
texts: result,
|
|
257
|
+
contextComplete: !rankedPeer && context.complete && !!context.text
|
|
258
|
+
};
|
|
259
|
+
}, scan = function(operation) {
|
|
260
|
+
clearRefs();
|
|
261
|
+
const refs = /* @__PURE__ */ new Map();
|
|
262
|
+
const candidates = [];
|
|
263
|
+
const selectedModal = modal();
|
|
264
|
+
if (selectedModal === void 0)
|
|
265
|
+
return { candidates, refs, complete: false };
|
|
266
|
+
const root = selectedModal ?? document.body;
|
|
267
|
+
if (!root) return { candidates, refs, complete: true };
|
|
268
|
+
const elements = root.querySelectorAll("*");
|
|
269
|
+
if (elements.length > MAX_ELEMENTS)
|
|
270
|
+
return { candidates, refs, complete: false };
|
|
271
|
+
for (const element of Array.from(elements)) {
|
|
272
|
+
if (!visible(element) || (operation === "read" ? !readable(element) : !interactive(element)))
|
|
273
|
+
continue;
|
|
274
|
+
if (operation === "fill" && !editable(element)) continue;
|
|
275
|
+
if (operation === "click" && editable(element) && !element.matches("input[type='checkbox'],input[type='radio']"))
|
|
276
|
+
continue;
|
|
277
|
+
const name = label(element);
|
|
278
|
+
if (!name) continue;
|
|
279
|
+
const ref = `${documentId}-${++sequence}`;
|
|
280
|
+
const peerData = peers(element, name);
|
|
281
|
+
owned.set(element, { old: element.getAttribute("data-sedum-ref"), ref });
|
|
282
|
+
element.setAttribute("data-sedum-ref", ref);
|
|
283
|
+
refs.set(ref, element);
|
|
284
|
+
candidates.push(
|
|
285
|
+
Object.freeze({
|
|
286
|
+
ref,
|
|
287
|
+
tag: element.tagName.toLowerCase(),
|
|
288
|
+
role: role(element),
|
|
289
|
+
name,
|
|
290
|
+
peers: Object.freeze(peerData.texts),
|
|
291
|
+
editable: editable(element),
|
|
292
|
+
disabled: disabled(element),
|
|
293
|
+
inputType: element instanceof HTMLInputElement ? element.type : "",
|
|
294
|
+
signals: Object.freeze({
|
|
295
|
+
...element.getAttribute("data-testid") ? { hook: element.getAttribute("data-testid") } : {},
|
|
296
|
+
...element.id ? { id: element.id } : {},
|
|
297
|
+
...element.getAttribute("name") ? { name: element.getAttribute("name") } : {},
|
|
298
|
+
...element.getAttribute("href") ? { href: element.getAttribute("href") } : {},
|
|
299
|
+
...inArticleBody(element) ? { region: "article-body" } : {},
|
|
300
|
+
path: path(element),
|
|
301
|
+
contextComplete: peerData.contextComplete
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
return { candidates: Object.freeze(candidates), refs, complete: true };
|
|
307
|
+
}, collect = function(input) {
|
|
308
|
+
const current = version();
|
|
309
|
+
const offset = input.offset ?? 0;
|
|
310
|
+
if (offset > 0 && (!snapshot || snapshot.operation !== input.operation || !input.version || !same(snapshot.version, input.version) || !same(current, input.version)))
|
|
311
|
+
return {
|
|
312
|
+
protocol: PAGE_PROTOCOL,
|
|
313
|
+
version: current,
|
|
314
|
+
total: 0,
|
|
315
|
+
offset,
|
|
316
|
+
next: null,
|
|
317
|
+
complete: false,
|
|
318
|
+
candidates: []
|
|
319
|
+
};
|
|
320
|
+
if (offset === 0) {
|
|
321
|
+
const result = scan(input.operation);
|
|
322
|
+
snapshot = { version: version(), operation: input.operation, ...result };
|
|
323
|
+
}
|
|
324
|
+
const currentSnapshot = snapshot;
|
|
325
|
+
if (!currentSnapshot.complete)
|
|
326
|
+
return {
|
|
327
|
+
protocol: PAGE_PROTOCOL,
|
|
328
|
+
version: currentSnapshot.version,
|
|
329
|
+
total: 0,
|
|
330
|
+
offset,
|
|
331
|
+
next: null,
|
|
332
|
+
complete: false,
|
|
333
|
+
candidates: []
|
|
334
|
+
};
|
|
335
|
+
const candidates = currentSnapshot.candidates.slice(
|
|
336
|
+
offset,
|
|
337
|
+
offset + CANDIDATE_LIMIT
|
|
338
|
+
);
|
|
339
|
+
const next = offset + candidates.length < currentSnapshot.candidates.length ? offset + candidates.length : null;
|
|
340
|
+
return {
|
|
341
|
+
protocol: PAGE_PROTOCOL,
|
|
342
|
+
version: currentSnapshot.version,
|
|
343
|
+
total: currentSnapshot.candidates.length,
|
|
344
|
+
offset,
|
|
345
|
+
next,
|
|
346
|
+
complete: true,
|
|
347
|
+
candidates
|
|
348
|
+
};
|
|
349
|
+
}, digest = function() {
|
|
350
|
+
const current = version();
|
|
351
|
+
const selectedModal = modal();
|
|
352
|
+
if (selectedModal === void 0)
|
|
353
|
+
return {
|
|
354
|
+
protocol: PAGE_PROTOCOL,
|
|
355
|
+
version: current,
|
|
356
|
+
text: "",
|
|
357
|
+
complete: false,
|
|
358
|
+
error: "scope_ambiguous"
|
|
359
|
+
};
|
|
360
|
+
const root = selectedModal ?? document.body;
|
|
361
|
+
if (!root)
|
|
362
|
+
return {
|
|
363
|
+
protocol: PAGE_PROTOCOL,
|
|
364
|
+
version: current,
|
|
365
|
+
text: "",
|
|
366
|
+
complete: true
|
|
367
|
+
};
|
|
368
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
|
|
369
|
+
const pieces = [];
|
|
370
|
+
let size = 0;
|
|
371
|
+
let count = 0;
|
|
372
|
+
while (walker.nextNode()) {
|
|
373
|
+
if (++count > MAX_TEXT_NODES)
|
|
374
|
+
return {
|
|
375
|
+
protocol: PAGE_PROTOCOL,
|
|
376
|
+
version: current,
|
|
377
|
+
text: "",
|
|
378
|
+
complete: false,
|
|
379
|
+
error: "resource_ceiling"
|
|
380
|
+
};
|
|
381
|
+
const node = walker.currentNode;
|
|
382
|
+
const parent = node.parentElement;
|
|
383
|
+
if (!parent || !visible(parent) || excludedTextAncestor(parent)) continue;
|
|
384
|
+
const part = (node.textContent ?? "").replace(/\s+/g, " ").trim();
|
|
385
|
+
if (!part) continue;
|
|
386
|
+
size += Array.from(part).length + (pieces.length ? 1 : 0);
|
|
387
|
+
if (size > DIGEST_LIMIT)
|
|
388
|
+
return {
|
|
389
|
+
protocol: PAGE_PROTOCOL,
|
|
390
|
+
version: current,
|
|
391
|
+
text: "",
|
|
392
|
+
complete: false,
|
|
393
|
+
error: "digest_too_large"
|
|
394
|
+
};
|
|
395
|
+
pieces.push(part);
|
|
396
|
+
}
|
|
397
|
+
return {
|
|
398
|
+
protocol: PAGE_PROTOCOL,
|
|
399
|
+
version: current,
|
|
400
|
+
text: pieces.join(" "),
|
|
401
|
+
complete: true
|
|
402
|
+
};
|
|
403
|
+
}, refElement = function(ref) {
|
|
404
|
+
const element = snapshot?.refs.get(ref);
|
|
405
|
+
if (!element || !element.isConnected || element.ownerDocument !== document || element.getAttribute("data-sedum-ref") !== ref)
|
|
406
|
+
return null;
|
|
407
|
+
return element;
|
|
408
|
+
}, fillElement = function(target) {
|
|
409
|
+
const current = version();
|
|
410
|
+
if (!snapshot || snapshot.operation !== "fill" || !same(snapshot.version, current) || !same(target.version, current))
|
|
411
|
+
return null;
|
|
412
|
+
const element = refElement(target.ref);
|
|
413
|
+
if (!element || element.tagName.toLowerCase() !== target.tag) return null;
|
|
414
|
+
const matches = Array.from(
|
|
415
|
+
document.querySelectorAll("[data-sedum-ref]")
|
|
416
|
+
).filter((node) => node.getAttribute("data-sedum-ref") === target.ref);
|
|
417
|
+
if (matches.length !== 1 || matches[0] !== element) return null;
|
|
418
|
+
const candidate = snapshot.candidates.find(
|
|
419
|
+
(item) => item.ref === target.ref
|
|
420
|
+
);
|
|
421
|
+
if (!candidate || !candidate.editable || !(element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLElement && element.isContentEditable) || candidate.name !== target.name || label(element) !== candidate.name || JSON.stringify(peers(element, candidate.name).texts) !== JSON.stringify(candidate.peers) || !editable(element) || !visible(element) || disabled(element))
|
|
422
|
+
return null;
|
|
423
|
+
return element;
|
|
424
|
+
}, readTarget = function(target) {
|
|
425
|
+
const current = version();
|
|
426
|
+
if (!snapshot || snapshot.operation !== "read" || !same(snapshot.version, current) || !same(target.version, current))
|
|
427
|
+
return { status: "stale" };
|
|
428
|
+
const element = refElement(target.ref);
|
|
429
|
+
const candidate = snapshot.candidates.find(
|
|
430
|
+
(item) => item.ref === target.ref
|
|
431
|
+
);
|
|
432
|
+
if (!element || !candidate || element.tagName.toLowerCase() !== target.tag || candidate.name !== target.name || !readable(element) || !visible(element) || label(element) !== candidate.name || JSON.stringify(peers(element, candidate.name).texts) !== JSON.stringify(candidate.peers))
|
|
433
|
+
return { status: "stale" };
|
|
434
|
+
const text = publicText(element);
|
|
435
|
+
if (!text) return { status: "empty" };
|
|
436
|
+
if (Array.from(text).length > 4096) return { status: "too_long" };
|
|
437
|
+
return { status: "ok", text };
|
|
438
|
+
}, aim = function(ref, expected) {
|
|
439
|
+
const current = version();
|
|
440
|
+
if (!snapshot || !same(snapshot.version, current))
|
|
441
|
+
return { actionable: false, reason: "stale" };
|
|
442
|
+
if (expected && (expected.document !== current.document || expected.route !== current.route || expected.revision !== current.revision))
|
|
443
|
+
return { actionable: false, reason: "stale" };
|
|
444
|
+
const element = refElement(ref);
|
|
445
|
+
if (!element) return { actionable: false, reason: "target_missing" };
|
|
446
|
+
if (expected && element.tagName.toLowerCase() !== expected.tag)
|
|
447
|
+
return { actionable: false, reason: "stale" };
|
|
448
|
+
const candidate = snapshot?.candidates.find((item) => item.ref === ref);
|
|
449
|
+
if (!candidate || label(element) !== candidate.name || JSON.stringify(peers(element, candidate.name).texts) !== JSON.stringify(candidate.peers) || expected && expected.name !== candidate.name)
|
|
450
|
+
return { actionable: false, reason: "stale" };
|
|
451
|
+
if (!visible(element) || disabled(element))
|
|
452
|
+
return { actionable: false, reason: "not_actionable" };
|
|
453
|
+
const enclosingLink = element.closest("a[href]");
|
|
454
|
+
if (enclosingLink) {
|
|
455
|
+
if (enclosingLink !== element)
|
|
456
|
+
return { actionable: false, reason: "not_actionable" };
|
|
457
|
+
if (enclosingLink instanceof HTMLAnchorElement && (enclosingLink.hasAttribute("download") || !["", "_self"].includes(enclosingLink.target) || !["http:", "https:"].includes(new URL(enclosingLink.href).protocol)))
|
|
458
|
+
return { actionable: false, reason: "not_actionable" };
|
|
459
|
+
}
|
|
460
|
+
if (!expected)
|
|
461
|
+
element.scrollIntoView({
|
|
462
|
+
block: "center",
|
|
463
|
+
inline: "center",
|
|
464
|
+
behavior: "instant"
|
|
465
|
+
});
|
|
466
|
+
const rect = element.getBoundingClientRect();
|
|
467
|
+
if (!rect.width || !rect.height)
|
|
468
|
+
return { actionable: false, reason: "not_actionable" };
|
|
469
|
+
const points = expected ? [[expected.point.x / rect.width, expected.point.y / rect.height]] : [
|
|
470
|
+
[0.5, 0.5],
|
|
471
|
+
[0.25, 0.5],
|
|
472
|
+
[0.75, 0.5],
|
|
473
|
+
[0.5, 0.25],
|
|
474
|
+
[0.5, 0.75]
|
|
475
|
+
];
|
|
476
|
+
for (const [fx, fy] of points) {
|
|
477
|
+
const x = rect.left + rect.width * fx;
|
|
478
|
+
const y = rect.top + rect.height * fy;
|
|
479
|
+
if (x < 0 || y < 0 || x >= innerWidth || y >= innerHeight) continue;
|
|
480
|
+
const hit = document.elementFromPoint(x, y);
|
|
481
|
+
if (hit && (hit === element || element.contains(hit))) {
|
|
482
|
+
return {
|
|
483
|
+
actionable: true,
|
|
484
|
+
aim: {
|
|
485
|
+
ref,
|
|
486
|
+
document: current.document,
|
|
487
|
+
route: current.route,
|
|
488
|
+
revision: current.revision,
|
|
489
|
+
tag: element.tagName.toLowerCase(),
|
|
490
|
+
name: candidate.name,
|
|
491
|
+
point: { x: rect.width * fx, y: rect.height * fy },
|
|
492
|
+
box: {
|
|
493
|
+
x: Math.max(0, rect.left / innerWidth),
|
|
494
|
+
y: Math.max(0, rect.top / innerHeight),
|
|
495
|
+
width: Math.min(1, rect.width / innerWidth),
|
|
496
|
+
height: Math.min(1, rect.height / innerHeight)
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return { actionable: false, reason: "not_actionable" };
|
|
503
|
+
};
|
|
504
|
+
version2 = version, same2 = same, clearRefs2 = clearRefs, visible2 = visible, modal2 = modal, excludedTextAncestor2 = excludedTextAncestor, publicText2 = publicText, label2 = label, role2 = role, interactive2 = interactive, readable2 = readable, editable2 = editable, disabled2 = disabled, path2 = path, inArticleBody2 = inArticleBody, itemContext2 = itemContext, peers2 = peers, scan2 = scan, collect2 = collect, digest2 = digest, refElement2 = refElement, fillElement2 = fillElement, readTarget2 = readTarget, aim2 = aim;
|
|
505
|
+
const documentId = Array.from(crypto.getRandomValues(new Uint32Array(4))).map((part) => part.toString(16)).join("-");
|
|
506
|
+
let revision = 0;
|
|
507
|
+
let route = location.href;
|
|
508
|
+
let sequence = 0;
|
|
509
|
+
let snapshot;
|
|
510
|
+
const owned = /* @__PURE__ */ new Map();
|
|
511
|
+
const MAX_ELEMENTS = 2e4;
|
|
512
|
+
const MAX_TEXT_NODES = 2e4;
|
|
513
|
+
const FILLABLE_INPUT_TYPES = /* @__PURE__ */ new Set([
|
|
514
|
+
"text",
|
|
515
|
+
"email",
|
|
516
|
+
"password",
|
|
517
|
+
"search",
|
|
518
|
+
"tel",
|
|
519
|
+
"url",
|
|
520
|
+
"number",
|
|
521
|
+
"date",
|
|
522
|
+
"datetime-local",
|
|
523
|
+
"time",
|
|
524
|
+
"month",
|
|
525
|
+
"week"
|
|
526
|
+
]);
|
|
527
|
+
const modalOrder = /* @__PURE__ */ new Map();
|
|
528
|
+
let modalSequence = 0;
|
|
529
|
+
const observer = new MutationObserver((changes) => {
|
|
530
|
+
for (const change of changes) {
|
|
531
|
+
if (change.type === "attributes" && change.attributeName === "open" && change.target instanceof HTMLDialogElement) {
|
|
532
|
+
if (change.oldValue === null && change.target.matches(":modal"))
|
|
533
|
+
modalOrder.set(change.target, ++modalSequence);
|
|
534
|
+
else if (!change.target.open) modalOrder.delete(change.target);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
if (changes.some(
|
|
538
|
+
(change) => change.type !== "attributes" || change.attributeName !== "data-sedum-ref"
|
|
539
|
+
))
|
|
540
|
+
revision++;
|
|
541
|
+
});
|
|
542
|
+
observer.observe(document, {
|
|
543
|
+
subtree: true,
|
|
544
|
+
childList: true,
|
|
545
|
+
characterData: true,
|
|
546
|
+
attributes: true,
|
|
547
|
+
attributeOldValue: true
|
|
548
|
+
});
|
|
549
|
+
const bridge = {
|
|
550
|
+
protocol: PAGE_PROTOCOL,
|
|
551
|
+
collect,
|
|
552
|
+
digest,
|
|
553
|
+
pageVersion: version,
|
|
554
|
+
findBySignals: ({ operation }) => {
|
|
555
|
+
const first = collect({ operation });
|
|
556
|
+
return { ...first, candidates: snapshot?.candidates ?? [], next: null };
|
|
557
|
+
},
|
|
558
|
+
clickTarget: (ref) => aim(ref),
|
|
559
|
+
readTarget,
|
|
560
|
+
checkAim: (expected) => aim(expected.ref, expected),
|
|
561
|
+
fillElement,
|
|
562
|
+
clearRefs,
|
|
563
|
+
quiet: async ({ ms, timeoutMs }) => {
|
|
564
|
+
const started = performance.now();
|
|
565
|
+
let stable = performance.now();
|
|
566
|
+
let last = version();
|
|
567
|
+
while (performance.now() - started < timeoutMs) {
|
|
568
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(25, ms)));
|
|
569
|
+
const now = version();
|
|
570
|
+
if (!same(last, now)) {
|
|
571
|
+
last = now;
|
|
572
|
+
stable = performance.now();
|
|
573
|
+
}
|
|
574
|
+
if (performance.now() - stable >= ms)
|
|
575
|
+
return { version: now, quiet: true };
|
|
576
|
+
}
|
|
577
|
+
return { version: version(), quiet: false };
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
Object.freeze(bridge);
|
|
581
|
+
Object.defineProperty(window, "__sedum", {
|
|
582
|
+
value: bridge,
|
|
583
|
+
configurable: false,
|
|
584
|
+
writable: false
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
var version2;
|
|
588
|
+
var same2;
|
|
589
|
+
var clearRefs2;
|
|
590
|
+
var visible2;
|
|
591
|
+
var modal2;
|
|
592
|
+
var excludedTextAncestor2;
|
|
593
|
+
var publicText2;
|
|
594
|
+
var label2;
|
|
595
|
+
var role2;
|
|
596
|
+
var interactive2;
|
|
597
|
+
var readable2;
|
|
598
|
+
var editable2;
|
|
599
|
+
var disabled2;
|
|
600
|
+
var path2;
|
|
601
|
+
var inArticleBody2;
|
|
602
|
+
var itemContext2;
|
|
603
|
+
var peers2;
|
|
604
|
+
var scan2;
|
|
605
|
+
var collect2;
|
|
606
|
+
var digest2;
|
|
607
|
+
var refElement2;
|
|
608
|
+
var fillElement2;
|
|
609
|
+
var readTarget2;
|
|
610
|
+
var aim2;
|
|
611
|
+
})();
|