codex-browser-use 4.8.2 → 4.9.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/README.md +5 -5
- package/bin/browser-use.js +67 -11
- package/extension/background.js +425 -139
- package/extension/manifest.json +2 -10
- package/package.json +2 -2
- package/rules/browser_use.md +6 -20
- package/skill/SKILL.md +9 -8
- package/skill/references/cookbook.md +3 -3
- package/src/ax-tree.js +63 -35
- package/src/backend-discovery.js +15 -8
- package/src/backend-native-driver.js +38 -12
- package/src/backend-tabs.js +93 -7
- package/src/backend-transport.js +3 -3
- package/src/bridge-browser.js +420 -123
- package/src/config.js +44 -11
- package/src/content-extractor.js +53 -19
- package/src/extension.js +6 -1
- package/src/install.js +243 -19
- package/src/locators.js +259 -36
- package/src/read-only.js +254 -52
- package/src/script-runner.js +243 -63
- package/src/server.js +84 -30
package/src/script-runner.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import vm from "node:vm";
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
2
3
|
import { mkdir, readFile as fsReadFile, writeFile as fsWriteFile } from "node:fs/promises";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
import { URL, URLSearchParams } from "node:url";
|
|
6
|
+
import { TextDecoder, TextEncoder } from "node:util";
|
|
5
7
|
import { parse } from "acorn";
|
|
6
8
|
|
|
7
9
|
const RESERVED_PARAM_NAMES = new Set([
|
|
@@ -77,6 +79,11 @@ function collectDeclaredIdentifiers(pattern, out) {
|
|
|
77
79
|
}
|
|
78
80
|
}
|
|
79
81
|
|
|
82
|
+
const TRANSFORM_CACHE_MAX = 128;
|
|
83
|
+
/** @type {Map<string, {transformed:string, declaredNames:string[]}>} */
|
|
84
|
+
const transformCache = new Map();
|
|
85
|
+
const DECL_KEYWORD_RE = /\b(?:const|let|var|class|function)\b/;
|
|
86
|
+
|
|
80
87
|
/**
|
|
81
88
|
* Rewrite top-level `const`/`let`/`class` declarations to `var` so bindings persist
|
|
82
89
|
* across `browser_run` calls and can be redeclared safely across turns.
|
|
@@ -85,6 +92,15 @@ function collectDeclaredIdentifiers(pattern, out) {
|
|
|
85
92
|
*/
|
|
86
93
|
export function transformPersistentCode(code) {
|
|
87
94
|
const source = String(code ?? "");
|
|
95
|
+
if (!DECL_KEYWORD_RE.test(source)) {
|
|
96
|
+
return { transformed: source, declaredNames: [] };
|
|
97
|
+
}
|
|
98
|
+
const cached = transformCache.get(source);
|
|
99
|
+
if (cached) {
|
|
100
|
+
transformCache.delete(source);
|
|
101
|
+
transformCache.set(source, cached);
|
|
102
|
+
return cached;
|
|
103
|
+
}
|
|
88
104
|
const tree = parse(source, {
|
|
89
105
|
ecmaVersion: "latest",
|
|
90
106
|
sourceType: "script",
|
|
@@ -114,10 +130,15 @@ export function transformPersistentCode(code) {
|
|
|
114
130
|
transformed = transformed.slice(0, edit.start) + edit.replacement + transformed.slice(edit.end);
|
|
115
131
|
}
|
|
116
132
|
const declaredNames = [...names].filter((n) => IDENTIFIER_RE.test(n) && !RESERVED_PARAM_NAMES.has(n));
|
|
117
|
-
|
|
133
|
+
const out = { transformed, declaredNames };
|
|
134
|
+
if (transformCache.size >= TRANSFORM_CACHE_MAX) {
|
|
135
|
+
transformCache.delete(transformCache.keys().next().value);
|
|
136
|
+
}
|
|
137
|
+
transformCache.set(source, out);
|
|
138
|
+
return out;
|
|
118
139
|
}
|
|
119
140
|
|
|
120
|
-
/** @param {string} role @param {{name?:string|RegExp, exact?:boolean}} [opts] @returns {string} */
|
|
141
|
+
/** @param {string} role @param {{name?:string|RegExp, exact?:boolean, level?:number, checked?:boolean, selected?:boolean, expanded?:boolean, pressed?:boolean, disabled?:boolean}} [opts] @returns {string} */
|
|
121
142
|
function buildRoleStep(role, opts = {}) {
|
|
122
143
|
let step = `role=${String(role)}`;
|
|
123
144
|
if (opts?.name instanceof RegExp) {
|
|
@@ -125,6 +146,14 @@ function buildRoleStep(role, opts = {}) {
|
|
|
125
146
|
} else if (opts?.name !== undefined) {
|
|
126
147
|
step += `[name=${JSON.stringify(String(opts.name))}][exact=${opts.exact ? "true" : "false"}]`;
|
|
127
148
|
}
|
|
149
|
+
if (Number.isInteger(opts?.level)) {
|
|
150
|
+
step += `[level=${opts.level}]`;
|
|
151
|
+
}
|
|
152
|
+
for (const attr of ["checked", "selected", "expanded", "pressed", "disabled"]) {
|
|
153
|
+
if (typeof opts?.[attr] === "boolean") {
|
|
154
|
+
step += `[${attr}=${opts[attr] ? "true" : "false"}]`;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
128
157
|
return step;
|
|
129
158
|
}
|
|
130
159
|
|
|
@@ -132,7 +161,16 @@ function buildRoleStep(role, opts = {}) {
|
|
|
132
161
|
function buildTextStep(text, opts = {}) {
|
|
133
162
|
if (text instanceof RegExp) return `text=/${text.source}/${text.flags}`;
|
|
134
163
|
const str = String(text);
|
|
135
|
-
return opts?.exact ? `text=${JSON.stringify(str)}` : `text=${str}`;
|
|
164
|
+
return opts?.exact ? `text=${JSON.stringify(str)}` : `text=${JSON.stringify(str)}[exact=false]`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** @param {string} kind @param {string|RegExp} value @param {{exact?:boolean}} [opts] @returns {string} */
|
|
168
|
+
function buildNamedMatchStep(kind, value, opts = {}) {
|
|
169
|
+
if (value instanceof RegExp) return `${kind}=/${value.source}/${value.flags}`;
|
|
170
|
+
const str = String(value);
|
|
171
|
+
return opts?.exact === true
|
|
172
|
+
? `${kind}=${JSON.stringify(str)}[exact=true]`
|
|
173
|
+
: `${kind}=${JSON.stringify(str)}[exact=false]`;
|
|
136
174
|
}
|
|
137
175
|
|
|
138
176
|
/** @param {unknown} value @returns {boolean} */
|
|
@@ -147,14 +185,16 @@ function hasOnlyKeys(obj, allowedKeys) {
|
|
|
147
185
|
return keys.length > 0 && keys.every((k) => allowedKeys.has(k));
|
|
148
186
|
}
|
|
149
187
|
|
|
150
|
-
const PAGE_EVAL_OPT_KEYS = new Set(["tabId", "tab_id", "timeoutMs", "timeout_ms"]);
|
|
151
|
-
const LOCATOR_EVAL_OPT_KEYS = new Set(["tabId", "tab_id", "frame", "timeoutMs", "timeout_ms"]);
|
|
188
|
+
const PAGE_EVAL_OPT_KEYS = new Set(["tabId", "tab_id", "timeout", "timeoutMs", "timeout_ms"]);
|
|
189
|
+
const LOCATOR_EVAL_OPT_KEYS = new Set(["tabId", "tab_id", "frame", "timeout", "timeoutMs", "timeout_ms"]);
|
|
152
190
|
|
|
153
191
|
/**
|
|
154
192
|
* Persistent VM session for `browser_run`, retaining top-level bindings and active tab
|
|
155
193
|
* state across turns while isolating host globals and enforcing read-only page evaluation.
|
|
156
194
|
*/
|
|
157
195
|
export class BrowserScriptSession {
|
|
196
|
+
#runQueue = Promise.resolve();
|
|
197
|
+
|
|
158
198
|
constructor() {
|
|
159
199
|
this.reset();
|
|
160
200
|
}
|
|
@@ -163,8 +203,53 @@ export class BrowserScriptSession {
|
|
|
163
203
|
this.scope = Object.create(null);
|
|
164
204
|
this.currentTabId = null;
|
|
165
205
|
this.activeRun = null;
|
|
206
|
+
this.#runQueue = Promise.resolve();
|
|
207
|
+
const vmSetTimeout = (fn, ms, ...args) => {
|
|
208
|
+
const run = this.activeRun;
|
|
209
|
+
const timer = setTimeout((...cbArgs) => {
|
|
210
|
+
run?.activeTimers?.delete(timer);
|
|
211
|
+
if (typeof fn === "function") fn(...cbArgs);
|
|
212
|
+
}, ms, ...args);
|
|
213
|
+
run?.activeTimers?.add(timer);
|
|
214
|
+
return timer;
|
|
215
|
+
};
|
|
216
|
+
const vmClearTimeout = (timer) => {
|
|
217
|
+
this.activeRun?.activeTimers?.delete(timer);
|
|
218
|
+
clearTimeout(timer);
|
|
219
|
+
};
|
|
220
|
+
const vmSetInterval = (fn, ms, ...args) => {
|
|
221
|
+
const run = this.activeRun;
|
|
222
|
+
const timer = setInterval((...cbArgs) => {
|
|
223
|
+
if (run?.aborted) {
|
|
224
|
+
clearInterval(timer);
|
|
225
|
+
run.activeIntervals?.delete(timer);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (typeof fn === "function") fn(...cbArgs);
|
|
229
|
+
}, ms, ...args);
|
|
230
|
+
run?.activeIntervals?.add(timer);
|
|
231
|
+
return timer;
|
|
232
|
+
};
|
|
233
|
+
const vmClearInterval = (timer) => {
|
|
234
|
+
this.activeRun?.activeIntervals?.delete(timer);
|
|
235
|
+
clearInterval(timer);
|
|
236
|
+
};
|
|
166
237
|
this.context = vm.createContext(
|
|
167
|
-
{
|
|
238
|
+
{
|
|
239
|
+
URL,
|
|
240
|
+
URLSearchParams,
|
|
241
|
+
Buffer,
|
|
242
|
+
TextEncoder,
|
|
243
|
+
TextDecoder,
|
|
244
|
+
atob: globalThis.atob,
|
|
245
|
+
btoa: globalThis.btoa,
|
|
246
|
+
crypto: globalThis.crypto,
|
|
247
|
+
structuredClone: globalThis.structuredClone,
|
|
248
|
+
setTimeout: vmSetTimeout,
|
|
249
|
+
clearTimeout: vmClearTimeout,
|
|
250
|
+
setInterval: vmSetInterval,
|
|
251
|
+
clearInterval: vmClearInterval,
|
|
252
|
+
},
|
|
168
253
|
{ codeGeneration: { strings: false, wasm: false } },
|
|
169
254
|
);
|
|
170
255
|
}
|
|
@@ -224,21 +309,31 @@ export class BrowserScriptSession {
|
|
|
224
309
|
return this.currentTabId ?? (browser?.activeTabId != null ? String(browser.activeTabId) : null);
|
|
225
310
|
}
|
|
226
311
|
|
|
227
|
-
|
|
312
|
+
#trackPending(run, promise) {
|
|
313
|
+
run.pendingPromises?.add(promise);
|
|
314
|
+
const cleanup = () => run.pendingPromises?.delete(promise);
|
|
315
|
+
promise.then(cleanup, cleanup);
|
|
316
|
+
return promise;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
#guard(fn, label = null, { trackActiveTab = true } = {}) {
|
|
228
320
|
const run = this.#requireRun();
|
|
229
321
|
if (label) run.lastAction = label;
|
|
230
|
-
const
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
322
|
+
const promise = (async () => {
|
|
323
|
+
const res = await fn(run.browser);
|
|
324
|
+
if (run.aborted || run !== this.activeRun) {
|
|
325
|
+
throw new Error(this.#timeoutMessage(run));
|
|
326
|
+
}
|
|
327
|
+
if (res && typeof res === "object" && res.tab_id != null) {
|
|
328
|
+
const tid = String(res.tab_id);
|
|
329
|
+
run.touchedTabs.add(tid);
|
|
330
|
+
if (trackActiveTab) this.currentTabId = tid;
|
|
331
|
+
} else if (trackActiveTab && run.browser.activeTabId != null && this.currentTabId === null) {
|
|
332
|
+
this.currentTabId = String(run.browser.activeTabId);
|
|
333
|
+
}
|
|
334
|
+
return res;
|
|
335
|
+
})();
|
|
336
|
+
return this.#trackPending(run, promise);
|
|
242
337
|
}
|
|
243
338
|
|
|
244
339
|
#log(...args) {
|
|
@@ -253,7 +348,7 @@ export class BrowserScriptSession {
|
|
|
253
348
|
|
|
254
349
|
#sleep(ms) {
|
|
255
350
|
const run = this.#requireRun();
|
|
256
|
-
|
|
351
|
+
const promise = new Promise((resolve, reject) => {
|
|
257
352
|
const waitMs = Math.max(0, Number(ms) || 0);
|
|
258
353
|
const timer = setTimeout(() => {
|
|
259
354
|
run.activeTimers.delete(timer);
|
|
@@ -262,6 +357,7 @@ export class BrowserScriptSession {
|
|
|
262
357
|
}, waitMs);
|
|
263
358
|
run.activeTimers.add(timer);
|
|
264
359
|
});
|
|
360
|
+
return this.#trackPending(run, promise);
|
|
265
361
|
}
|
|
266
362
|
|
|
267
363
|
async #viewImage(captureOrOpts, fixedTabId = null) {
|
|
@@ -456,8 +552,8 @@ export class BrowserScriptSession {
|
|
|
456
552
|
const tabFor = (opts) => this.#resolveTabArg(opts, boundTabId);
|
|
457
553
|
const guardOpts = { trackActiveTab: boundTabId === null };
|
|
458
554
|
const timeoutOpt = (opts) => (
|
|
459
|
-
opts?.timeoutMs !== undefined || opts?.timeout_ms !== undefined
|
|
460
|
-
? this.#clampTimeout(opts?.timeoutMs ?? opts?.timeout_ms)
|
|
555
|
+
opts?.timeout !== undefined || opts?.timeoutMs !== undefined || opts?.timeout_ms !== undefined
|
|
556
|
+
? this.#clampTimeout(opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms)
|
|
461
557
|
: undefined
|
|
462
558
|
);
|
|
463
559
|
|
|
@@ -471,8 +567,10 @@ export class BrowserScriptSession {
|
|
|
471
567
|
},
|
|
472
568
|
getByRole: (role, opts) => this.#createLocator([...chain, buildRoleStep(role, opts)], frame, boundTabId),
|
|
473
569
|
getByText: (text, opts) => this.#createLocator([...chain, buildTextStep(text, opts)], frame, boundTabId),
|
|
474
|
-
getByLabel: (text) => this.#createLocator([...chain,
|
|
475
|
-
getByPlaceholder: (text) => this.#createLocator([...chain,
|
|
570
|
+
getByLabel: (text, opts) => this.#createLocator([...chain, buildNamedMatchStep("label", text, opts)], frame, boundTabId),
|
|
571
|
+
getByPlaceholder: (text, opts) => this.#createLocator([...chain, buildNamedMatchStep("placeholder", text, opts)], frame, boundTabId),
|
|
572
|
+
getByAltText: (text, opts) => this.#createLocator([...chain, buildNamedMatchStep("alt", text, opts)], frame, boundTabId),
|
|
573
|
+
getByTitle: (text, opts) => this.#createLocator([...chain, buildNamedMatchStep("title", text, opts)], frame, boundTabId),
|
|
476
574
|
getByTestId: (id) => this.#createLocator([...chain, `testid=${JSON.stringify(String(id))}`], frame, boundTabId),
|
|
477
575
|
filter: ({ visible, hasText, hasNotText, has, hasNot } = {}) => {
|
|
478
576
|
const steps = [];
|
|
@@ -500,7 +598,7 @@ export class BrowserScriptSession {
|
|
|
500
598
|
modifiers: opts?.modifiers,
|
|
501
599
|
force: opts?.force === true,
|
|
502
600
|
timeoutMs: timeoutOpt(opts),
|
|
503
|
-
settle: opts?.settle ??
|
|
601
|
+
settle: opts?.settle ?? false,
|
|
504
602
|
}), `locator.click(${selector})`, guardOpts),
|
|
505
603
|
dblclick: (opts = {}) => self.click({ ...opts, clickCount: 2 }),
|
|
506
604
|
fill: (value, opts = {}) => this.#guard((b) => b.locatorFill(selector, String(value), {
|
|
@@ -508,21 +606,21 @@ export class BrowserScriptSession {
|
|
|
508
606
|
frame: opts?.frame ?? frame ?? undefined,
|
|
509
607
|
force: opts?.force === true,
|
|
510
608
|
timeoutMs: timeoutOpt(opts),
|
|
511
|
-
settle: opts?.settle ??
|
|
609
|
+
settle: opts?.settle ?? false,
|
|
512
610
|
}), `locator.fill(${selector})`, guardOpts),
|
|
513
611
|
clear: (opts = {}) => this.#guard((b) => b.locatorClear(selector, {
|
|
514
612
|
tabId: tabFor(opts),
|
|
515
613
|
frame: opts?.frame ?? frame ?? undefined,
|
|
516
614
|
force: opts?.force === true,
|
|
517
615
|
timeoutMs: timeoutOpt(opts),
|
|
518
|
-
settle: opts?.settle ??
|
|
616
|
+
settle: opts?.settle ?? false,
|
|
519
617
|
}), `locator.clear(${selector})`, guardOpts),
|
|
520
618
|
press: (key, opts = {}) => this.#guard((b) => b.locatorPress(selector, String(key), {
|
|
521
619
|
tabId: tabFor(opts),
|
|
522
620
|
frame: opts?.frame ?? frame ?? undefined,
|
|
523
621
|
force: opts?.force === true,
|
|
524
622
|
timeoutMs: timeoutOpt(opts),
|
|
525
|
-
settle: opts?.settle ??
|
|
623
|
+
settle: opts?.settle ?? false,
|
|
526
624
|
}), `locator.press(${selector})`, guardOpts),
|
|
527
625
|
type: (text, opts = {}) => this.#guard((b) => b.locatorType(selector, String(text), {
|
|
528
626
|
tabId: tabFor(opts),
|
|
@@ -530,7 +628,7 @@ export class BrowserScriptSession {
|
|
|
530
628
|
force: opts?.force === true,
|
|
531
629
|
timeoutMs: timeoutOpt(opts),
|
|
532
630
|
sequentially: false,
|
|
533
|
-
settle: opts?.settle ??
|
|
631
|
+
settle: opts?.settle ?? false,
|
|
534
632
|
}), `locator.type(${selector})`, guardOpts),
|
|
535
633
|
pressSequentially: (text, opts = {}) => this.#guard((b) => b.locatorType(selector, String(text), {
|
|
536
634
|
tabId: tabFor(opts),
|
|
@@ -539,21 +637,21 @@ export class BrowserScriptSession {
|
|
|
539
637
|
timeoutMs: timeoutOpt(opts),
|
|
540
638
|
sequentially: true,
|
|
541
639
|
delayMs: opts?.delay ?? opts?.delayMs ?? 0,
|
|
542
|
-
settle: opts?.settle ??
|
|
640
|
+
settle: opts?.settle ?? false,
|
|
543
641
|
}), `locator.pressSequentially(${selector})`, guardOpts),
|
|
544
642
|
check: (opts = {}) => this.#guard((b) => b.locatorCheck(selector, true, {
|
|
545
643
|
tabId: tabFor(opts),
|
|
546
644
|
frame: opts?.frame ?? frame ?? undefined,
|
|
547
645
|
force: opts?.force === true,
|
|
548
646
|
timeoutMs: timeoutOpt(opts),
|
|
549
|
-
settle: opts?.settle ??
|
|
647
|
+
settle: opts?.settle ?? false,
|
|
550
648
|
}), `locator.check(${selector})`, guardOpts),
|
|
551
649
|
uncheck: (opts = {}) => this.#guard((b) => b.locatorCheck(selector, false, {
|
|
552
650
|
tabId: tabFor(opts),
|
|
553
651
|
frame: opts?.frame ?? frame ?? undefined,
|
|
554
652
|
force: opts?.force === true,
|
|
555
653
|
timeoutMs: timeoutOpt(opts),
|
|
556
|
-
settle: opts?.settle ??
|
|
654
|
+
settle: opts?.settle ?? false,
|
|
557
655
|
}), `locator.uncheck(${selector})`, guardOpts),
|
|
558
656
|
setChecked: (checked, opts = {}) => (checked ? self.check(opts) : self.uncheck(opts)),
|
|
559
657
|
selectOption: (spec, opts = {}) => {
|
|
@@ -620,7 +718,7 @@ export class BrowserScriptSession {
|
|
|
620
718
|
selector,
|
|
621
719
|
frame: opts?.frame ?? frame ?? undefined,
|
|
622
720
|
state: opts?.state ?? "visible",
|
|
623
|
-
timeoutMs: this.#clampTimeout(opts?.timeoutMs ?? opts?.timeout_ms ?? 10_000),
|
|
721
|
+
timeoutMs: this.#clampTimeout(opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms ?? 10_000),
|
|
624
722
|
}), `locator.waitFor(${selector})`, guardOpts),
|
|
625
723
|
count: async (opts = {}) => (await this.#guard((b) => b.locatorCount(selector, {
|
|
626
724
|
tabId: tabFor(opts),
|
|
@@ -674,6 +772,24 @@ export class BrowserScriptSession {
|
|
|
674
772
|
tabId: tabFor(opts),
|
|
675
773
|
frame: opts?.frame ?? frame ?? undefined,
|
|
676
774
|
}), `locator.isChecked(${selector})`, guardOpts)).value,
|
|
775
|
+
isHidden: async (opts = {}) => !(await self.isVisible(opts)),
|
|
776
|
+
isDisabled: async (opts = {}) => !(await self.isEnabled(opts)),
|
|
777
|
+
focus: (opts = {}) => this.#guard((b) => b.locatorFocus(selector, {
|
|
778
|
+
tabId: tabFor(opts),
|
|
779
|
+
frame: opts?.frame ?? frame ?? undefined,
|
|
780
|
+
force: opts?.force === true,
|
|
781
|
+
timeoutMs: timeoutOpt(opts),
|
|
782
|
+
}), `locator.focus(${selector})`, guardOpts),
|
|
783
|
+
boundingBox: async (opts = {}) => (await this.#guard((b) => b.locatorBoundingBox(selector, {
|
|
784
|
+
tabId: tabFor(opts),
|
|
785
|
+
frame: opts?.frame ?? frame ?? undefined,
|
|
786
|
+
timeoutMs: timeoutOpt(opts),
|
|
787
|
+
}), `locator.boundingBox(${selector})`, guardOpts)) ?? null,
|
|
788
|
+
innerHTML: (opts = {}) => self.evaluate((el) => el.innerHTML, opts),
|
|
789
|
+
frameLocator: (sub) => this.#createFrameLocator(
|
|
790
|
+
frame ? `${frame} >> ${selector} >> ${String(sub)}` : `${selector} >> ${String(sub)}`,
|
|
791
|
+
boundTabId,
|
|
792
|
+
),
|
|
677
793
|
evaluate: async (...args) => {
|
|
678
794
|
const [fnOrExpr, argOrOpts, maybeOpts] = args;
|
|
679
795
|
const parsed = this.#parseLocatorEvalArgs(fnOrExpr, argOrOpts, maybeOpts, args.length);
|
|
@@ -718,8 +834,10 @@ export class BrowserScriptSession {
|
|
|
718
834
|
},
|
|
719
835
|
getByRole: (role, opts) => this.#createLocator([buildRoleStep(role, opts)], sel, boundTabId),
|
|
720
836
|
getByText: (text, opts) => this.#createLocator([buildTextStep(text, opts)], sel, boundTabId),
|
|
721
|
-
getByLabel: (text) => this.#createLocator([
|
|
722
|
-
getByPlaceholder: (text) => this.#createLocator([
|
|
837
|
+
getByLabel: (text, opts) => this.#createLocator([buildNamedMatchStep("label", text, opts)], sel, boundTabId),
|
|
838
|
+
getByPlaceholder: (text, opts) => this.#createLocator([buildNamedMatchStep("placeholder", text, opts)], sel, boundTabId),
|
|
839
|
+
getByAltText: (text, opts) => this.#createLocator([buildNamedMatchStep("alt", text, opts)], sel, boundTabId),
|
|
840
|
+
getByTitle: (text, opts) => this.#createLocator([buildNamedMatchStep("title", text, opts)], sel, boundTabId),
|
|
723
841
|
getByTestId: (id) => this.#createLocator([`testid=${JSON.stringify(String(id))}`], sel, boundTabId),
|
|
724
842
|
};
|
|
725
843
|
}
|
|
@@ -764,18 +882,21 @@ export class BrowserScriptSession {
|
|
|
764
882
|
close: () => tabApi.closeTab(boundId),
|
|
765
883
|
goto: (url, opts = {}) => self.#guard((b) => b.goto(url, resolveTab(opts), {
|
|
766
884
|
waitUntil: opts?.waitUntil ?? opts?.wait_until ?? "load",
|
|
767
|
-
timeoutMs: self.#clampTimeout(opts?.timeoutMs ?? opts?.timeout_ms ?? 15_000),
|
|
885
|
+
timeoutMs: self.#clampTimeout(opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms ?? 15_000),
|
|
768
886
|
}), `goto(${url})`, guardOpts),
|
|
769
887
|
back: (opts) => self.#guard((b) => b.history("back", resolveTab(opts)), "back", guardOpts),
|
|
888
|
+
goBack: (opts) => tabApi.back(opts),
|
|
770
889
|
forward: (opts) => self.#guard((b) => b.history("forward", resolveTab(opts)), "forward", guardOpts),
|
|
890
|
+
goForward: (opts) => tabApi.forward(opts),
|
|
771
891
|
reload: (opts) => self.#guard((b) => b.reload(resolveTab(opts)), "reload", guardOpts),
|
|
892
|
+
viewportSize: (opts = {}) => tabApi.evaluate("({ width: window.innerWidth, height: window.innerHeight })", opts),
|
|
772
893
|
expectNavigation: async (action, opts = {}) => {
|
|
773
894
|
const targetTab = resolveTab(opts);
|
|
774
895
|
return self.#guard((b) => b.expectNavigation(action, {
|
|
775
896
|
tabId: targetTab,
|
|
776
897
|
url: opts?.url,
|
|
777
898
|
waitUntil: opts?.waitUntil ?? opts?.wait_until ?? "load",
|
|
778
|
-
timeoutMs: self.#clampTimeout(opts?.timeoutMs ?? opts?.timeout_ms ?? 15_000),
|
|
899
|
+
timeoutMs: self.#clampTimeout(opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms ?? 15_000),
|
|
779
900
|
}), "expectNavigation", guardOpts);
|
|
780
901
|
},
|
|
781
902
|
getState: (opts = {}) => self.#guard((b) => b.getState(resolveTab(opts), {
|
|
@@ -823,8 +944,8 @@ export class BrowserScriptSession {
|
|
|
823
944
|
format: opts?.format ?? "jpeg",
|
|
824
945
|
quality: opts?.quality ?? 75,
|
|
825
946
|
scale: opts?.scale ?? "css",
|
|
826
|
-
timeoutMs: opts?.timeoutMs !== undefined || opts?.timeout_ms !== undefined
|
|
827
|
-
? self.#clampTimeout(opts.timeoutMs ?? opts.timeout_ms)
|
|
947
|
+
timeoutMs: opts?.timeout !== undefined || opts?.timeoutMs !== undefined || opts?.timeout_ms !== undefined
|
|
948
|
+
? self.#clampTimeout(opts.timeout ?? opts.timeoutMs ?? opts.timeout_ms)
|
|
828
949
|
: undefined,
|
|
829
950
|
}), `elementScreenshot(${selector})`, guardOpts),
|
|
830
951
|
saveScreenshot: (filePath, opts = {}) => self.#saveScreenshot(filePath, opts, boundId),
|
|
@@ -954,8 +1075,10 @@ export class BrowserScriptSession {
|
|
|
954
1075
|
frameLocator: (sel) => self.#createFrameLocator(sel, boundId),
|
|
955
1076
|
getByRole: (role, opts) => self.#createLocator([buildRoleStep(role, opts)], null, boundId),
|
|
956
1077
|
getByText: (text, opts) => self.#createLocator([buildTextStep(text, opts)], null, boundId),
|
|
957
|
-
getByLabel: (text) => self.#createLocator([
|
|
958
|
-
getByPlaceholder: (text) => self.#createLocator([
|
|
1078
|
+
getByLabel: (text, opts) => self.#createLocator([buildNamedMatchStep("label", text, opts)], null, boundId),
|
|
1079
|
+
getByPlaceholder: (text, opts) => self.#createLocator([buildNamedMatchStep("placeholder", text, opts)], null, boundId),
|
|
1080
|
+
getByAltText: (text, opts) => self.#createLocator([buildNamedMatchStep("alt", text, opts)], null, boundId),
|
|
1081
|
+
getByTitle: (text, opts) => self.#createLocator([buildNamedMatchStep("title", text, opts)], null, boundId),
|
|
959
1082
|
getByTestId: (id) => self.#createLocator([`testid=${JSON.stringify(String(id))}`], null, boundId),
|
|
960
1083
|
click: (target, opts = {}) => {
|
|
961
1084
|
const tabId = resolveTab(opts);
|
|
@@ -968,7 +1091,7 @@ export class BrowserScriptSession {
|
|
|
968
1091
|
clickCount: opts?.clickCount ?? opts?.click_count ?? target.clickCount ?? 1,
|
|
969
1092
|
button: opts?.button ?? opts?.mouse_button ?? "left",
|
|
970
1093
|
modifiers: opts?.modifiers,
|
|
971
|
-
settle: opts?.settle ??
|
|
1094
|
+
settle: opts?.settle ?? false,
|
|
972
1095
|
}), "click(point)", guardOpts);
|
|
973
1096
|
}
|
|
974
1097
|
if (isLocatorLike(target)) {
|
|
@@ -979,8 +1102,8 @@ export class BrowserScriptSession {
|
|
|
979
1102
|
button: opts?.button ?? opts?.mouse_button ?? "left",
|
|
980
1103
|
modifiers: opts?.modifiers,
|
|
981
1104
|
force: opts?.force === true,
|
|
982
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
983
|
-
settle: opts?.settle ??
|
|
1105
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1106
|
+
settle: opts?.settle ?? false,
|
|
984
1107
|
}), `click(${target})`, guardOpts);
|
|
985
1108
|
}
|
|
986
1109
|
return self.#guard((b) => b.click(target, {
|
|
@@ -988,7 +1111,7 @@ export class BrowserScriptSession {
|
|
|
988
1111
|
clickCount: opts?.clickCount ?? opts?.click_count ?? 1,
|
|
989
1112
|
button: opts?.button ?? opts?.mouse_button ?? "left",
|
|
990
1113
|
modifiers: opts?.modifiers,
|
|
991
|
-
settle: opts?.settle ??
|
|
1114
|
+
settle: opts?.settle ?? false,
|
|
992
1115
|
}), `click(${target})`, guardOpts);
|
|
993
1116
|
},
|
|
994
1117
|
locatorClick: (selector, opts = {}) => self.#guard((b) => b.locatorClick(selector, {
|
|
@@ -998,8 +1121,8 @@ export class BrowserScriptSession {
|
|
|
998
1121
|
button: opts?.button ?? opts?.mouse_button ?? "left",
|
|
999
1122
|
modifiers: opts?.modifiers,
|
|
1000
1123
|
force: opts?.force === true,
|
|
1001
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1002
|
-
settle: opts?.settle ??
|
|
1124
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1125
|
+
settle: opts?.settle ?? false,
|
|
1003
1126
|
}), `locatorClick(${selector})`, guardOpts),
|
|
1004
1127
|
fill: (target, value, opts = {}) => {
|
|
1005
1128
|
const tabId = resolveTab(opts);
|
|
@@ -1008,36 +1131,36 @@ export class BrowserScriptSession {
|
|
|
1008
1131
|
tabId,
|
|
1009
1132
|
frame: opts?.frame,
|
|
1010
1133
|
force: opts?.force === true,
|
|
1011
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1012
|
-
settle: opts?.settle ??
|
|
1134
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1135
|
+
settle: opts?.settle ?? false,
|
|
1013
1136
|
}), `fill(${target})`, guardOpts);
|
|
1014
1137
|
}
|
|
1015
1138
|
return self.#guard((b) => b.setValue(target, String(value), {
|
|
1016
1139
|
tabId,
|
|
1017
|
-
settle: opts?.settle ??
|
|
1140
|
+
settle: opts?.settle ?? false,
|
|
1018
1141
|
}), `setValue(${target})`, guardOpts);
|
|
1019
1142
|
},
|
|
1020
1143
|
locatorFill: (selector, value, opts = {}) => self.#guard((b) => b.locatorFill(selector, String(value), {
|
|
1021
1144
|
tabId: resolveTab(opts),
|
|
1022
1145
|
frame: opts?.frame,
|
|
1023
1146
|
force: opts?.force === true,
|
|
1024
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1025
|
-
settle: opts?.settle ??
|
|
1147
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1148
|
+
settle: opts?.settle ?? false,
|
|
1026
1149
|
}), `locatorFill(${selector})`, guardOpts),
|
|
1027
1150
|
setValue: (index, value, opts = {}) => self.#guard((b) => b.setValue(index, String(value), {
|
|
1028
1151
|
tabId: resolveTab(opts),
|
|
1029
|
-
settle: opts?.settle ??
|
|
1152
|
+
settle: opts?.settle ?? false,
|
|
1030
1153
|
}), `setValue(${index})`, guardOpts),
|
|
1031
1154
|
type: (text, opts = {}) => self.#guard((b) => b.typeText(String(text), {
|
|
1032
1155
|
tabId: resolveTab(opts),
|
|
1033
1156
|
elementIndex: opts?.elementIndex ?? opts?.element_index,
|
|
1034
|
-
settle: opts?.settle ??
|
|
1157
|
+
settle: opts?.settle ?? false,
|
|
1035
1158
|
}), "typeText", guardOpts),
|
|
1036
1159
|
typeText: (text, opts = {}) => tabApi.type(text, opts),
|
|
1037
1160
|
press: (key, opts = {}) => self.#guard((b) => b.pressKey(String(key), {
|
|
1038
1161
|
tabId: resolveTab(opts),
|
|
1039
1162
|
elementIndex: opts?.elementIndex ?? opts?.element_index,
|
|
1040
|
-
settle: opts?.settle ??
|
|
1163
|
+
settle: opts?.settle ?? false,
|
|
1041
1164
|
}), `pressKey(${key})`, guardOpts),
|
|
1042
1165
|
pressKey: (key, opts = {}) => tabApi.press(key, opts),
|
|
1043
1166
|
hover: (target, opts = {}) => {
|
|
@@ -1052,7 +1175,7 @@ export class BrowserScriptSession {
|
|
|
1052
1175
|
selector: target,
|
|
1053
1176
|
frame: opts?.frame,
|
|
1054
1177
|
force: opts?.force === true,
|
|
1055
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1178
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1056
1179
|
}), `hover(${target})`, guardOpts);
|
|
1057
1180
|
}
|
|
1058
1181
|
return self.#guard((b) => b.hover({ tabId, elementIndex: target }), `hover(${target})`, guardOpts);
|
|
@@ -1095,7 +1218,7 @@ export class BrowserScriptSession {
|
|
|
1095
1218
|
elementIndex: opts?.elementIndex ?? opts?.element_index,
|
|
1096
1219
|
selector: opts?.selector,
|
|
1097
1220
|
frame: opts?.frame,
|
|
1098
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1221
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1099
1222
|
}), "uploadFiles", guardOpts),
|
|
1100
1223
|
innerText: (selector, opts = {}) => {
|
|
1101
1224
|
if (selector === undefined || selector === null || isPlainObject(selector)) {
|
|
@@ -1109,11 +1232,16 @@ export class BrowserScriptSession {
|
|
|
1109
1232
|
}
|
|
1110
1233
|
return tabApi.locator(selector).textContent(opts);
|
|
1111
1234
|
},
|
|
1235
|
+
innerHTML: (selector, opts = {}) => tabApi.locator(selector).innerHTML(opts),
|
|
1112
1236
|
inputValue: (selector, opts = {}) => tabApi.locator(selector).inputValue(opts),
|
|
1113
1237
|
getAttribute: (selector, name, opts = {}) => tabApi.locator(selector).getAttribute(name, opts),
|
|
1114
1238
|
isVisible: (selector, opts = {}) => tabApi.locator(selector).isVisible(opts),
|
|
1239
|
+
isHidden: (selector, opts = {}) => tabApi.locator(selector).isHidden(opts),
|
|
1115
1240
|
isEnabled: (selector, opts = {}) => tabApi.locator(selector).isEnabled(opts),
|
|
1241
|
+
isDisabled: (selector, opts = {}) => tabApi.locator(selector).isDisabled(opts),
|
|
1116
1242
|
isChecked: (selector, opts = {}) => tabApi.locator(selector).isChecked(opts),
|
|
1243
|
+
focus: (selector, opts = {}) => tabApi.locator(selector).focus(opts),
|
|
1244
|
+
boundingBox: (selector, opts = {}) => tabApi.locator(selector).boundingBox(opts),
|
|
1117
1245
|
check: (selector, opts = {}) => tabApi.locator(selector).check(opts),
|
|
1118
1246
|
uncheck: (selector, opts = {}) => tabApi.locator(selector).uncheck(opts),
|
|
1119
1247
|
waitFor: (optsOrMs) => {
|
|
@@ -1134,14 +1262,15 @@ export class BrowserScriptSession {
|
|
|
1134
1262
|
loadState: opts.loadState ?? opts.load_state,
|
|
1135
1263
|
state: opts.state,
|
|
1136
1264
|
frame: opts.frame,
|
|
1137
|
-
timeoutMs: self.#clampTimeout(opts.timeoutMs ?? opts.timeout_ms ?? 10_000),
|
|
1265
|
+
timeoutMs: self.#clampTimeout(opts.timeout ?? opts.timeoutMs ?? opts.timeout_ms ?? 10_000),
|
|
1138
1266
|
}), "waitFor", guardOpts);
|
|
1139
1267
|
},
|
|
1268
|
+
waitForSelector: (selector, opts = {}) => tabApi.locator(selector).waitFor(opts),
|
|
1140
1269
|
wait: (optsOrMs) => tabApi.waitFor(optsOrMs),
|
|
1141
1270
|
waitForEvent: async (eventName, opts = {}) => {
|
|
1142
1271
|
const res = await self.#guard((b) => b.waitForEvent(eventName, {
|
|
1143
1272
|
tabId: resolveTab(opts),
|
|
1144
|
-
timeoutMs: self.#clampTimeout(opts?.timeoutMs ?? opts?.timeout_ms ?? 30_000),
|
|
1273
|
+
timeoutMs: self.#clampTimeout(opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms ?? 30_000),
|
|
1145
1274
|
}), `waitForEvent(${eventName})`, guardOpts);
|
|
1146
1275
|
if (eventName === "popup" && res?.tab_id) {
|
|
1147
1276
|
const handle = self.#createTabHandle(res.tab_id);
|
|
@@ -1315,7 +1444,7 @@ export class BrowserScriptSession {
|
|
|
1315
1444
|
},
|
|
1316
1445
|
performSecondaryAction: (idx, action, opts = {}) => self.#guard((b) => b.performSecondaryAction(idx, action, {
|
|
1317
1446
|
tabId: resolveTab(opts),
|
|
1318
|
-
settle: opts?.settle ??
|
|
1447
|
+
settle: opts?.settle ?? false,
|
|
1319
1448
|
}), `ax.performSecondaryAction(${idx}, ${action})`, guardOpts),
|
|
1320
1449
|
ariaSnapshot: (opts = {}) => tabApi.ariaSnapshot(opts),
|
|
1321
1450
|
},
|
|
@@ -1422,7 +1551,15 @@ export class BrowserScriptSession {
|
|
|
1422
1551
|
* @param {string} code
|
|
1423
1552
|
* @param {{tabId?:unknown, timeoutMs?:number, maxLogChars?:number, maxResultChars?:number, maxImages?:number}} [options]
|
|
1424
1553
|
*/
|
|
1425
|
-
|
|
1554
|
+
run(browser, code, options = {}) {
|
|
1555
|
+
const exec = () => this.#runSerialized(browser, code, options);
|
|
1556
|
+
const task = this.#runQueue.then(exec, exec);
|
|
1557
|
+
this.#runQueue = task.catch(() => {});
|
|
1558
|
+
return task;
|
|
1559
|
+
}
|
|
1560
|
+
|
|
1561
|
+
/** @param {import("./bridge-browser.js").BridgeBrowser} browser @param {string} code @param {{tabId?:unknown, timeoutMs?:number, maxLogChars?:number, maxResultChars?:number, maxImages?:number}} [options] */
|
|
1562
|
+
async #runSerialized(browser, code, options = {}) {
|
|
1426
1563
|
const timeoutMs = Math.max(500, Math.min(300_000, Number(options.timeoutMs) || 45_000));
|
|
1427
1564
|
const maxLogChars = options.maxLogChars ?? 16_000;
|
|
1428
1565
|
const maxResultChars = options.maxResultChars ?? 32_000;
|
|
@@ -1444,12 +1581,14 @@ export class BrowserScriptSession {
|
|
|
1444
1581
|
keptTabs: new Map(),
|
|
1445
1582
|
savedFiles: new Map(),
|
|
1446
1583
|
eventCleanups: new Map(),
|
|
1584
|
+
pendingPromises: new Set(),
|
|
1447
1585
|
endTurnRequested: false,
|
|
1448
1586
|
ungroupOnEndTurn: false,
|
|
1449
1587
|
logs: [],
|
|
1450
1588
|
logChars: 0,
|
|
1451
1589
|
images: [],
|
|
1452
1590
|
activeTimers: new Set(),
|
|
1591
|
+
activeIntervals: new Set(),
|
|
1453
1592
|
maxLogChars,
|
|
1454
1593
|
maxImages,
|
|
1455
1594
|
};
|
|
@@ -1478,7 +1617,9 @@ export class BrowserScriptSession {
|
|
|
1478
1617
|
.map((k) => `try { __scope[${JSON.stringify(k)}] = ${k}; } catch { /* uninitialized */ }`)
|
|
1479
1618
|
.join("\n");
|
|
1480
1619
|
|
|
1481
|
-
const
|
|
1620
|
+
const headerLines = `(async function __browserRun(browser, page, log, sleep, viewImage, saveFile, readFile, saveScreenshot, nodeRepl, console, __scope) {\n${preloadLines}\ntry {\n`;
|
|
1621
|
+
const headerLineOffset = headerLines.match(/\n/g)?.length ?? 3;
|
|
1622
|
+
const wrapped = `${headerLines}${transformed}\n} finally {\n${saveLines}\n}\n})`;
|
|
1482
1623
|
const script = new vm.Script(wrapped, { filename: "browser-run.js" });
|
|
1483
1624
|
const runner = script.runInContext(this.context, { timeout: Math.min(timeoutMs, 5_000) });
|
|
1484
1625
|
|
|
@@ -1490,9 +1631,23 @@ export class BrowserScriptSession {
|
|
|
1490
1631
|
}, timeoutMs);
|
|
1491
1632
|
});
|
|
1492
1633
|
|
|
1634
|
+
const runnerPromise = runner(
|
|
1635
|
+
api,
|
|
1636
|
+
api,
|
|
1637
|
+
log,
|
|
1638
|
+
sleep,
|
|
1639
|
+
viewImage,
|
|
1640
|
+
saveFile,
|
|
1641
|
+
readFile,
|
|
1642
|
+
saveScreenshot,
|
|
1643
|
+
nodeRepl,
|
|
1644
|
+
consoleFacade,
|
|
1645
|
+
this.scope,
|
|
1646
|
+
);
|
|
1647
|
+
|
|
1493
1648
|
try {
|
|
1494
1649
|
const rawValue = await Promise.race([
|
|
1495
|
-
|
|
1650
|
+
runnerPromise,
|
|
1496
1651
|
timeoutPromise,
|
|
1497
1652
|
]);
|
|
1498
1653
|
const notifications = [];
|
|
@@ -1520,11 +1675,36 @@ export class BrowserScriptSession {
|
|
|
1520
1675
|
endTurnRequested: run.endTurnRequested,
|
|
1521
1676
|
ungroupOnEndTurn: run.ungroupOnEndTurn,
|
|
1522
1677
|
};
|
|
1678
|
+
} catch (err) {
|
|
1679
|
+
if (err && typeof err === "object") {
|
|
1680
|
+
const stackMatch = typeof err.stack === "string"
|
|
1681
|
+
? /browser-run\.js:(\d+):(\d+)/.exec(err.stack)
|
|
1682
|
+
: null;
|
|
1683
|
+
const location = stackMatch
|
|
1684
|
+
? {
|
|
1685
|
+
line: Math.max(1, Number(stackMatch[1]) - headerLineOffset),
|
|
1686
|
+
column: Number(stackMatch[2]),
|
|
1687
|
+
}
|
|
1688
|
+
: null;
|
|
1689
|
+
err.browserRunMeta = {
|
|
1690
|
+
location,
|
|
1691
|
+
last_action: run.lastAction ?? null,
|
|
1692
|
+
saved_files: [...run.savedFiles.values()],
|
|
1693
|
+
partial_logs: [...run.logs],
|
|
1694
|
+
activeTabId: this.#activeTabId(),
|
|
1695
|
+
};
|
|
1696
|
+
}
|
|
1697
|
+
throw err;
|
|
1523
1698
|
} finally {
|
|
1524
1699
|
run.aborted = true;
|
|
1525
1700
|
clearTimeout(deadlineTimer);
|
|
1701
|
+
runnerPromise.catch(() => {});
|
|
1702
|
+
for (const pending of run.pendingPromises) pending.catch(() => {});
|
|
1703
|
+
run.pendingPromises.clear();
|
|
1526
1704
|
for (const timer of run.activeTimers) clearTimeout(timer);
|
|
1527
1705
|
run.activeTimers.clear();
|
|
1706
|
+
for (const interval of run.activeIntervals) clearInterval(interval);
|
|
1707
|
+
run.activeIntervals.clear();
|
|
1528
1708
|
for (const unsub of run.eventCleanups.values()) {
|
|
1529
1709
|
try { unsub(); } catch { /* Ignore cleanup failure */ }
|
|
1530
1710
|
}
|