browser-pilot 0.0.8 → 0.0.9

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.
@@ -1,283 +0,0 @@
1
- // src/browser/types.ts
2
- var ElementNotFoundError = class extends Error {
3
- selectors;
4
- hints;
5
- constructor(selectors, hints) {
6
- const selectorList = Array.isArray(selectors) ? selectors : [selectors];
7
- super(`Element not found: ${selectorList.join(", ")}`);
8
- this.name = "ElementNotFoundError";
9
- this.selectors = selectorList;
10
- this.hints = hints;
11
- }
12
- };
13
- var TimeoutError = class extends Error {
14
- constructor(message = "Operation timed out") {
15
- super(message);
16
- this.name = "TimeoutError";
17
- }
18
- };
19
- var NavigationError = class extends Error {
20
- constructor(message) {
21
- super(message);
22
- this.name = "NavigationError";
23
- }
24
- };
25
-
26
- // src/actions/executor.ts
27
- var DEFAULT_TIMEOUT = 3e4;
28
- var BatchExecutor = class {
29
- page;
30
- constructor(page) {
31
- this.page = page;
32
- }
33
- /**
34
- * Execute a batch of steps
35
- */
36
- async execute(steps, options = {}) {
37
- const { timeout = DEFAULT_TIMEOUT, onFail = "stop" } = options;
38
- const results = [];
39
- const startTime = Date.now();
40
- for (let i = 0; i < steps.length; i++) {
41
- const step = steps[i];
42
- const stepStart = Date.now();
43
- try {
44
- const result = await this.executeStep(step, timeout);
45
- results.push({
46
- index: i,
47
- action: step.action,
48
- selector: step.selector,
49
- selectorUsed: result.selectorUsed,
50
- success: true,
51
- durationMs: Date.now() - stepStart,
52
- result: result.value,
53
- text: result.text
54
- });
55
- } catch (error) {
56
- const errorMessage = error instanceof Error ? error.message : String(error);
57
- const hints = error instanceof ElementNotFoundError ? error.hints : void 0;
58
- results.push({
59
- index: i,
60
- action: step.action,
61
- selector: step.selector,
62
- success: false,
63
- durationMs: Date.now() - stepStart,
64
- error: errorMessage,
65
- hints
66
- });
67
- if (onFail === "stop" && !step.optional) {
68
- return {
69
- success: false,
70
- stoppedAtIndex: i,
71
- steps: results,
72
- totalDurationMs: Date.now() - startTime
73
- };
74
- }
75
- }
76
- }
77
- const allSuccess = results.every((r) => r.success || steps[r.index]?.optional);
78
- return {
79
- success: allSuccess,
80
- steps: results,
81
- totalDurationMs: Date.now() - startTime
82
- };
83
- }
84
- /**
85
- * Execute a single step
86
- */
87
- async executeStep(step, defaultTimeout) {
88
- const timeout = step.timeout ?? defaultTimeout;
89
- const optional = step.optional ?? false;
90
- switch (step.action) {
91
- case "goto": {
92
- if (!step.url) throw new Error("goto requires url");
93
- await this.page.goto(step.url, { timeout, optional });
94
- return {};
95
- }
96
- case "click": {
97
- if (!step.selector) throw new Error("click requires selector");
98
- if (step.waitForNavigation) {
99
- const navPromise = this.page.waitForNavigation({ timeout, optional });
100
- await this.page.click(step.selector, { timeout, optional });
101
- await navPromise;
102
- } else {
103
- await this.page.click(step.selector, { timeout, optional });
104
- }
105
- return { selectorUsed: this.getUsedSelector(step.selector) };
106
- }
107
- case "fill": {
108
- if (!step.selector) throw new Error("fill requires selector");
109
- if (typeof step.value !== "string") throw new Error("fill requires string value");
110
- await this.page.fill(step.selector, step.value, {
111
- timeout,
112
- optional,
113
- clear: step.clear ?? true,
114
- blur: step.blur
115
- });
116
- return { selectorUsed: this.getUsedSelector(step.selector) };
117
- }
118
- case "type": {
119
- if (!step.selector) throw new Error("type requires selector");
120
- if (typeof step.value !== "string") throw new Error("type requires string value");
121
- await this.page.type(step.selector, step.value, {
122
- timeout,
123
- optional,
124
- delay: step.delay ?? 50
125
- });
126
- return { selectorUsed: this.getUsedSelector(step.selector) };
127
- }
128
- case "select": {
129
- if (step.trigger && step.option && typeof step.value === "string") {
130
- await this.page.select(
131
- {
132
- trigger: step.trigger,
133
- option: step.option,
134
- value: step.value,
135
- match: step.match
136
- },
137
- { timeout, optional }
138
- );
139
- return { selectorUsed: this.getUsedSelector(step.trigger) };
140
- }
141
- if (!step.selector) throw new Error("select requires selector");
142
- if (!step.value) throw new Error("select requires value");
143
- await this.page.select(step.selector, step.value, { timeout, optional });
144
- return { selectorUsed: this.getUsedSelector(step.selector) };
145
- }
146
- case "check": {
147
- if (!step.selector) throw new Error("check requires selector");
148
- await this.page.check(step.selector, { timeout, optional });
149
- return { selectorUsed: this.getUsedSelector(step.selector) };
150
- }
151
- case "uncheck": {
152
- if (!step.selector) throw new Error("uncheck requires selector");
153
- await this.page.uncheck(step.selector, { timeout, optional });
154
- return { selectorUsed: this.getUsedSelector(step.selector) };
155
- }
156
- case "submit": {
157
- if (!step.selector) throw new Error("submit requires selector");
158
- await this.page.submit(step.selector, {
159
- timeout,
160
- optional,
161
- method: step.method ?? "enter+click"
162
- });
163
- return { selectorUsed: this.getUsedSelector(step.selector) };
164
- }
165
- case "press": {
166
- if (!step.key) throw new Error("press requires key");
167
- await this.page.press(step.key);
168
- return {};
169
- }
170
- case "focus": {
171
- if (!step.selector) throw new Error("focus requires selector");
172
- await this.page.focus(step.selector, { timeout, optional });
173
- return { selectorUsed: this.getUsedSelector(step.selector) };
174
- }
175
- case "hover": {
176
- if (!step.selector) throw new Error("hover requires selector");
177
- await this.page.hover(step.selector, { timeout, optional });
178
- return { selectorUsed: this.getUsedSelector(step.selector) };
179
- }
180
- case "scroll": {
181
- if (step.x !== void 0 || step.y !== void 0) {
182
- await this.page.scroll("body", { x: step.x, y: step.y, timeout, optional });
183
- return {};
184
- }
185
- if (!step.selector && (step.direction || step.amount !== void 0)) {
186
- const amount = step.amount ?? 500;
187
- const direction = step.direction ?? "down";
188
- const deltaY = direction === "down" ? amount : direction === "up" ? -amount : 0;
189
- const deltaX = direction === "right" ? amount : direction === "left" ? -amount : 0;
190
- await this.page.evaluate(`window.scrollBy(${deltaX}, ${deltaY})`);
191
- return {};
192
- }
193
- if (!step.selector) throw new Error("scroll requires selector, coordinates, or direction");
194
- await this.page.scroll(step.selector, { timeout, optional });
195
- return { selectorUsed: this.getUsedSelector(step.selector) };
196
- }
197
- case "wait": {
198
- if (!step.selector && !step.waitFor) {
199
- const delay = step.timeout ?? 1e3;
200
- await new Promise((resolve) => setTimeout(resolve, delay));
201
- return {};
202
- }
203
- if (step.waitFor === "navigation") {
204
- await this.page.waitForNavigation({ timeout, optional });
205
- return {};
206
- }
207
- if (step.waitFor === "networkIdle") {
208
- await this.page.waitForNetworkIdle({ timeout, optional });
209
- return {};
210
- }
211
- if (!step.selector)
212
- throw new Error(
213
- "wait requires selector (or waitFor: navigation/networkIdle, or timeout for simple delay)"
214
- );
215
- await this.page.waitFor(step.selector, {
216
- timeout,
217
- optional,
218
- state: step.waitFor ?? "visible"
219
- });
220
- return { selectorUsed: this.getUsedSelector(step.selector) };
221
- }
222
- case "snapshot": {
223
- const snapshot = await this.page.snapshot();
224
- return { value: snapshot };
225
- }
226
- case "screenshot": {
227
- const data = await this.page.screenshot({
228
- format: step.format,
229
- quality: step.quality,
230
- fullPage: step.fullPage
231
- });
232
- return { value: data };
233
- }
234
- case "evaluate": {
235
- if (typeof step.value !== "string")
236
- throw new Error("evaluate requires string value (expression)");
237
- const result = await this.page.evaluate(step.value);
238
- return { value: result };
239
- }
240
- case "text": {
241
- const selector = Array.isArray(step.selector) ? step.selector[0] : step.selector;
242
- const text = await this.page.text(selector);
243
- return { text, selectorUsed: selector };
244
- }
245
- case "switchFrame": {
246
- if (!step.selector) throw new Error("switchFrame requires selector");
247
- await this.page.switchToFrame(step.selector, { timeout, optional });
248
- return { selectorUsed: this.getUsedSelector(step.selector) };
249
- }
250
- case "switchToMain": {
251
- await this.page.switchToMain();
252
- return {};
253
- }
254
- default:
255
- throw new Error(
256
- `Unknown action: ${step.action}. Run 'bp actions' for available actions.`
257
- );
258
- }
259
- }
260
- /**
261
- * Get the actual selector that matched the element.
262
- * Uses the last matched selector tracked by Page, falls back to first selector if unavailable.
263
- */
264
- getUsedSelector(selector) {
265
- const matched = this.page.getLastMatchedSelector();
266
- if (matched) return matched;
267
- return Array.isArray(selector) ? selector[0] : selector;
268
- }
269
- };
270
- function addBatchToPage(page) {
271
- const executor = new BatchExecutor(page);
272
- return Object.assign(page, {
273
- batch: (steps, options) => executor.execute(steps, options)
274
- });
275
- }
276
-
277
- export {
278
- ElementNotFoundError,
279
- TimeoutError,
280
- NavigationError,
281
- BatchExecutor,
282
- addBatchToPage
283
- };