@web-auto/camo 0.2.0 → 0.2.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.
Files changed (114) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +586 -586
  3. package/bin/browser-service.mjs +11 -11
  4. package/bin/camo.mjs +22 -22
  5. package/package.json +48 -48
  6. package/scripts/build.mjs +19 -19
  7. package/scripts/bump-version.mjs +34 -34
  8. package/scripts/check-file-size.mjs +80 -80
  9. package/scripts/file-size-policy.json +12 -2
  10. package/scripts/install.mjs +76 -76
  11. package/scripts/release.sh +54 -54
  12. package/src/autoscript/action-providers/index.mjs +6 -6
  13. package/src/autoscript/impact-engine.mjs +78 -78
  14. package/src/autoscript/runtime.mjs +1017 -1017
  15. package/src/autoscript/schema.mjs +376 -376
  16. package/src/cli.mjs +405 -405
  17. package/src/commands/attach.mjs +141 -141
  18. package/src/commands/autoscript.mjs +1011 -1011
  19. package/src/commands/browser.mjs +1255 -1255
  20. package/src/commands/container.mjs +401 -401
  21. package/src/commands/cookies.mjs +69 -69
  22. package/src/commands/create.mjs +98 -98
  23. package/src/commands/devtools.mjs +349 -349
  24. package/src/commands/events.mjs +152 -152
  25. package/src/commands/highlight-mode.mjs +24 -24
  26. package/src/commands/init.mjs +68 -68
  27. package/src/commands/lifecycle.mjs +275 -275
  28. package/src/commands/mouse.mjs +45 -45
  29. package/src/commands/profile.mjs +46 -46
  30. package/src/commands/record.mjs +115 -115
  31. package/src/commands/system.mjs +14 -14
  32. package/src/commands/window.mjs +123 -123
  33. package/src/container/change-notifier.mjs +362 -362
  34. package/src/container/element-filter.mjs +143 -143
  35. package/src/container/index.mjs +3 -3
  36. package/src/container/runtime-core/checkpoint.mjs +209 -209
  37. package/src/container/runtime-core/index.mjs +21 -21
  38. package/src/container/runtime-core/operations/index.mjs +774 -774
  39. package/src/container/runtime-core/operations/selector-scripts.mjs +277 -277
  40. package/src/container/runtime-core/operations/tab-pool.mjs +746 -746
  41. package/src/container/runtime-core/operations/viewport.mjs +189 -189
  42. package/src/container/runtime-core/search.mjs +190 -190
  43. package/src/container/runtime-core/subscription.mjs +224 -224
  44. package/src/container/runtime-core/utils.mjs +94 -94
  45. package/src/container/runtime-core/validation.mjs +127 -127
  46. package/src/container/runtime-core.mjs +1 -1
  47. package/src/container/subscription-registry.mjs +459 -459
  48. package/src/core/actions.mjs +561 -561
  49. package/src/core/browser.mjs +266 -266
  50. package/src/core/index.mjs +52 -52
  51. package/src/core/utils.mjs +91 -91
  52. package/src/events/daemon-entry.mjs +33 -33
  53. package/src/events/daemon.mjs +80 -80
  54. package/src/events/progress-log.mjs +109 -109
  55. package/src/events/ws-server.mjs +239 -239
  56. package/src/lib/client.mjs +200 -200
  57. package/src/lifecycle/cleanup.mjs +83 -83
  58. package/src/lifecycle/lock.mjs +126 -126
  59. package/src/lifecycle/session-registry.mjs +279 -279
  60. package/src/lifecycle/session-view.mjs +76 -76
  61. package/src/lifecycle/session-watchdog.mjs +281 -281
  62. package/src/services/browser-service/index.js +671 -671
  63. package/src/services/browser-service/internal/BrowserSession.input.test.js +389 -389
  64. package/src/services/browser-service/internal/BrowserSession.js +325 -304
  65. package/src/services/browser-service/internal/ElementRegistry.js +60 -60
  66. package/src/services/browser-service/internal/ProfileLock.js +84 -84
  67. package/src/services/browser-service/internal/SessionManager.js +184 -184
  68. package/src/services/browser-service/internal/SessionManager.test.js +39 -39
  69. package/src/services/browser-service/internal/browser-session/cookies.js +144 -144
  70. package/src/services/browser-service/internal/browser-session/input-ops.js +222 -222
  71. package/src/services/browser-service/internal/browser-session/input-pipeline.js +144 -144
  72. package/src/services/browser-service/internal/browser-session/logging.js +46 -46
  73. package/src/services/browser-service/internal/browser-session/navigation.js +38 -38
  74. package/src/services/browser-service/internal/browser-session/page-hooks.js +442 -442
  75. package/src/services/browser-service/internal/browser-session/page-management.js +302 -302
  76. package/src/services/browser-service/internal/browser-session/page-management.test.js +148 -148
  77. package/src/services/browser-service/internal/browser-session/recording.js +198 -198
  78. package/src/services/browser-service/internal/browser-session/runtime-events.js +61 -61
  79. package/src/services/browser-service/internal/browser-session/session-core.js +84 -84
  80. package/src/services/browser-service/internal/browser-session/session-state.js +38 -38
  81. package/src/services/browser-service/internal/browser-session/types.js +14 -14
  82. package/src/services/browser-service/internal/browser-session/utils.js +95 -95
  83. package/src/services/browser-service/internal/browser-session/viewport-manager.js +46 -46
  84. package/src/services/browser-service/internal/browser-session/viewport.js +215 -215
  85. package/src/services/browser-service/internal/container-matcher.js +851 -851
  86. package/src/services/browser-service/internal/container-registry.js +182 -182
  87. package/src/services/browser-service/internal/engine-manager.js +259 -259
  88. package/src/services/browser-service/internal/fingerprint.js +203 -203
  89. package/src/services/browser-service/internal/heartbeat.js +137 -137
  90. package/src/services/browser-service/internal/logging.js +46 -46
  91. package/src/services/browser-service/internal/page-runtime/runtime.js +1317 -1317
  92. package/src/services/browser-service/internal/pageRuntime.js +28 -28
  93. package/src/services/browser-service/internal/runtimeInjector.js +31 -31
  94. package/src/services/browser-service/internal/service-process-logger.js +140 -140
  95. package/src/services/browser-service/internal/state-bus.js +45 -45
  96. package/src/services/browser-service/internal/storage-paths.js +42 -42
  97. package/src/services/browser-service/internal/ws-server.js +1194 -1194
  98. package/src/services/browser-service/internal/ws-server.test.js +58 -58
  99. package/src/services/browser-service/server.mjs +6 -6
  100. package/src/services/controller/cli-bridge.js +93 -93
  101. package/src/services/controller/container-index.js +50 -50
  102. package/src/services/controller/container-storage.js +36 -36
  103. package/src/services/controller/controller-actions.js +207 -207
  104. package/src/services/controller/controller.js +1138 -1138
  105. package/src/services/controller/selectors.js +54 -54
  106. package/src/services/controller/transport.js +125 -125
  107. package/src/utils/args.mjs +26 -26
  108. package/src/utils/browser-service.mjs +544 -544
  109. package/src/utils/command-log.mjs +64 -64
  110. package/src/utils/config.mjs +214 -214
  111. package/src/utils/fingerprint.mjs +181 -181
  112. package/src/utils/help.mjs +216 -216
  113. package/src/utils/js-policy.mjs +13 -13
  114. package/src/utils/ws-client.mjs +30 -30
