codex-browser-use 4.8.3 → 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 +419 -137
- package/extension/manifest.json +2 -10
- package/package.json +2 -2
- package/rules/browser_use.md +6 -20
- package/skill/SKILL.md +7 -7
- 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 +383 -113
- 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 +199 -48
- package/src/server.js +64 -26
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
|
}
|
|
@@ -467,8 +552,8 @@ export class BrowserScriptSession {
|
|
|
467
552
|
const tabFor = (opts) => this.#resolveTabArg(opts, boundTabId);
|
|
468
553
|
const guardOpts = { trackActiveTab: boundTabId === null };
|
|
469
554
|
const timeoutOpt = (opts) => (
|
|
470
|
-
opts?.timeoutMs !== undefined || opts?.timeout_ms !== undefined
|
|
471
|
-
? 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)
|
|
472
557
|
: undefined
|
|
473
558
|
);
|
|
474
559
|
|
|
@@ -482,8 +567,10 @@ export class BrowserScriptSession {
|
|
|
482
567
|
},
|
|
483
568
|
getByRole: (role, opts) => this.#createLocator([...chain, buildRoleStep(role, opts)], frame, boundTabId),
|
|
484
569
|
getByText: (text, opts) => this.#createLocator([...chain, buildTextStep(text, opts)], frame, boundTabId),
|
|
485
|
-
getByLabel: (text) => this.#createLocator([...chain,
|
|
486
|
-
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),
|
|
487
574
|
getByTestId: (id) => this.#createLocator([...chain, `testid=${JSON.stringify(String(id))}`], frame, boundTabId),
|
|
488
575
|
filter: ({ visible, hasText, hasNotText, has, hasNot } = {}) => {
|
|
489
576
|
const steps = [];
|
|
@@ -511,7 +598,7 @@ export class BrowserScriptSession {
|
|
|
511
598
|
modifiers: opts?.modifiers,
|
|
512
599
|
force: opts?.force === true,
|
|
513
600
|
timeoutMs: timeoutOpt(opts),
|
|
514
|
-
settle: opts?.settle ??
|
|
601
|
+
settle: opts?.settle ?? false,
|
|
515
602
|
}), `locator.click(${selector})`, guardOpts),
|
|
516
603
|
dblclick: (opts = {}) => self.click({ ...opts, clickCount: 2 }),
|
|
517
604
|
fill: (value, opts = {}) => this.#guard((b) => b.locatorFill(selector, String(value), {
|
|
@@ -519,21 +606,21 @@ export class BrowserScriptSession {
|
|
|
519
606
|
frame: opts?.frame ?? frame ?? undefined,
|
|
520
607
|
force: opts?.force === true,
|
|
521
608
|
timeoutMs: timeoutOpt(opts),
|
|
522
|
-
settle: opts?.settle ??
|
|
609
|
+
settle: opts?.settle ?? false,
|
|
523
610
|
}), `locator.fill(${selector})`, guardOpts),
|
|
524
611
|
clear: (opts = {}) => this.#guard((b) => b.locatorClear(selector, {
|
|
525
612
|
tabId: tabFor(opts),
|
|
526
613
|
frame: opts?.frame ?? frame ?? undefined,
|
|
527
614
|
force: opts?.force === true,
|
|
528
615
|
timeoutMs: timeoutOpt(opts),
|
|
529
|
-
settle: opts?.settle ??
|
|
616
|
+
settle: opts?.settle ?? false,
|
|
530
617
|
}), `locator.clear(${selector})`, guardOpts),
|
|
531
618
|
press: (key, opts = {}) => this.#guard((b) => b.locatorPress(selector, String(key), {
|
|
532
619
|
tabId: tabFor(opts),
|
|
533
620
|
frame: opts?.frame ?? frame ?? undefined,
|
|
534
621
|
force: opts?.force === true,
|
|
535
622
|
timeoutMs: timeoutOpt(opts),
|
|
536
|
-
settle: opts?.settle ??
|
|
623
|
+
settle: opts?.settle ?? false,
|
|
537
624
|
}), `locator.press(${selector})`, guardOpts),
|
|
538
625
|
type: (text, opts = {}) => this.#guard((b) => b.locatorType(selector, String(text), {
|
|
539
626
|
tabId: tabFor(opts),
|
|
@@ -541,7 +628,7 @@ export class BrowserScriptSession {
|
|
|
541
628
|
force: opts?.force === true,
|
|
542
629
|
timeoutMs: timeoutOpt(opts),
|
|
543
630
|
sequentially: false,
|
|
544
|
-
settle: opts?.settle ??
|
|
631
|
+
settle: opts?.settle ?? false,
|
|
545
632
|
}), `locator.type(${selector})`, guardOpts),
|
|
546
633
|
pressSequentially: (text, opts = {}) => this.#guard((b) => b.locatorType(selector, String(text), {
|
|
547
634
|
tabId: tabFor(opts),
|
|
@@ -550,21 +637,21 @@ export class BrowserScriptSession {
|
|
|
550
637
|
timeoutMs: timeoutOpt(opts),
|
|
551
638
|
sequentially: true,
|
|
552
639
|
delayMs: opts?.delay ?? opts?.delayMs ?? 0,
|
|
553
|
-
settle: opts?.settle ??
|
|
640
|
+
settle: opts?.settle ?? false,
|
|
554
641
|
}), `locator.pressSequentially(${selector})`, guardOpts),
|
|
555
642
|
check: (opts = {}) => this.#guard((b) => b.locatorCheck(selector, true, {
|
|
556
643
|
tabId: tabFor(opts),
|
|
557
644
|
frame: opts?.frame ?? frame ?? undefined,
|
|
558
645
|
force: opts?.force === true,
|
|
559
646
|
timeoutMs: timeoutOpt(opts),
|
|
560
|
-
settle: opts?.settle ??
|
|
647
|
+
settle: opts?.settle ?? false,
|
|
561
648
|
}), `locator.check(${selector})`, guardOpts),
|
|
562
649
|
uncheck: (opts = {}) => this.#guard((b) => b.locatorCheck(selector, false, {
|
|
563
650
|
tabId: tabFor(opts),
|
|
564
651
|
frame: opts?.frame ?? frame ?? undefined,
|
|
565
652
|
force: opts?.force === true,
|
|
566
653
|
timeoutMs: timeoutOpt(opts),
|
|
567
|
-
settle: opts?.settle ??
|
|
654
|
+
settle: opts?.settle ?? false,
|
|
568
655
|
}), `locator.uncheck(${selector})`, guardOpts),
|
|
569
656
|
setChecked: (checked, opts = {}) => (checked ? self.check(opts) : self.uncheck(opts)),
|
|
570
657
|
selectOption: (spec, opts = {}) => {
|
|
@@ -631,7 +718,7 @@ export class BrowserScriptSession {
|
|
|
631
718
|
selector,
|
|
632
719
|
frame: opts?.frame ?? frame ?? undefined,
|
|
633
720
|
state: opts?.state ?? "visible",
|
|
634
|
-
timeoutMs: this.#clampTimeout(opts?.timeoutMs ?? opts?.timeout_ms ?? 10_000),
|
|
721
|
+
timeoutMs: this.#clampTimeout(opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms ?? 10_000),
|
|
635
722
|
}), `locator.waitFor(${selector})`, guardOpts),
|
|
636
723
|
count: async (opts = {}) => (await this.#guard((b) => b.locatorCount(selector, {
|
|
637
724
|
tabId: tabFor(opts),
|
|
@@ -685,6 +772,24 @@ export class BrowserScriptSession {
|
|
|
685
772
|
tabId: tabFor(opts),
|
|
686
773
|
frame: opts?.frame ?? frame ?? undefined,
|
|
687
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
|
+
),
|
|
688
793
|
evaluate: async (...args) => {
|
|
689
794
|
const [fnOrExpr, argOrOpts, maybeOpts] = args;
|
|
690
795
|
const parsed = this.#parseLocatorEvalArgs(fnOrExpr, argOrOpts, maybeOpts, args.length);
|
|
@@ -729,8 +834,10 @@ export class BrowserScriptSession {
|
|
|
729
834
|
},
|
|
730
835
|
getByRole: (role, opts) => this.#createLocator([buildRoleStep(role, opts)], sel, boundTabId),
|
|
731
836
|
getByText: (text, opts) => this.#createLocator([buildTextStep(text, opts)], sel, boundTabId),
|
|
732
|
-
getByLabel: (text) => this.#createLocator([
|
|
733
|
-
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),
|
|
734
841
|
getByTestId: (id) => this.#createLocator([`testid=${JSON.stringify(String(id))}`], sel, boundTabId),
|
|
735
842
|
};
|
|
736
843
|
}
|
|
@@ -775,18 +882,21 @@ export class BrowserScriptSession {
|
|
|
775
882
|
close: () => tabApi.closeTab(boundId),
|
|
776
883
|
goto: (url, opts = {}) => self.#guard((b) => b.goto(url, resolveTab(opts), {
|
|
777
884
|
waitUntil: opts?.waitUntil ?? opts?.wait_until ?? "load",
|
|
778
|
-
timeoutMs: self.#clampTimeout(opts?.timeoutMs ?? opts?.timeout_ms ?? 15_000),
|
|
885
|
+
timeoutMs: self.#clampTimeout(opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms ?? 15_000),
|
|
779
886
|
}), `goto(${url})`, guardOpts),
|
|
780
887
|
back: (opts) => self.#guard((b) => b.history("back", resolveTab(opts)), "back", guardOpts),
|
|
888
|
+
goBack: (opts) => tabApi.back(opts),
|
|
781
889
|
forward: (opts) => self.#guard((b) => b.history("forward", resolveTab(opts)), "forward", guardOpts),
|
|
890
|
+
goForward: (opts) => tabApi.forward(opts),
|
|
782
891
|
reload: (opts) => self.#guard((b) => b.reload(resolveTab(opts)), "reload", guardOpts),
|
|
892
|
+
viewportSize: (opts = {}) => tabApi.evaluate("({ width: window.innerWidth, height: window.innerHeight })", opts),
|
|
783
893
|
expectNavigation: async (action, opts = {}) => {
|
|
784
894
|
const targetTab = resolveTab(opts);
|
|
785
895
|
return self.#guard((b) => b.expectNavigation(action, {
|
|
786
896
|
tabId: targetTab,
|
|
787
897
|
url: opts?.url,
|
|
788
898
|
waitUntil: opts?.waitUntil ?? opts?.wait_until ?? "load",
|
|
789
|
-
timeoutMs: self.#clampTimeout(opts?.timeoutMs ?? opts?.timeout_ms ?? 15_000),
|
|
899
|
+
timeoutMs: self.#clampTimeout(opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms ?? 15_000),
|
|
790
900
|
}), "expectNavigation", guardOpts);
|
|
791
901
|
},
|
|
792
902
|
getState: (opts = {}) => self.#guard((b) => b.getState(resolveTab(opts), {
|
|
@@ -834,8 +944,8 @@ export class BrowserScriptSession {
|
|
|
834
944
|
format: opts?.format ?? "jpeg",
|
|
835
945
|
quality: opts?.quality ?? 75,
|
|
836
946
|
scale: opts?.scale ?? "css",
|
|
837
|
-
timeoutMs: opts?.timeoutMs !== undefined || opts?.timeout_ms !== undefined
|
|
838
|
-
? 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)
|
|
839
949
|
: undefined,
|
|
840
950
|
}), `elementScreenshot(${selector})`, guardOpts),
|
|
841
951
|
saveScreenshot: (filePath, opts = {}) => self.#saveScreenshot(filePath, opts, boundId),
|
|
@@ -965,8 +1075,10 @@ export class BrowserScriptSession {
|
|
|
965
1075
|
frameLocator: (sel) => self.#createFrameLocator(sel, boundId),
|
|
966
1076
|
getByRole: (role, opts) => self.#createLocator([buildRoleStep(role, opts)], null, boundId),
|
|
967
1077
|
getByText: (text, opts) => self.#createLocator([buildTextStep(text, opts)], null, boundId),
|
|
968
|
-
getByLabel: (text) => self.#createLocator([
|
|
969
|
-
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),
|
|
970
1082
|
getByTestId: (id) => self.#createLocator([`testid=${JSON.stringify(String(id))}`], null, boundId),
|
|
971
1083
|
click: (target, opts = {}) => {
|
|
972
1084
|
const tabId = resolveTab(opts);
|
|
@@ -979,7 +1091,7 @@ export class BrowserScriptSession {
|
|
|
979
1091
|
clickCount: opts?.clickCount ?? opts?.click_count ?? target.clickCount ?? 1,
|
|
980
1092
|
button: opts?.button ?? opts?.mouse_button ?? "left",
|
|
981
1093
|
modifiers: opts?.modifiers,
|
|
982
|
-
settle: opts?.settle ??
|
|
1094
|
+
settle: opts?.settle ?? false,
|
|
983
1095
|
}), "click(point)", guardOpts);
|
|
984
1096
|
}
|
|
985
1097
|
if (isLocatorLike(target)) {
|
|
@@ -990,8 +1102,8 @@ export class BrowserScriptSession {
|
|
|
990
1102
|
button: opts?.button ?? opts?.mouse_button ?? "left",
|
|
991
1103
|
modifiers: opts?.modifiers,
|
|
992
1104
|
force: opts?.force === true,
|
|
993
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
994
|
-
settle: opts?.settle ??
|
|
1105
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1106
|
+
settle: opts?.settle ?? false,
|
|
995
1107
|
}), `click(${target})`, guardOpts);
|
|
996
1108
|
}
|
|
997
1109
|
return self.#guard((b) => b.click(target, {
|
|
@@ -999,7 +1111,7 @@ export class BrowserScriptSession {
|
|
|
999
1111
|
clickCount: opts?.clickCount ?? opts?.click_count ?? 1,
|
|
1000
1112
|
button: opts?.button ?? opts?.mouse_button ?? "left",
|
|
1001
1113
|
modifiers: opts?.modifiers,
|
|
1002
|
-
settle: opts?.settle ??
|
|
1114
|
+
settle: opts?.settle ?? false,
|
|
1003
1115
|
}), `click(${target})`, guardOpts);
|
|
1004
1116
|
},
|
|
1005
1117
|
locatorClick: (selector, opts = {}) => self.#guard((b) => b.locatorClick(selector, {
|
|
@@ -1009,8 +1121,8 @@ export class BrowserScriptSession {
|
|
|
1009
1121
|
button: opts?.button ?? opts?.mouse_button ?? "left",
|
|
1010
1122
|
modifiers: opts?.modifiers,
|
|
1011
1123
|
force: opts?.force === true,
|
|
1012
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1013
|
-
settle: opts?.settle ??
|
|
1124
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1125
|
+
settle: opts?.settle ?? false,
|
|
1014
1126
|
}), `locatorClick(${selector})`, guardOpts),
|
|
1015
1127
|
fill: (target, value, opts = {}) => {
|
|
1016
1128
|
const tabId = resolveTab(opts);
|
|
@@ -1019,36 +1131,36 @@ export class BrowserScriptSession {
|
|
|
1019
1131
|
tabId,
|
|
1020
1132
|
frame: opts?.frame,
|
|
1021
1133
|
force: opts?.force === true,
|
|
1022
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1023
|
-
settle: opts?.settle ??
|
|
1134
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1135
|
+
settle: opts?.settle ?? false,
|
|
1024
1136
|
}), `fill(${target})`, guardOpts);
|
|
1025
1137
|
}
|
|
1026
1138
|
return self.#guard((b) => b.setValue(target, String(value), {
|
|
1027
1139
|
tabId,
|
|
1028
|
-
settle: opts?.settle ??
|
|
1140
|
+
settle: opts?.settle ?? false,
|
|
1029
1141
|
}), `setValue(${target})`, guardOpts);
|
|
1030
1142
|
},
|
|
1031
1143
|
locatorFill: (selector, value, opts = {}) => self.#guard((b) => b.locatorFill(selector, String(value), {
|
|
1032
1144
|
tabId: resolveTab(opts),
|
|
1033
1145
|
frame: opts?.frame,
|
|
1034
1146
|
force: opts?.force === true,
|
|
1035
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1036
|
-
settle: opts?.settle ??
|
|
1147
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1148
|
+
settle: opts?.settle ?? false,
|
|
1037
1149
|
}), `locatorFill(${selector})`, guardOpts),
|
|
1038
1150
|
setValue: (index, value, opts = {}) => self.#guard((b) => b.setValue(index, String(value), {
|
|
1039
1151
|
tabId: resolveTab(opts),
|
|
1040
|
-
settle: opts?.settle ??
|
|
1152
|
+
settle: opts?.settle ?? false,
|
|
1041
1153
|
}), `setValue(${index})`, guardOpts),
|
|
1042
1154
|
type: (text, opts = {}) => self.#guard((b) => b.typeText(String(text), {
|
|
1043
1155
|
tabId: resolveTab(opts),
|
|
1044
1156
|
elementIndex: opts?.elementIndex ?? opts?.element_index,
|
|
1045
|
-
settle: opts?.settle ??
|
|
1157
|
+
settle: opts?.settle ?? false,
|
|
1046
1158
|
}), "typeText", guardOpts),
|
|
1047
1159
|
typeText: (text, opts = {}) => tabApi.type(text, opts),
|
|
1048
1160
|
press: (key, opts = {}) => self.#guard((b) => b.pressKey(String(key), {
|
|
1049
1161
|
tabId: resolveTab(opts),
|
|
1050
1162
|
elementIndex: opts?.elementIndex ?? opts?.element_index,
|
|
1051
|
-
settle: opts?.settle ??
|
|
1163
|
+
settle: opts?.settle ?? false,
|
|
1052
1164
|
}), `pressKey(${key})`, guardOpts),
|
|
1053
1165
|
pressKey: (key, opts = {}) => tabApi.press(key, opts),
|
|
1054
1166
|
hover: (target, opts = {}) => {
|
|
@@ -1063,7 +1175,7 @@ export class BrowserScriptSession {
|
|
|
1063
1175
|
selector: target,
|
|
1064
1176
|
frame: opts?.frame,
|
|
1065
1177
|
force: opts?.force === true,
|
|
1066
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1178
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1067
1179
|
}), `hover(${target})`, guardOpts);
|
|
1068
1180
|
}
|
|
1069
1181
|
return self.#guard((b) => b.hover({ tabId, elementIndex: target }), `hover(${target})`, guardOpts);
|
|
@@ -1106,7 +1218,7 @@ export class BrowserScriptSession {
|
|
|
1106
1218
|
elementIndex: opts?.elementIndex ?? opts?.element_index,
|
|
1107
1219
|
selector: opts?.selector,
|
|
1108
1220
|
frame: opts?.frame,
|
|
1109
|
-
timeoutMs: opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1221
|
+
timeoutMs: opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms,
|
|
1110
1222
|
}), "uploadFiles", guardOpts),
|
|
1111
1223
|
innerText: (selector, opts = {}) => {
|
|
1112
1224
|
if (selector === undefined || selector === null || isPlainObject(selector)) {
|
|
@@ -1120,11 +1232,16 @@ export class BrowserScriptSession {
|
|
|
1120
1232
|
}
|
|
1121
1233
|
return tabApi.locator(selector).textContent(opts);
|
|
1122
1234
|
},
|
|
1235
|
+
innerHTML: (selector, opts = {}) => tabApi.locator(selector).innerHTML(opts),
|
|
1123
1236
|
inputValue: (selector, opts = {}) => tabApi.locator(selector).inputValue(opts),
|
|
1124
1237
|
getAttribute: (selector, name, opts = {}) => tabApi.locator(selector).getAttribute(name, opts),
|
|
1125
1238
|
isVisible: (selector, opts = {}) => tabApi.locator(selector).isVisible(opts),
|
|
1239
|
+
isHidden: (selector, opts = {}) => tabApi.locator(selector).isHidden(opts),
|
|
1126
1240
|
isEnabled: (selector, opts = {}) => tabApi.locator(selector).isEnabled(opts),
|
|
1241
|
+
isDisabled: (selector, opts = {}) => tabApi.locator(selector).isDisabled(opts),
|
|
1127
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),
|
|
1128
1245
|
check: (selector, opts = {}) => tabApi.locator(selector).check(opts),
|
|
1129
1246
|
uncheck: (selector, opts = {}) => tabApi.locator(selector).uncheck(opts),
|
|
1130
1247
|
waitFor: (optsOrMs) => {
|
|
@@ -1145,14 +1262,15 @@ export class BrowserScriptSession {
|
|
|
1145
1262
|
loadState: opts.loadState ?? opts.load_state,
|
|
1146
1263
|
state: opts.state,
|
|
1147
1264
|
frame: opts.frame,
|
|
1148
|
-
timeoutMs: self.#clampTimeout(opts.timeoutMs ?? opts.timeout_ms ?? 10_000),
|
|
1265
|
+
timeoutMs: self.#clampTimeout(opts.timeout ?? opts.timeoutMs ?? opts.timeout_ms ?? 10_000),
|
|
1149
1266
|
}), "waitFor", guardOpts);
|
|
1150
1267
|
},
|
|
1268
|
+
waitForSelector: (selector, opts = {}) => tabApi.locator(selector).waitFor(opts),
|
|
1151
1269
|
wait: (optsOrMs) => tabApi.waitFor(optsOrMs),
|
|
1152
1270
|
waitForEvent: async (eventName, opts = {}) => {
|
|
1153
1271
|
const res = await self.#guard((b) => b.waitForEvent(eventName, {
|
|
1154
1272
|
tabId: resolveTab(opts),
|
|
1155
|
-
timeoutMs: self.#clampTimeout(opts?.timeoutMs ?? opts?.timeout_ms ?? 30_000),
|
|
1273
|
+
timeoutMs: self.#clampTimeout(opts?.timeout ?? opts?.timeoutMs ?? opts?.timeout_ms ?? 30_000),
|
|
1156
1274
|
}), `waitForEvent(${eventName})`, guardOpts);
|
|
1157
1275
|
if (eventName === "popup" && res?.tab_id) {
|
|
1158
1276
|
const handle = self.#createTabHandle(res.tab_id);
|
|
@@ -1326,7 +1444,7 @@ export class BrowserScriptSession {
|
|
|
1326
1444
|
},
|
|
1327
1445
|
performSecondaryAction: (idx, action, opts = {}) => self.#guard((b) => b.performSecondaryAction(idx, action, {
|
|
1328
1446
|
tabId: resolveTab(opts),
|
|
1329
|
-
settle: opts?.settle ??
|
|
1447
|
+
settle: opts?.settle ?? false,
|
|
1330
1448
|
}), `ax.performSecondaryAction(${idx}, ${action})`, guardOpts),
|
|
1331
1449
|
ariaSnapshot: (opts = {}) => tabApi.ariaSnapshot(opts),
|
|
1332
1450
|
},
|
|
@@ -1433,7 +1551,15 @@ export class BrowserScriptSession {
|
|
|
1433
1551
|
* @param {string} code
|
|
1434
1552
|
* @param {{tabId?:unknown, timeoutMs?:number, maxLogChars?:number, maxResultChars?:number, maxImages?:number}} [options]
|
|
1435
1553
|
*/
|
|
1436
|
-
|
|
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 = {}) {
|
|
1437
1563
|
const timeoutMs = Math.max(500, Math.min(300_000, Number(options.timeoutMs) || 45_000));
|
|
1438
1564
|
const maxLogChars = options.maxLogChars ?? 16_000;
|
|
1439
1565
|
const maxResultChars = options.maxResultChars ?? 32_000;
|
|
@@ -1462,6 +1588,7 @@ export class BrowserScriptSession {
|
|
|
1462
1588
|
logChars: 0,
|
|
1463
1589
|
images: [],
|
|
1464
1590
|
activeTimers: new Set(),
|
|
1591
|
+
activeIntervals: new Set(),
|
|
1465
1592
|
maxLogChars,
|
|
1466
1593
|
maxImages,
|
|
1467
1594
|
};
|
|
@@ -1490,7 +1617,9 @@ export class BrowserScriptSession {
|
|
|
1490
1617
|
.map((k) => `try { __scope[${JSON.stringify(k)}] = ${k}; } catch { /* uninitialized */ }`)
|
|
1491
1618
|
.join("\n");
|
|
1492
1619
|
|
|
1493
|
-
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})`;
|
|
1494
1623
|
const script = new vm.Script(wrapped, { filename: "browser-run.js" });
|
|
1495
1624
|
const runner = script.runInContext(this.context, { timeout: Math.min(timeoutMs, 5_000) });
|
|
1496
1625
|
|
|
@@ -1546,6 +1675,26 @@ export class BrowserScriptSession {
|
|
|
1546
1675
|
endTurnRequested: run.endTurnRequested,
|
|
1547
1676
|
ungroupOnEndTurn: run.ungroupOnEndTurn,
|
|
1548
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;
|
|
1549
1698
|
} finally {
|
|
1550
1699
|
run.aborted = true;
|
|
1551
1700
|
clearTimeout(deadlineTimer);
|
|
@@ -1554,6 +1703,8 @@ export class BrowserScriptSession {
|
|
|
1554
1703
|
run.pendingPromises.clear();
|
|
1555
1704
|
for (const timer of run.activeTimers) clearTimeout(timer);
|
|
1556
1705
|
run.activeTimers.clear();
|
|
1706
|
+
for (const interval of run.activeIntervals) clearInterval(interval);
|
|
1707
|
+
run.activeIntervals.clear();
|
|
1557
1708
|
for (const unsub of run.eventCleanups.values()) {
|
|
1558
1709
|
try { unsub(); } catch { /* Ignore cleanup failure */ }
|
|
1559
1710
|
}
|