@@ -1,277 +1,277 @@
1
- function asBoolLiteral(value) {
2
- return value ? 'true' : 'false';
3
- }
4
-
5
- function buildVisibleElementResolverLiteral(selector) {
6
- return `
7
- const selector = ${JSON.stringify(selector)};
8
- const candidates = Array.from(document.querySelectorAll(selector));
9
- const isVisible = (node) => {
10
- if (!(node instanceof Element)) return false;
11
- const rect = node.getBoundingClientRect?.();
12
- if (!rect || rect.width <= 0 || rect.height <= 0) return false;
13
- try {
14
- const style = window.getComputedStyle(node);
15
- if (!style) return false;
16
- if (style.display === 'none') return false;
17
- if (style.visibility === 'hidden' || style.visibility === 'collapse') return false;
18
- const opacity = Number.parseFloat(String(style.opacity || '1'));
19
- if (Number.isFinite(opacity) && opacity <= 0.01) return false;
20
- } catch {
21
- return false;
22
- }
23
- return true;
24
- };
25
- const hitVisible = (node) => {
26
- if (!(node instanceof Element)) return false;
27
- const rect = node.getBoundingClientRect?.();
28
- if (!rect) return false;
29
- const x = Math.max(0, Math.min((window.innerWidth || 1) - 1, rect.left + rect.width / 2));
30
- const y = Math.max(0, Math.min((window.innerHeight || 1) - 1, rect.top + rect.height / 2));
31
- const top = document.elementFromPoint(x, y);
32
- if (!top) return false;
33
- return top === node || node.contains(top) || top.contains(node);
34
- };
35
- const pick = () => {
36
- if (!candidates.length) return null;
37
- const strict = candidates.find((node) => isVisible(node) && hitVisible(node));
38
- if (strict) return strict;
39
- const visible = candidates.find((node) => isVisible(node));
40
- if (visible) return visible;
41
- return candidates[0] || null;
42
- };
43
- const el = pick();
44
- if (!el) throw new Error('Element not found: ' + selector);
45
- const matchedIndex = Math.max(0, candidates.indexOf(el));
46
- `;
47
- }
48
-
49
- export function buildSelectorScrollIntoViewScript({ selector, highlight }) {
50
- const highlightLiteral = asBoolLiteral(highlight);
51
- return `(async () => {
52
- ${buildVisibleElementResolverLiteral(selector)}
53
- const restoreOutline = el instanceof HTMLElement ? el.style.outline : '';
54
- if (${highlightLiteral} && el instanceof HTMLElement) {
55
- el.style.outline = '2px solid #ff4d4f';
56
- }
57
- el.scrollIntoView({ behavior: 'auto', block: 'center', inline: 'nearest' });
58
- await new Promise((r) => setTimeout(r, 120));
59
- if (${highlightLiteral} && el instanceof HTMLElement) {
60
- el.style.outline = restoreOutline;
61
- }
62
- return {
63
- ok: true,
64
- selector,
65
- matchedIndex,
66
- action: 'scrollIntoView',
67
- highlight: ${highlightLiteral},
68
- target: { tag: String(el.tagName || '').toLowerCase(), id: el.id || null }
69
- };
70
- })()`;
71
- }
72
-
73
- export function buildSelectorClickScript({ selector, highlight }) {
74
- const highlightLiteral = asBoolLiteral(highlight);
75
- return `(async () => {
76
- ${buildVisibleElementResolverLiteral(selector)}
77
- const restoreOutline = el instanceof HTMLElement ? el.style.outline : '';
78
- if (${highlightLiteral} && el instanceof HTMLElement) {
79
- el.style.outline = '2px solid #ff4d4f';
80
- }
81
- el.scrollIntoView({ behavior: 'smooth', block: 'center' });
82
- await new Promise((r) => setTimeout(r, 150));
83
- if (el instanceof HTMLElement && typeof el.click === 'function') {
84
- el.click();
85
- } else {
86
- throw new Error('Target element is not clickable');
87
- }
88
- if (${highlightLiteral} && el instanceof HTMLElement) {
89
- setTimeout(() => { el.style.outline = restoreOutline; }, 260);
90
- }
91
- return {
92
- ok: true,
93
- selector,
94
- matchedIndex,
95
- action: 'click',
96
- highlight: ${highlightLiteral},
97
- target: { tag: String(el.tagName || '').toLowerCase(), id: el.id || null }
98
- };
99
- })()`;
100
- }
101
-
102
- export function buildSelectorTypeScript({ selector, highlight, text }) {
103
- const highlightLiteral = asBoolLiteral(highlight);
104
- const textLiteral = JSON.stringify(String(text || ''));
105
- const textLength = String(text || '').length;
106
-
107
- return `(async () => {
108
- ${buildVisibleElementResolverLiteral(selector)}
109
- const isTypeable = (node) => {
110
- if (!node) return false;
111
- if (node instanceof HTMLInputElement) return !node.disabled && !node.readOnly;
112
- if (node instanceof HTMLTextAreaElement) return !node.disabled && !node.readOnly;
113
- return node instanceof HTMLElement && node.isContentEditable;
114
- };
115
- if (!isTypeable(el)) {
116
- throw new Error('Target element is not typeable: ' + selector);
117
- }
118
- const restoreOutline = el instanceof HTMLElement ? el.style.outline : '';
119
- if (${highlightLiteral} && el instanceof HTMLElement) {
120
- el.style.outline = '2px solid #ff4d4f';
121
- }
122
- el.scrollIntoView({ behavior: 'smooth', block: 'center' });
123
- await new Promise((r) => setTimeout(r, 150));
124
- if (el instanceof HTMLElement && typeof el.focus === 'function') {
125
- el.focus();
126
- }
127
-
128
- if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
129
- const prototype = el instanceof HTMLTextAreaElement
130
- ? window.HTMLTextAreaElement.prototype
131
- : window.HTMLInputElement.prototype;
132
- const valueSetter = Object.getOwnPropertyDescriptor(prototype, 'value')?.set;
133
- if (typeof valueSetter === 'function') {
134
- valueSetter.call(el, ${textLiteral});
135
- } else {
136
- el.value = ${textLiteral};
137
- }
138
- } else if (el instanceof HTMLElement && el.isContentEditable) {
139
- el.textContent = ${textLiteral};
140
- }
141
-
142
- const inputEvent = typeof InputEvent === 'function'
143
- ? new InputEvent('input', { bubbles: true, cancelable: true, data: ${textLiteral}, inputType: 'insertText' })
144
- : new Event('input', { bubbles: true, cancelable: true });
145
- el.dispatchEvent(inputEvent);
146
- el.dispatchEvent(new Event('change', { bubbles: true }));
147
- if (${highlightLiteral} && el instanceof HTMLElement) {
148
- setTimeout(() => { el.style.outline = restoreOutline; }, 260);
149
- }
150
- return {
151
- ok: true,
152
- selector,
153
- matchedIndex,
154
- action: 'type',
155
- length: ${textLength},
156
- highlight: ${highlightLiteral},
157
- target: { tag: String(el.tagName || '').toLowerCase(), id: el.id || null }
158
- };
159
- })()`;
160
- }
161
-
162
- export function buildScrollTargetScript({ selector, highlight, requireVisibleContainer = false }) {
163
- const selectorLiteral = JSON.stringify(String(selector || '').trim() || null);
164
- const highlightLiteral = asBoolLiteral(highlight);
165
- const requireVisibleContainerLiteral = asBoolLiteral(requireVisibleContainer);
166
- return `(() => {
167
- const selector = ${selectorLiteral};
168
- const requireVisibleContainer = ${requireVisibleContainerLiteral};
169
- const isVisible = (node) => {
170
- if (!(node instanceof Element)) return false;
171
- const rect = node.getBoundingClientRect?.();
172
- if (!rect || rect.width <= 0 || rect.height <= 0) return false;
173
- try {
174
- const style = window.getComputedStyle(node);
175
- if (!style) return false;
176
- if (style.display === 'none') return false;
177
- if (style.visibility === 'hidden' || style.visibility === 'collapse') return false;
178
- } catch {
179
- return false;
180
- }
181
- return true;
182
- };
183
- const isScrollable = (node) => {
184
- if (!(node instanceof Element)) return false;
185
- const style = window.getComputedStyle(node);
186
- const overflowY = String(style.overflowY || '');
187
- const overflowX = String(style.overflowX || '');
188
- const scrollableSelectors = ['.comments-container', '.comment-list', '.comments-el', '.note-scroller'];
189
- const selectorScrollable = scrollableSelectors.some((sel) => typeof node.matches === 'function' && node.matches(sel));
190
- const yScrollable = (overflowY.includes('auto') || overflowY.includes('scroll') || overflowY.includes('overlay'))
191
- && (node.scrollHeight - node.clientHeight > 2);
192
- const xScrollable = (overflowX.includes('auto') || overflowX.includes('scroll') || overflowX.includes('overlay'))
193
- && (node.scrollWidth - node.clientWidth > 2);
194
- return yScrollable || xScrollable || selectorScrollable;
195
- };
196
- const findScrollableAncestor = (node) => {
197
- let cursor = node instanceof Element ? node : null;
198
- while (cursor) {
199
- if (isVisible(cursor) && isScrollable(cursor)) return cursor;
200
- cursor = cursor.parentElement;
201
- }
202
- return null;
203
- };
204
-
205
- let target = null;
206
- let source = 'document';
207
- if (selector) {
208
- const list = Array.from(document.querySelectorAll(selector));
209
- target = list.find((node) => isVisible(node) && isScrollable(node))
210
- || list.map((node) => findScrollableAncestor(node)).find((node) => isVisible(node))
211
- || null;
212
- if (target) source = 'selector';
213
- if (!target && requireVisibleContainer) {
214
- return { ok: false, error: 'visible_scroll_container_not_found', selector };
215
- }
216
- }
217
- if (!target) {
218
- const active = document.activeElement instanceof Element ? document.activeElement : null;
219
- target = findScrollableAncestor(active);
220
- if (target) source = 'active';
221
- }
222
- if (!target) {
223
- const cx = Math.max(1, Math.floor((window.innerWidth || 1) / 2));
224
- const cy = Math.max(1, Math.floor((window.innerHeight || 1) / 2));
225
- const point = document.elementFromPoint(cx, cy);
226
- target = findScrollableAncestor(point);
227
- if (target) source = 'center';
228
- }
229
- if (!target) {
230
- target = document.scrollingElement || document.documentElement || document.body;
231
- source = 'document';
232
- }
233
- if (!target) {
234
- throw new Error('No scroll target available');
235
- }
236
-
237
- const rect = target.getBoundingClientRect?.() || {
238
- left: 0,
239
- top: 0,
240
- width: window.innerWidth || 1,
241
- height: window.innerHeight || 1,
242
- };
243
- const centerX = Math.max(1, Math.min((window.innerWidth || 1) - 1, Math.round(rect.left + Math.max(1, rect.width / 2))));
244
- const centerY = Math.max(1, Math.min((window.innerHeight || 1) - 1, Math.round(rect.top + Math.max(1, rect.height / 2))));
245
-
246
- const restoreOutline = target instanceof HTMLElement ? target.style.outline : '';
247
- if (${highlightLiteral} && target instanceof HTMLElement) {
248
- target.style.outline = '2px solid #ff4d4f';
249
- setTimeout(() => {
250
- target.style.outline = restoreOutline;
251
- }, 320);
252
- }
253
-
254
- return {
255
- ok: true,
256
- selector,
257
- source,
258
- highlight: ${highlightLiteral},
259
- center: { x: centerX, y: centerY },
260
- rect: {
261
- left: Number(rect.left || 0),
262
- top: Number(rect.top || 0),
263
- width: Number(rect.width || 0),
264
- height: Number(rect.height || 0)
265
- },
266
- target: {
267
- tag: String(target.tagName || '').toLowerCase(),
268
- id: target.id || null,
269
- className: typeof target.className === 'string' ? target.className : null,
270
- scrollHeight: Number(target.scrollHeight || 0),
271
- clientHeight: Number(target.clientHeight || 0),
272
- scrollWidth: Number(target.scrollWidth || 0),
273
- clientWidth: Number(target.clientWidth || 0)
274
- }
275
- };
276
- })()`;
277
- }
1
+ function asBoolLiteral(value) {
2
+ return value ? 'true' : 'false';
3
+ }
4
+
5
+ function buildVisibleElementResolverLiteral(selector) {
6
+ return `
7
+ const selector = ${JSON.stringify(selector)};
8
+ const candidates = Array.from(document.querySelectorAll(selector));
9
+ const isVisible = (node) => {
10
+ if (!(node instanceof Element)) return false;
11
+ const rect = node.getBoundingClientRect?.();
12
+ if (!rect || rect.width <= 0 || rect.height <= 0) return false;
13
+ try {
14
+ const style = window.getComputedStyle(node);
15
+ if (!style) return false;
16
+ if (style.display === 'none') return false;
17
+ if (style.visibility === 'hidden' || style.visibility === 'collapse') return false;
18
+ const opacity = Number.parseFloat(String(style.opacity || '1'));
19
+ if (Number.isFinite(opacity) && opacity <= 0.01) return false;
20
+ } catch {
21
+ return false;
22
+ }
23
+ return true;
24
+ };
25
+ const hitVisible = (node) => {
26
+ if (!(node instanceof Element)) return false;
27
+ const rect = node.getBoundingClientRect?.();
28
+ if (!rect) return false;
29
+ const x = Math.max(0, Math.min((window.innerWidth || 1) - 1, rect.left + rect.width / 2));
30
+ const y = Math.max(0, Math.min((window.innerHeight || 1) - 1, rect.top + rect.height / 2));
31
+ const top = document.elementFromPoint(x, y);
32
+ if (!top) return false;
33
+ return top === node || node.contains(top) || top.contains(node);
34
+ };
35
+ const pick = () => {
36
+ if (!candidates.length) return null;
37
+ const strict = candidates.find((node) => isVisible(node) && hitVisible(node));
38
+ if (strict) return strict;
39
+ const visible = candidates.find((node) => isVisible(node));
40
+ if (visible) return visible;
41
+ return candidates[0] || null;
42
+ };
43
+ const el = pick();
44
+ if (!el) throw new Error('Element not found: ' + selector);
45
+ const matchedIndex = Math.max(0, candidates.indexOf(el));
46
+ `;
47
+ }
48
+
49
+ export function buildSelectorScrollIntoViewScript({ selector, highlight }) {
50
+ const highlightLiteral = asBoolLiteral(highlight);
51
+ return `(async () => {
52
+ ${buildVisibleElementResolverLiteral(selector)}
53
+ const restoreOutline = el instanceof HTMLElement ? el.style.outline : '';
54
+ if (${highlightLiteral} && el instanceof HTMLElement) {
55
+ el.style.outline = '2px solid #ff4d4f';
56
+ }
57
+ el.scrollIntoView({ behavior: 'auto', block: 'center', inline: 'nearest' });
58
+ await new Promise((r) => setTimeout(r, 120));
59
+ if (${highlightLiteral} && el instanceof HTMLElement) {
60
+ el.style.outline = restoreOutline;
61
+ }
62
+ return {
63
+ ok: true,
64
+ selector,
65
+ matchedIndex,
66
+ action: 'scrollIntoView',
67
+ highlight: ${highlightLiteral},
68
+ target: { tag: String(el.tagName || '').toLowerCase(), id: el.id || null }
69
+ };
70
+ })()`;
71
+ }
72
+
73
+ export function buildSelectorClickScript({ selector, highlight }) {
74
+ const highlightLiteral = asBoolLiteral(highlight);
75
+ return `(async () => {
76
+ ${buildVisibleElementResolverLiteral(selector)}
77
+ const restoreOutline = el instanceof HTMLElement ? el.style.outline : '';
78
+ if (${highlightLiteral} && el instanceof HTMLElement) {
79
+ el.style.outline = '2px solid #ff4d4f';
80
+ }
81
+ el.scrollIntoView({ behavior: 'smooth', block: 'center' });
82
+ await new Promise((r) => setTimeout(r, 150));
83
+ if (el instanceof HTMLElement && typeof el.click === 'function') {
84
+ el.click();
85
+ } else {
86
+ throw new Error('Target element is not clickable');
87
+ }
88
+ if (${highlightLiteral} && el instanceof HTMLElement) {
89
+ setTimeout(() => { el.style.outline = restoreOutline; }, 260);
90
+ }
91
+ return {
92
+ ok: true,
93
+ selector,
94
+ matchedIndex,
95
+ action: 'click',
96
+ highlight: ${highlightLiteral},
97
+ target: { tag: String(el.tagName || '').toLowerCase(), id: el.id || null }
98
+ };
99
+ })()`;
100
+ }
101
+
102
+ export function buildSelectorTypeScript({ selector, highlight, text }) {
103
+ const highlightLiteral = asBoolLiteral(highlight);
104
+ const textLiteral = JSON.stringify(String(text || ''));
105
+ const textLength = String(text || '').length;
106
+
107
+ return `(async () => {
108
+ ${buildVisibleElementResolverLiteral(selector)}
109
+ const isTypeable = (node) => {
110
+ if (!node) return false;
111
+ if (node instanceof HTMLInputElement) return !node.disabled && !node.readOnly;
112
+ if (node instanceof HTMLTextAreaElement) return !node.disabled && !node.readOnly;
113
+ return node instanceof HTMLElement && node.isContentEditable;
114
+ };
115
+ if (!isTypeable(el)) {
116
+ throw new Error('Target element is not typeable: ' + selector);
117
+ }
118
+ const restoreOutline = el instanceof HTMLElement ? el.style.outline : '';
119
+ if (${highlightLiteral} && el instanceof HTMLElement) {
120
+ el.style.outline = '2px solid #ff4d4f';
121
+ }
122
+ el.scrollIntoView({ behavior: 'smooth', block: 'center' });
123
+ await new Promise((r) => setTimeout(r, 150));
124
+ if (el instanceof HTMLElement && typeof el.focus === 'function') {
125
+ el.focus();
126
+ }
127
+
128
+ if (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement) {
129
+ const prototype = el instanceof HTMLTextAreaElement
130
+ ? window.HTMLTextAreaElement.prototype
131
+ : window.HTMLInputElement.prototype;
132
+ const valueSetter = Object.getOwnPropertyDescriptor(prototype, 'value')?.set;
133
+ if (typeof valueSetter === 'function') {
134
+ valueSetter.call(el, ${textLiteral});
135
+ } else {
136
+ el.value = ${textLiteral};
137
+ }
138
+ } else if (el instanceof HTMLElement && el.isContentEditable) {
139
+ el.textContent = ${textLiteral};
140
+ }
141
+
142
+ const inputEvent = typeof InputEvent === 'function'
143
+ ? new InputEvent('input', { bubbles: true, cancelable: true, data: ${textLiteral}, inputType: 'insertText' })
144
+ : new Event('input', { bubbles: true, cancelable: true });
145
+ el.dispatchEvent(inputEvent);
146
+ el.dispatchEvent(new Event('change', { bubbles: true }));
147
+ if (${highlightLiteral} && el instanceof HTMLElement) {
148
+ setTimeout(() => { el.style.outline = restoreOutline; }, 260);
149
+ }
150
+ return {
151
+ ok: true,
152
+ selector,
153
+ matchedIndex,
154
+ action: 'type',
155
+ length: ${textLength},
156
+ highlight: ${highlightLiteral},
157
+ target: { tag: String(el.tagName || '').toLowerCase(), id: el.id || null }
158
+ };
159
+ })()`;
160
+ }
161
+
162
+ export function buildScrollTargetScript({ selector, highlight, requireVisibleContainer = false }) {
163
+ const selectorLiteral = JSON.stringify(String(selector || '').trim() || null);
164
+ const highlightLiteral = asBoolLiteral(highlight);
165
+ const requireVisibleContainerLiteral = asBoolLiteral(requireVisibleContainer);
166
+ return `(() => {
167
+ const selector = ${selectorLiteral};
168
+ const requireVisibleContainer = ${requireVisibleContainerLiteral};
169
+ const isVisible = (node) => {
170
+ if (!(node instanceof Element)) return false;
171
+ const rect = node.getBoundingClientRect?.();
172
+ if (!rect || rect.width <= 0 || rect.height <= 0) return false;
173
+ try {
174
+ const style = window.getComputedStyle(node);
175
+ if (!style) return false;
176
+ if (style.display === 'none') return false;
177
+ if (style.visibility === 'hidden' || style.visibility === 'collapse') return false;
178
+ } catch {
179
+ return false;
180
+ }
181
+ return true;
182
+ };
183
+ const isScrollable = (node) => {
184
+ if (!(node instanceof Element)) return false;
185
+ const style = window.getComputedStyle(node);
186
+ const overflowY = String(style.overflowY || '');
187
+ const overflowX = String(style.overflowX || '');
188
+ const scrollableSelectors = ['.comments-container', '.comment-list', '.comments-el', '.note-scroller'];
189
+ const selectorScrollable = scrollableSelectors.some((sel) => typeof node.matches === 'function' && node.matches(sel));
190
+ const yScrollable = (overflowY.includes('auto') || overflowY.includes('scroll') || overflowY.includes('overlay'))
191
+ && (node.scrollHeight - node.clientHeight > 2);
192
+ const xScrollable = (overflowX.includes('auto') || overflowX.includes('scroll') || overflowX.includes('overlay'))
193
+ && (node.scrollWidth - node.clientWidth > 2);
194
+ return yScrollable || xScrollable || selectorScrollable;
195
+ };
196
+ const findScrollableAncestor = (node) => {
197
+ let cursor = node instanceof Element ? node : null;
198
+ while (cursor) {
199
+ if (isVisible(cursor) && isScrollable(cursor)) return cursor;
200
+ cursor = cursor.parentElement;
201
+ }
202
+ return null;
203
+ };
204
+
205
+ let target = null;
206
+ let source = 'document';
207
+ if (selector) {
208
+ const list = Array.from(document.querySelectorAll(selector));
209
+ target = list.find((node) => isVisible(node) && isScrollable(node))
210
+ || list.map((node) => findScrollableAncestor(node)).find((node) => isVisible(node))
211
+ || null;
212
+ if (target) source = 'selector';
213
+ if (!target && requireVisibleContainer) {
214
+ return { ok: false, error: 'visible_scroll_container_not_found', selector };
215
+ }
216
+ }
217
+ if (!target) {
218
+ const active = document.activeElement instanceof Element ? document.activeElement : null;
219
+ target = findScrollableAncestor(active);
220
+ if (target) source = 'active';
221
+ }
222
+ if (!target) {
223
+ const cx = Math.max(1, Math.floor((window.innerWidth || 1) / 2));
224
+ const cy = Math.max(1, Math.floor((window.innerHeight || 1) / 2));
225
+ const point = document.elementFromPoint(cx, cy);
226
+ target = findScrollableAncestor(point);
227
+ if (target) source = 'center';
228
+ }
229
+ if (!target) {
230
+ target = document.scrollingElement || document.documentElement || document.body;
231
+ source = 'document';
232
+ }
233
+ if (!target) {
234
+ throw new Error('No scroll target available');
235
+ }
236
+
237
+ const rect = target.getBoundingClientRect?.() || {
238
+ left: 0,
239
+ top: 0,
240
+ width: window.innerWidth || 1,
241
+ height: window.innerHeight || 1,
242
+ };
243
+ const centerX = Math.max(1, Math.min((window.innerWidth || 1) - 1, Math.round(rect.left + Math.max(1, rect.width / 2))));
244
+ const centerY = Math.max(1, Math.min((window.innerHeight || 1) - 1, Math.round(rect.top + Math.max(1, rect.height / 2))));
245
+
246
+ const restoreOutline = target instanceof HTMLElement ? target.style.outline : '';
247
+ if (${highlightLiteral} && target instanceof HTMLElement) {
248
+ target.style.outline = '2px solid #ff4d4f';
249
+ setTimeout(() => {
250
+ target.style.outline = restoreOutline;
251
+ }, 320);
252
+ }
253
+
254
+ return {
255
+ ok: true,
256
+ selector,
257
+ source,
258
+ highlight: ${highlightLiteral},
259
+ center: { x: centerX, y: centerY },
260
+ rect: {
261
+ left: Number(rect.left || 0),
262
+ top: Number(rect.top || 0),
263
+ width: Number(rect.width || 0),
264
+ height: Number(rect.height || 0)
265
+ },
266
+ target: {
267
+ tag: String(target.tagName || '').toLowerCase(),
268
+ id: target.id || null,
269
+ className: typeof target.className === 'string' ? target.className : null,
270
+ scrollHeight: Number(target.scrollHeight || 0),
271
+ clientHeight: Number(target.clientHeight || 0),
272
+ scrollWidth: Number(target.scrollWidth || 0),
273
+ clientWidth: Number(target.clientWidth || 0)
274
+ }
275
+ };
276
+ })()`;
277
+ }