browserpilot-cdp 1.2.6 → 1.3.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 +4 -4
- package/dist/cli.js +772 -77
- package/dist/cli.js.map +2 -2
- package/dist/index.js +719 -70
- package/dist/index.js.map +2 -2
- package/dist/native-host.js.map +1 -1
- package/extension/background.js +64 -1
- package/extension/background.js.map +2 -2
- package/extension/content-script.js +263 -33
- package/extension/content-script.js.map +2 -2
- package/extension/manifest.json +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import os from "node:os";
|
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
|
|
7
7
|
// ../protocol/dist/constants.js
|
|
8
|
-
var BROWSERPILOT_VERSION = "1.
|
|
8
|
+
var BROWSERPILOT_VERSION = "1.3.0";
|
|
9
9
|
var DEFAULT_DAEMON_HOST = "127.0.0.1";
|
|
10
10
|
var DEFAULT_DAEMON_PORT = 19888;
|
|
11
11
|
var NATIVE_HOST_NAME = "com.browserpilot.bridge";
|
|
@@ -617,6 +617,14 @@ var ExtensionBridgeTransport = class {
|
|
|
617
617
|
await this.rpc("extension.ghostCursor", { tabId, x, y, clicking }).catch(() => {
|
|
618
618
|
});
|
|
619
619
|
}
|
|
620
|
+
async setOverlayVisible(tabId, visible) {
|
|
621
|
+
await this.rpc("extension.setOverlayVisible", { tabId, visible }).catch(() => {
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
async setControlBanner(tabId, banner) {
|
|
625
|
+
await this.rpc("extension.setControlBanner", { tabId, ...banner }).catch(() => {
|
|
626
|
+
});
|
|
627
|
+
}
|
|
620
628
|
async getPinnedElement(tabId) {
|
|
621
629
|
return await this.rpc("extension.getPinnedElement", { tabId });
|
|
622
630
|
}
|
|
@@ -1693,6 +1701,34 @@ function buttonBitmask(button) {
|
|
|
1693
1701
|
return 4;
|
|
1694
1702
|
return 1;
|
|
1695
1703
|
}
|
|
1704
|
+
function resolveEditingCommands(key, mods) {
|
|
1705
|
+
const hasAccel = mods.includes("Meta") || mods.includes("Control");
|
|
1706
|
+
const hasShift = mods.includes("Shift");
|
|
1707
|
+
const lower = key.toLowerCase();
|
|
1708
|
+
if (hasAccel && !mods.includes("Alt")) {
|
|
1709
|
+
if (lower === "a" && !hasShift)
|
|
1710
|
+
return ["selectAll"];
|
|
1711
|
+
if (lower === "c" && !hasShift)
|
|
1712
|
+
return ["copy"];
|
|
1713
|
+
if (lower === "x" && !hasShift)
|
|
1714
|
+
return ["cut"];
|
|
1715
|
+
if (lower === "v" && !hasShift)
|
|
1716
|
+
return ["paste"];
|
|
1717
|
+
if (lower === "v" && hasShift)
|
|
1718
|
+
return ["pasteAndMatchStyle"];
|
|
1719
|
+
if (lower === "z" && !hasShift)
|
|
1720
|
+
return ["undo"];
|
|
1721
|
+
if (lower === "z" && hasShift || lower === "y" && !hasShift)
|
|
1722
|
+
return ["redo"];
|
|
1723
|
+
}
|
|
1724
|
+
if (!hasAccel && !mods.includes("Alt")) {
|
|
1725
|
+
if (key === "Backspace")
|
|
1726
|
+
return ["deleteBackward"];
|
|
1727
|
+
if (key === "Delete")
|
|
1728
|
+
return ["deleteForward"];
|
|
1729
|
+
}
|
|
1730
|
+
return [];
|
|
1731
|
+
}
|
|
1696
1732
|
function resolveKeyDef(keyName) {
|
|
1697
1733
|
if (SPECIAL_KEYS[keyName])
|
|
1698
1734
|
return SPECIAL_KEYS[keyName];
|
|
@@ -1833,8 +1869,9 @@ var PageController = class {
|
|
|
1833
1869
|
}
|
|
1834
1870
|
if (!box || box.width === 0 && box.height === 0)
|
|
1835
1871
|
return null;
|
|
1872
|
+
let frameOffset;
|
|
1836
1873
|
if (sessionId) {
|
|
1837
|
-
|
|
1874
|
+
frameOffset = await this.getIframeOffset(refTarget?.frameId, sessionId);
|
|
1838
1875
|
box = {
|
|
1839
1876
|
x: box.x + frameOffset.x,
|
|
1840
1877
|
y: box.y + frameOffset.y,
|
|
@@ -1846,6 +1883,7 @@ var PageController = class {
|
|
|
1846
1883
|
objectId,
|
|
1847
1884
|
backendNodeId,
|
|
1848
1885
|
sessionId,
|
|
1886
|
+
frameOffset,
|
|
1849
1887
|
box,
|
|
1850
1888
|
center: {
|
|
1851
1889
|
x: Math.round(box.x + box.width / 2),
|
|
@@ -2028,23 +2066,189 @@ var PageController = class {
|
|
|
2028
2066
|
});
|
|
2029
2067
|
return res.result?.objectId;
|
|
2030
2068
|
}
|
|
2069
|
+
/**
|
|
2070
|
+
* Verifies pre-click actionability (position stability, enabled state, and non-occlusion).
|
|
2071
|
+
* If a fixed/sticky header occludes the target, automatically re-centers it in viewport.
|
|
2072
|
+
*/
|
|
2073
|
+
async ensureClickActionable(el, targetLabel) {
|
|
2074
|
+
let current = el;
|
|
2075
|
+
let underOwnPill = false;
|
|
2076
|
+
for (let attempt = 0; attempt < 6; attempt++) {
|
|
2077
|
+
await sleep(35);
|
|
2078
|
+
const nextBox = await this.readElementViewportBox(current);
|
|
2079
|
+
if (!nextBox)
|
|
2080
|
+
break;
|
|
2081
|
+
const moved = Math.abs(nextBox.x - current.box.x) > 2 || Math.abs(nextBox.y - current.box.y) > 2;
|
|
2082
|
+
current = {
|
|
2083
|
+
...current,
|
|
2084
|
+
box: nextBox,
|
|
2085
|
+
center: { x: nextBox.x + nextBox.width / 2, y: nextBox.y + nextBox.height / 2 }
|
|
2086
|
+
};
|
|
2087
|
+
if (!moved)
|
|
2088
|
+
break;
|
|
2089
|
+
}
|
|
2090
|
+
if (current.sessionId)
|
|
2091
|
+
return { el: current, underOwnPill: false };
|
|
2092
|
+
for (let retry = 0; retry < 2; retry++) {
|
|
2093
|
+
const check = await this.session.send("Runtime.callFunctionOn", {
|
|
2094
|
+
objectId: current.objectId,
|
|
2095
|
+
arguments: [{ value: current.center.x }, { value: current.center.y }],
|
|
2096
|
+
functionDeclaration: `function(cx, cy) {
|
|
2097
|
+
if (this.disabled === true || this.getAttribute?.('aria-disabled') === 'true') {
|
|
2098
|
+
return { ok: false, reason: 'disabled' };
|
|
2099
|
+
}
|
|
2100
|
+
const style = getComputedStyle(this);
|
|
2101
|
+
if (style.pointerEvents === 'none') {
|
|
2102
|
+
return { ok: false, reason: 'pointer-events-none' };
|
|
2103
|
+
}
|
|
2104
|
+
function deepHit(root, x, y) {
|
|
2105
|
+
let hit = root.elementFromPoint(x, y);
|
|
2106
|
+
while (hit && hit.shadowRoot) {
|
|
2107
|
+
const inner = hit.shadowRoot.elementFromPoint(x, y);
|
|
2108
|
+
if (!inner || inner === hit) break;
|
|
2109
|
+
hit = inner;
|
|
2110
|
+
}
|
|
2111
|
+
return hit;
|
|
2112
|
+
}
|
|
2113
|
+
let hit = deepHit(document, cx, cy);
|
|
2114
|
+
let hitOwnPill = false;
|
|
2115
|
+
if (hit && hit.id === '__browserpilot_ui_host') {
|
|
2116
|
+
hitOwnPill = true;
|
|
2117
|
+
const prevDisplay = hit.style.display;
|
|
2118
|
+
hit.style.display = 'none';
|
|
2119
|
+
hit = deepHit(document, cx, cy);
|
|
2120
|
+
const uiHost = document.getElementById('__browserpilot_ui_host');
|
|
2121
|
+
if (uiHost) uiHost.style.display = prevDisplay;
|
|
2122
|
+
}
|
|
2123
|
+
if (!hit) return { ok: true, underOwnPill: hitOwnPill };
|
|
2124
|
+
if (hit === this || this.contains(hit) || hit.contains(this)) return { ok: true, underOwnPill: hitOwnPill };
|
|
2125
|
+
// Allow clicking associated <label> for form controls
|
|
2126
|
+
if (this.id && hit.closest && hit.closest('label[for="' + CSS.escape(this.id) + '"]')) return { ok: true, underOwnPill: hitOwnPill };
|
|
2127
|
+
// Walk up occluder to detect fixed/sticky header vs modal overlay
|
|
2128
|
+
let cur = hit;
|
|
2129
|
+
let isFixedOrSticky = false;
|
|
2130
|
+
while (cur && cur !== document.body && cur !== document.documentElement) {
|
|
2131
|
+
const pos = getComputedStyle(cur).position;
|
|
2132
|
+
if (pos === 'fixed' || pos === 'sticky') {
|
|
2133
|
+
isFixedOrSticky = true;
|
|
2134
|
+
break;
|
|
2135
|
+
}
|
|
2136
|
+
cur = cur.parentElement;
|
|
2137
|
+
}
|
|
2138
|
+
const tag = hit.tagName?.toLowerCase() || 'element';
|
|
2139
|
+
const cls = typeof hit.className === 'string' && hit.className.trim()
|
|
2140
|
+
? '.' + hit.className.trim().split(/\\s+/).slice(0, 2).join('.')
|
|
2141
|
+
: '';
|
|
2142
|
+
const id = hit.id ? '#' + hit.id : '';
|
|
2143
|
+
const txt = (hit.textContent || '').replace(/\\s+/g, ' ').trim().slice(0, 40);
|
|
2144
|
+
return {
|
|
2145
|
+
ok: false,
|
|
2146
|
+
reason: 'occluded',
|
|
2147
|
+
occluder: '<' + tag + id + cls + '>' + (txt ? ' ("' + txt + '")' : ''),
|
|
2148
|
+
isFixedOrSticky,
|
|
2149
|
+
underOwnPill: hitOwnPill,
|
|
2150
|
+
};
|
|
2151
|
+
}`,
|
|
2152
|
+
returnByValue: true
|
|
2153
|
+
}).catch(() => null);
|
|
2154
|
+
const val = check?.result?.value;
|
|
2155
|
+
if (val?.underOwnPill)
|
|
2156
|
+
underOwnPill = true;
|
|
2157
|
+
if (!val || val.ok)
|
|
2158
|
+
return { el: current, underOwnPill };
|
|
2159
|
+
if (val.reason === "disabled") {
|
|
2160
|
+
throw new Error(`Element "${targetLabel}" is disabled and cannot be clicked.`);
|
|
2161
|
+
}
|
|
2162
|
+
if (val.reason === "pointer-events-none") {
|
|
2163
|
+
throw new Error(`Element "${targetLabel}" has pointer-events: none (pass force: true to override).`);
|
|
2164
|
+
}
|
|
2165
|
+
if (val.reason === "occluded" && val.isFixedOrSticky && retry === 0) {
|
|
2166
|
+
await this.session.send("Runtime.callFunctionOn", {
|
|
2167
|
+
objectId: current.objectId,
|
|
2168
|
+
functionDeclaration: `function() { this.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' }); }`
|
|
2169
|
+
}).catch(() => {
|
|
2170
|
+
});
|
|
2171
|
+
await sleep(60);
|
|
2172
|
+
const recentered = await this.readElementViewportBox(current);
|
|
2173
|
+
if (recentered) {
|
|
2174
|
+
current = {
|
|
2175
|
+
...current,
|
|
2176
|
+
box: recentered,
|
|
2177
|
+
center: { x: recentered.x + recentered.width / 2, y: recentered.y + recentered.height / 2 }
|
|
2178
|
+
};
|
|
2179
|
+
continue;
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
if (val.reason === "occluded") {
|
|
2183
|
+
throw new Error(`Element "${targetLabel}" is occluded at (${Math.round(current.center.x)}, ${Math.round(current.center.y)}) by ${val.occluder ?? "another element"}. Dismiss the overlay/banner first or pass force: true.`);
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
return { el: current, underOwnPill };
|
|
2187
|
+
}
|
|
2188
|
+
async readElementViewportBox(el) {
|
|
2189
|
+
try {
|
|
2190
|
+
const res = await this.session.send("Runtime.callFunctionOn", {
|
|
2191
|
+
objectId: el.objectId,
|
|
2192
|
+
functionDeclaration: `function() {
|
|
2193
|
+
const r = this.getBoundingClientRect();
|
|
2194
|
+
return { x: r.left, y: r.top, width: r.width, height: r.height };
|
|
2195
|
+
}`,
|
|
2196
|
+
returnByValue: true
|
|
2197
|
+
}, { sessionId: el.sessionId });
|
|
2198
|
+
const b = res.result?.value;
|
|
2199
|
+
if (!b || b.width <= 0 || b.height <= 0)
|
|
2200
|
+
return null;
|
|
2201
|
+
if (el.frameOffset) {
|
|
2202
|
+
return {
|
|
2203
|
+
x: b.x + el.frameOffset.x,
|
|
2204
|
+
y: b.y + el.frameOffset.y,
|
|
2205
|
+
width: b.width,
|
|
2206
|
+
height: b.height
|
|
2207
|
+
};
|
|
2208
|
+
}
|
|
2209
|
+
return b;
|
|
2210
|
+
} catch {
|
|
2211
|
+
return null;
|
|
2212
|
+
}
|
|
2213
|
+
}
|
|
2031
2214
|
async click(targetOrCoords, options = {}) {
|
|
2032
2215
|
const button = options.button ?? "left";
|
|
2033
2216
|
const clickCount = options.clickCount ?? 1;
|
|
2034
2217
|
const modifiers = modifierBitmask(options.modifiers);
|
|
2035
2218
|
let x;
|
|
2036
2219
|
let y;
|
|
2220
|
+
let underOwnPill = false;
|
|
2037
2221
|
if (typeof targetOrCoords === "string") {
|
|
2038
|
-
|
|
2222
|
+
let el = await this.resolveElement(targetOrCoords, options.timeoutMs);
|
|
2223
|
+
if (!options.force) {
|
|
2224
|
+
const checked = await this.ensureClickActionable(el, targetOrCoords);
|
|
2225
|
+
el = checked.el;
|
|
2226
|
+
underOwnPill = checked.underOwnPill;
|
|
2227
|
+
}
|
|
2039
2228
|
x = el.center.x;
|
|
2040
2229
|
y = el.center.y;
|
|
2230
|
+
await this.session.transport.setControlBanner?.(this.tabId, { active: true, status: `Clicking ${targetOrCoords}`, mode: "working" }).catch(() => {
|
|
2231
|
+
});
|
|
2041
2232
|
await this.session.transport.highlightElement?.(this.tabId, el.box, `click ${targetOrCoords}`).catch(() => {
|
|
2042
2233
|
});
|
|
2043
2234
|
} else {
|
|
2044
2235
|
x = targetOrCoords.x;
|
|
2045
2236
|
y = targetOrCoords.y;
|
|
2237
|
+
await this.session.transport.setControlBanner?.(this.tabId, { active: true, status: `Clicking (${Math.round(x)}, ${Math.round(y)})`, mode: "working" }).catch(() => {
|
|
2238
|
+
});
|
|
2239
|
+
}
|
|
2240
|
+
if (underOwnPill) {
|
|
2241
|
+
await this.evaluate(`(() => { const h = document.getElementById('__browserpilot_ui_host'); if (h) h.style.display = 'none'; })()`).catch(() => {
|
|
2242
|
+
});
|
|
2243
|
+
}
|
|
2244
|
+
try {
|
|
2245
|
+
await this.mouseClick(x, y, { button, clickCount, modifiers });
|
|
2246
|
+
} finally {
|
|
2247
|
+
if (underOwnPill) {
|
|
2248
|
+
await this.evaluate(`(() => { const h = document.getElementById('__browserpilot_ui_host'); if (h) h.style.display = ''; })()`).catch(() => {
|
|
2249
|
+
});
|
|
2250
|
+
}
|
|
2046
2251
|
}
|
|
2047
|
-
await this.mouseClick(x, y, { button, clickCount, modifiers });
|
|
2048
2252
|
}
|
|
2049
2253
|
async dblclick(targetOrCoords) {
|
|
2050
2254
|
await this.click(targetOrCoords, { clickCount: 2 });
|
|
@@ -2072,19 +2276,26 @@ var PageController = class {
|
|
|
2072
2276
|
const startX = this.lastMousePos.x;
|
|
2073
2277
|
const startY = this.lastMousePos.y;
|
|
2074
2278
|
for (let i = 1; i <= steps; i++) {
|
|
2075
|
-
const
|
|
2076
|
-
const
|
|
2279
|
+
const t = i / steps;
|
|
2280
|
+
const eased = steps > 1 ? 1 - Math.pow(1 - t, 2) : 1;
|
|
2281
|
+
const cx = startX + (x - startX) * eased;
|
|
2282
|
+
const cy = startY + (y - startY) * eased;
|
|
2077
2283
|
await this.session.send("Input.dispatchMouseEvent", {
|
|
2078
2284
|
type: "mouseMoved",
|
|
2079
2285
|
x: cx,
|
|
2080
2286
|
y: cy
|
|
2081
2287
|
});
|
|
2082
|
-
if (steps > 1)
|
|
2083
|
-
await
|
|
2288
|
+
if (steps > 1) {
|
|
2289
|
+
await this.session.transport.moveGhostCursor?.(this.tabId, cx, cy, false).catch(() => {
|
|
2290
|
+
});
|
|
2291
|
+
await sleep(8);
|
|
2292
|
+
}
|
|
2084
2293
|
}
|
|
2085
2294
|
this.lastMousePos = { x, y };
|
|
2086
|
-
|
|
2087
|
-
|
|
2295
|
+
if (steps === 1) {
|
|
2296
|
+
await this.session.transport.moveGhostCursor?.(this.tabId, x, y, false).catch(() => {
|
|
2297
|
+
});
|
|
2298
|
+
}
|
|
2088
2299
|
}
|
|
2089
2300
|
async mouseDown(x, y, opts = {}) {
|
|
2090
2301
|
const button = opts.button ?? "left";
|
|
@@ -2119,7 +2330,8 @@ var PageController = class {
|
|
|
2119
2330
|
async mouseClick(x, y, opts = {}) {
|
|
2120
2331
|
const button = opts.button ?? "left";
|
|
2121
2332
|
const clicks = opts.clickCount ?? 1;
|
|
2122
|
-
|
|
2333
|
+
const dist = Math.hypot(x - this.lastMousePos.x, y - this.lastMousePos.y);
|
|
2334
|
+
await this.mouseMove(x, y, { steps: dist > 40 ? 4 : 1 });
|
|
2123
2335
|
for (let c = 1; c <= clicks; c++) {
|
|
2124
2336
|
await this.mouseDown(x, y, { button, clickCount: c, modifiers: opts.modifiers });
|
|
2125
2337
|
await this.mouseUp(x, y, { button, clickCount: c, modifiers: opts.modifiers });
|
|
@@ -2156,14 +2368,87 @@ var PageController = class {
|
|
|
2156
2368
|
dx = amount;
|
|
2157
2369
|
else if (options.direction === "left")
|
|
2158
2370
|
dx = -amount;
|
|
2371
|
+
await this.session.transport.setControlBanner?.(this.tabId, {
|
|
2372
|
+
active: true,
|
|
2373
|
+
status: `Scrolling ${options.direction ?? (dy >= 0 ? "down" : "up")}`,
|
|
2374
|
+
mode: "working"
|
|
2375
|
+
}).catch(() => {
|
|
2376
|
+
});
|
|
2377
|
+
const probeMainScrollContainer = async () => {
|
|
2378
|
+
return await this.evaluate(`(() => {
|
|
2379
|
+
const vw = window.innerWidth || 1280;
|
|
2380
|
+
const vh = window.innerHeight || 720;
|
|
2381
|
+
const docEl = document.scrollingElement || document.documentElement;
|
|
2382
|
+
const docMaxY = Math.max(0, (docEl?.scrollHeight || 0) - vh);
|
|
2383
|
+
if (docMaxY > 24) {
|
|
2384
|
+
return {
|
|
2385
|
+
x: Math.round(vw / 2),
|
|
2386
|
+
y: Math.round(vh / 2),
|
|
2387
|
+
scrollX: Math.round(window.scrollX || docEl.scrollLeft || 0),
|
|
2388
|
+
scrollY: Math.round(window.scrollY || docEl.scrollTop || 0),
|
|
2389
|
+
maxScrollY: Math.round(docMaxY),
|
|
2390
|
+
container: 'window',
|
|
2391
|
+
};
|
|
2392
|
+
}
|
|
2393
|
+
// SPA layout where an inner container (main/section/div) owns scroll
|
|
2394
|
+
let best = null;
|
|
2395
|
+
let bestArea = 0;
|
|
2396
|
+
for (const el of document.querySelectorAll('main, [role="main"], section, article, div')) {
|
|
2397
|
+
const st = getComputedStyle(el);
|
|
2398
|
+
if (!/(auto|scroll|overlay)/.test(st.overflowY)) continue;
|
|
2399
|
+
const maxY = el.scrollHeight - el.clientHeight;
|
|
2400
|
+
if (maxY <= 24) continue;
|
|
2401
|
+
const r = el.getBoundingClientRect();
|
|
2402
|
+
if (r.width < 160 || r.height < 160) continue;
|
|
2403
|
+
const visW = Math.max(0, Math.min(r.right, vw) - Math.max(r.left, 0));
|
|
2404
|
+
const visH = Math.max(0, Math.min(r.bottom, vh) - Math.max(r.top, 0));
|
|
2405
|
+
const area = visW * visH;
|
|
2406
|
+
if (area > bestArea) {
|
|
2407
|
+
bestArea = area;
|
|
2408
|
+
const id = el.id ? '#' + el.id : '';
|
|
2409
|
+
best = {
|
|
2410
|
+
x: Math.round(Math.max(r.left, 0) + visW / 2),
|
|
2411
|
+
y: Math.round(Math.max(r.top, 0) + visH / 2),
|
|
2412
|
+
scrollX: Math.round(el.scrollLeft),
|
|
2413
|
+
scrollY: Math.round(el.scrollTop),
|
|
2414
|
+
maxScrollY: Math.round(maxY),
|
|
2415
|
+
container: el.tagName.toLowerCase() + id,
|
|
2416
|
+
};
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
return best || {
|
|
2420
|
+
x: Math.round(vw / 2),
|
|
2421
|
+
y: Math.round(vh / 2),
|
|
2422
|
+
scrollX: Math.round(window.scrollX || 0),
|
|
2423
|
+
scrollY: Math.round(window.scrollY || 0),
|
|
2424
|
+
maxScrollY: Math.round(docMaxY),
|
|
2425
|
+
container: 'window',
|
|
2426
|
+
};
|
|
2427
|
+
})()`);
|
|
2428
|
+
};
|
|
2429
|
+
const before = await probeMainScrollContainer();
|
|
2159
2430
|
if (options.target) {
|
|
2160
2431
|
const el = await this.resolveElement(options.target);
|
|
2161
|
-
if (dx
|
|
2162
|
-
|
|
2163
|
-
|
|
2432
|
+
if (dx !== 0 || dy !== 0) {
|
|
2433
|
+
await this.wheel(dx, dy, el.center);
|
|
2434
|
+
await sleep(120);
|
|
2435
|
+
}
|
|
2164
2436
|
} else {
|
|
2165
|
-
await this.wheel(dx, dy, { x:
|
|
2437
|
+
await this.wheel(dx, dy, { x: before.x, y: before.y });
|
|
2438
|
+
await sleep(120);
|
|
2166
2439
|
}
|
|
2440
|
+
const after = await probeMainScrollContainer();
|
|
2441
|
+
const percentY = after.maxScrollY > 0 ? Math.min(100, Math.round(after.scrollY / after.maxScrollY * 100)) : 100;
|
|
2442
|
+
return {
|
|
2443
|
+
scrolled: after.scrollY !== before.scrollY || after.scrollX !== before.scrollX,
|
|
2444
|
+
scrollX: after.scrollX,
|
|
2445
|
+
scrollY: after.scrollY,
|
|
2446
|
+
maxScrollY: after.maxScrollY,
|
|
2447
|
+
percentY,
|
|
2448
|
+
atTop: after.scrollY <= 1,
|
|
2449
|
+
atBottom: after.maxScrollY <= 1 || after.scrollY >= after.maxScrollY - 2,
|
|
2450
|
+
container: after.container
|
|
2451
|
+
};
|
|
2167
2452
|
}
|
|
2168
2453
|
/**
|
|
2169
2454
|
* Focuses element, clears existing value, and inserts text via trusted CDP Input.insertText.
|
|
@@ -2171,6 +2456,8 @@ var PageController = class {
|
|
|
2171
2456
|
*/
|
|
2172
2457
|
async fill(target, value, options = {}) {
|
|
2173
2458
|
const el = await this.resolveElement(target, options.timeoutMs);
|
|
2459
|
+
await this.session.transport.setControlBanner?.(this.tabId, { active: true, status: `Filling ${target}`, mode: "working" }).catch(() => {
|
|
2460
|
+
});
|
|
2174
2461
|
await this.session.transport.highlightElement?.(this.tabId, el.box, `fill ${target}`).catch(() => {
|
|
2175
2462
|
});
|
|
2176
2463
|
await this.mouseClick(el.center.x, el.center.y);
|
|
@@ -2246,17 +2533,23 @@ var PageController = class {
|
|
|
2246
2533
|
}
|
|
2247
2534
|
const mask = modifierBitmask(mods);
|
|
2248
2535
|
const def = resolveKeyDef(keyPart);
|
|
2249
|
-
|
|
2250
|
-
|
|
2536
|
+
const commands = resolveEditingCommands(def.key, mods);
|
|
2537
|
+
const hasAccel = mods.includes("Meta") || mods.includes("Control");
|
|
2538
|
+
const activeMods = [];
|
|
2539
|
+
for (const m of mods) {
|
|
2540
|
+
activeMods.push(m);
|
|
2541
|
+
await this.keyDown(m, modifierBitmask(activeMods));
|
|
2542
|
+
}
|
|
2251
2543
|
await this.session.send("Input.dispatchKeyEvent", {
|
|
2252
|
-
type: def.text ? "keyDown" : "rawKeyDown",
|
|
2544
|
+
type: def.text && !hasAccel ? "keyDown" : "rawKeyDown",
|
|
2253
2545
|
key: def.key,
|
|
2254
2546
|
code: def.code,
|
|
2255
2547
|
windowsVirtualKeyCode: def.keyCode,
|
|
2256
2548
|
nativeVirtualKeyCode: def.keyCode,
|
|
2257
|
-
text: def.text,
|
|
2549
|
+
text: hasAccel ? void 0 : def.text,
|
|
2258
2550
|
unmodifiedText: def.text,
|
|
2259
|
-
modifiers: mask
|
|
2551
|
+
modifiers: mask,
|
|
2552
|
+
...commands.length ? { commands } : {}
|
|
2260
2553
|
});
|
|
2261
2554
|
await this.session.send("Input.dispatchKeyEvent", {
|
|
2262
2555
|
type: "keyUp",
|
|
@@ -2266,10 +2559,192 @@ var PageController = class {
|
|
|
2266
2559
|
nativeVirtualKeyCode: def.keyCode,
|
|
2267
2560
|
modifiers: mask
|
|
2268
2561
|
});
|
|
2269
|
-
for (
|
|
2270
|
-
|
|
2562
|
+
for (let i = mods.length - 1; i >= 0; i--) {
|
|
2563
|
+
activeMods.pop();
|
|
2564
|
+
await this.keyUp(mods[i], modifierBitmask(activeMods));
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
/**
|
|
2568
|
+
* Fills multiple form fields (text, select, checkbox, radio) in a single structured call.
|
|
2569
|
+
*/
|
|
2570
|
+
async fillForm(fields) {
|
|
2571
|
+
const filled = [];
|
|
2572
|
+
for (const field of fields) {
|
|
2573
|
+
const kind = field.type ?? "auto";
|
|
2574
|
+
let detectedType = kind;
|
|
2575
|
+
if (kind === "auto") {
|
|
2576
|
+
const el = await this.resolveElement(field.target);
|
|
2577
|
+
const meta = await this.session.send("Runtime.callFunctionOn", {
|
|
2578
|
+
objectId: el.objectId,
|
|
2579
|
+
functionDeclaration: `function() { return { tag: (this.tagName || '').toLowerCase(), inputType: (this.type || '').toLowerCase() }; }`,
|
|
2580
|
+
returnByValue: true
|
|
2581
|
+
}, { sessionId: el.sessionId }).catch(() => null);
|
|
2582
|
+
const tag = meta?.result?.value?.tag ?? "";
|
|
2583
|
+
const inputType = meta?.result?.value?.inputType ?? "";
|
|
2584
|
+
if (tag === "select")
|
|
2585
|
+
detectedType = "select";
|
|
2586
|
+
else if (inputType === "checkbox")
|
|
2587
|
+
detectedType = "checkbox";
|
|
2588
|
+
else if (inputType === "radio")
|
|
2589
|
+
detectedType = "radio";
|
|
2590
|
+
else
|
|
2591
|
+
detectedType = "text";
|
|
2592
|
+
}
|
|
2593
|
+
if (detectedType === "select") {
|
|
2594
|
+
const selected = await this.selectOption(field.target, String(field.value));
|
|
2595
|
+
filled.push({ target: field.target, type: "select", value: selected });
|
|
2596
|
+
} else if (detectedType === "checkbox") {
|
|
2597
|
+
const desired = field.value === true || String(field.value).toLowerCase() === "true";
|
|
2598
|
+
const el = await this.resolveElement(field.target);
|
|
2599
|
+
const current = await this.session.send("Runtime.callFunctionOn", {
|
|
2600
|
+
objectId: el.objectId,
|
|
2601
|
+
functionDeclaration: `function() { return Boolean(this.checked || this.getAttribute?.('aria-checked') === 'true'); }`,
|
|
2602
|
+
returnByValue: true
|
|
2603
|
+
}, { sessionId: el.sessionId }).catch(() => null);
|
|
2604
|
+
if (Boolean(current?.result?.value) !== desired) {
|
|
2605
|
+
await this.click(field.target);
|
|
2606
|
+
}
|
|
2607
|
+
filled.push({ target: field.target, type: "checkbox", value: desired });
|
|
2608
|
+
} else if (detectedType === "radio") {
|
|
2609
|
+
await this.click(field.target);
|
|
2610
|
+
filled.push({ target: field.target, type: "radio", value: field.value });
|
|
2611
|
+
} else {
|
|
2612
|
+
await this.fill(field.target, String(field.value));
|
|
2613
|
+
filled.push({ target: field.target, type: "text", value: String(field.value) });
|
|
2614
|
+
}
|
|
2615
|
+
}
|
|
2616
|
+
return { filled };
|
|
2271
2617
|
}
|
|
2272
|
-
|
|
2618
|
+
/**
|
|
2619
|
+
* Displays an interactive in-page Human Handoff card ("Your Turn") and pauses until
|
|
2620
|
+
* the user clicks "Done, Continue AI" or "Cancel" inside the browser tab.
|
|
2621
|
+
* Works identically in both Chrome Extension mode and Direct CDP mode.
|
|
2622
|
+
*/
|
|
2623
|
+
async requestHumanHandoff(message, options = {}) {
|
|
2624
|
+
const timeoutMs = options.timeoutMs ?? 3e5;
|
|
2625
|
+
const start = Date.now();
|
|
2626
|
+
const bindingName = "__browserpilotHandoffDone";
|
|
2627
|
+
await this.session.ensureBaseDomains();
|
|
2628
|
+
await this.session.send("Runtime.addBinding", { name: bindingName }).catch(() => {
|
|
2629
|
+
});
|
|
2630
|
+
await this.session.transport.setControlBanner?.(this.tabId, { active: true, status: "Waiting for you...", mode: "handoff" }).catch(() => {
|
|
2631
|
+
});
|
|
2632
|
+
let resolveAction;
|
|
2633
|
+
const decisionPromise = new Promise((resolve) => {
|
|
2634
|
+
resolveAction = resolve;
|
|
2635
|
+
});
|
|
2636
|
+
const offBinding = this.session.on("Runtime.bindingCalled", (params) => {
|
|
2637
|
+
if (params.name !== bindingName)
|
|
2638
|
+
return;
|
|
2639
|
+
const payload = String(params.payload ?? "");
|
|
2640
|
+
resolveAction(payload === "cancel" ? "cancel" : "resume");
|
|
2641
|
+
});
|
|
2642
|
+
const injectCard = async () => {
|
|
2643
|
+
await this.evaluate(`(() => {
|
|
2644
|
+
const old = document.getElementById('__browserpilot_handoff_host');
|
|
2645
|
+
if (old) old.remove();
|
|
2646
|
+
const host = document.createElement('div');
|
|
2647
|
+
host.id = '__browserpilot_handoff_host';
|
|
2648
|
+
host.style.cssText = 'position:fixed;inset:0;pointer-events:none;z-index:2147483647;';
|
|
2649
|
+
const shadow = host.attachShadow({ mode: 'closed' });
|
|
2650
|
+
const style = document.createElement('style');
|
|
2651
|
+
style.textContent = \`
|
|
2652
|
+
.bp-edge {
|
|
2653
|
+
position: fixed; inset: 0; pointer-events: none;
|
|
2654
|
+
box-shadow: inset 0 0 0 3px rgba(245, 158, 11, 0.72), inset 0 0 26px rgba(245, 158, 11, 0.22);
|
|
2655
|
+
}
|
|
2656
|
+
.bp-card {
|
|
2657
|
+
position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
|
|
2658
|
+
pointer-events: auto;
|
|
2659
|
+
display: flex; align-items: center; gap: 14px; max-width: 560px;
|
|
2660
|
+
background: rgba(15, 23, 42, 0.94); color: #f8fafc;
|
|
2661
|
+
border: 1px solid rgba(251, 191, 36, 0.55);
|
|
2662
|
+
backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
|
|
2663
|
+
border-radius: 14px; padding: 12px 16px;
|
|
2664
|
+
box-shadow: 0 14px 36px rgba(2, 6, 23, 0.45);
|
|
2665
|
+
font: 500 13px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
|
2666
|
+
}
|
|
2667
|
+
.bp-dot {
|
|
2668
|
+
width: 10px; height: 10px; border-radius: 999px; flex: none;
|
|
2669
|
+
background: #fbbf24; box-shadow: 0 0 10px #f59e0b;
|
|
2670
|
+
}
|
|
2671
|
+
.bp-msg { flex: 1; color: #f1f5f9; }
|
|
2672
|
+
.bp-title { font-weight: 700; font-size: 12px; color: #fcd34d; margin-bottom: 2px; }
|
|
2673
|
+
.bp-actions { display: flex; gap: 8px; flex: none; }
|
|
2674
|
+
button {
|
|
2675
|
+
cursor: pointer; border-radius: 8px; border: 1px solid transparent;
|
|
2676
|
+
padding: 7px 12px; font: 600 12px/1 -apple-system, BlinkMacSystemFont, sans-serif;
|
|
2677
|
+
}
|
|
2678
|
+
.bp-resume { background: linear-gradient(135deg, #2dd4bf, #06b6d4); color: #042f2e; }
|
|
2679
|
+
.bp-resume:hover { filter: brightness(1.06); }
|
|
2680
|
+
.bp-cancel { background: rgba(148, 163, 184, 0.16); color: #cbd5e1; border-color: rgba(148, 163, 184, 0.3); }
|
|
2681
|
+
.bp-cancel:hover { background: rgba(148, 163, 184, 0.28); }
|
|
2682
|
+
\`;
|
|
2683
|
+
shadow.appendChild(style);
|
|
2684
|
+
const edge = document.createElement('div');
|
|
2685
|
+
edge.className = 'bp-edge';
|
|
2686
|
+
const card = document.createElement('div');
|
|
2687
|
+
card.className = 'bp-card';
|
|
2688
|
+
const dot = document.createElement('div');
|
|
2689
|
+
dot.className = 'bp-dot';
|
|
2690
|
+
const msgWrap = document.createElement('div');
|
|
2691
|
+
msgWrap.className = 'bp-msg';
|
|
2692
|
+
const title = document.createElement('div');
|
|
2693
|
+
title.className = 'bp-title';
|
|
2694
|
+
title.textContent = 'BrowserPilot \xB7 Your Turn';
|
|
2695
|
+
const body = document.createElement('div');
|
|
2696
|
+
body.textContent = ${JSON.stringify(message)};
|
|
2697
|
+
msgWrap.append(title, body);
|
|
2698
|
+
const actions = document.createElement('div');
|
|
2699
|
+
actions.className = 'bp-actions';
|
|
2700
|
+
const btnResume = document.createElement('button');
|
|
2701
|
+
btnResume.className = 'bp-resume';
|
|
2702
|
+
btnResume.textContent = 'Done, Continue AI';
|
|
2703
|
+
btnResume.onclick = () => { globalThis.${bindingName}?.('resume'); host.remove(); };
|
|
2704
|
+
const btnCancel = document.createElement('button');
|
|
2705
|
+
btnCancel.className = 'bp-cancel';
|
|
2706
|
+
btnCancel.textContent = 'Cancel';
|
|
2707
|
+
btnCancel.onclick = () => { globalThis.${bindingName}?.('cancel'); host.remove(); };
|
|
2708
|
+
actions.append(btnResume, btnCancel);
|
|
2709
|
+
card.append(dot, msgWrap, actions);
|
|
2710
|
+
shadow.append(edge, card);
|
|
2711
|
+
(document.documentElement || document.body).appendChild(host);
|
|
2712
|
+
})()`).catch(() => {
|
|
2713
|
+
});
|
|
2714
|
+
};
|
|
2715
|
+
await injectCard();
|
|
2716
|
+
const offLoad = this.session.on("Page.loadEventFired", () => {
|
|
2717
|
+
void injectCard();
|
|
2718
|
+
});
|
|
2719
|
+
let timer = null;
|
|
2720
|
+
try {
|
|
2721
|
+
const outcome = await Promise.race([
|
|
2722
|
+
decisionPromise,
|
|
2723
|
+
new Promise((resolve) => {
|
|
2724
|
+
timer = setTimeout(() => resolve("timeout"), timeoutMs);
|
|
2725
|
+
})
|
|
2726
|
+
]);
|
|
2727
|
+
const meta = await this.getUrlAndTitle().catch(() => ({ url: "", title: "" }));
|
|
2728
|
+
return {
|
|
2729
|
+
resumed: outcome === "resume",
|
|
2730
|
+
cancelled: outcome === "cancel",
|
|
2731
|
+
timedOut: outcome === "timeout",
|
|
2732
|
+
elapsedMs: Date.now() - start,
|
|
2733
|
+
url: meta.url,
|
|
2734
|
+
title: meta.title
|
|
2735
|
+
};
|
|
2736
|
+
} finally {
|
|
2737
|
+
if (timer)
|
|
2738
|
+
clearTimeout(timer);
|
|
2739
|
+
offBinding();
|
|
2740
|
+
offLoad();
|
|
2741
|
+
await this.evaluate(`document.getElementById('__browserpilot_handoff_host')?.remove()`).catch(() => {
|
|
2742
|
+
});
|
|
2743
|
+
await this.session.transport.setControlBanner?.(this.tabId, { active: true, status: "Active", mode: "working" }).catch(() => {
|
|
2744
|
+
});
|
|
2745
|
+
}
|
|
2746
|
+
}
|
|
2747
|
+
async keyDown(keyName, modifiers = 0) {
|
|
2273
2748
|
const def = resolveKeyDef(keyName);
|
|
2274
2749
|
await this.session.send("Input.dispatchKeyEvent", {
|
|
2275
2750
|
type: def.text ? "keyDown" : "rawKeyDown",
|
|
@@ -2277,17 +2752,19 @@ var PageController = class {
|
|
|
2277
2752
|
code: def.code,
|
|
2278
2753
|
windowsVirtualKeyCode: def.keyCode,
|
|
2279
2754
|
nativeVirtualKeyCode: def.keyCode,
|
|
2280
|
-
text: def.text
|
|
2755
|
+
text: def.text,
|
|
2756
|
+
modifiers
|
|
2281
2757
|
});
|
|
2282
2758
|
}
|
|
2283
|
-
async keyUp(keyName) {
|
|
2759
|
+
async keyUp(keyName, modifiers = 0) {
|
|
2284
2760
|
const def = resolveKeyDef(keyName);
|
|
2285
2761
|
await this.session.send("Input.dispatchKeyEvent", {
|
|
2286
2762
|
type: "keyUp",
|
|
2287
2763
|
key: def.key,
|
|
2288
2764
|
code: def.code,
|
|
2289
2765
|
windowsVirtualKeyCode: def.keyCode,
|
|
2290
|
-
nativeVirtualKeyCode: def.keyCode
|
|
2766
|
+
nativeVirtualKeyCode: def.keyCode,
|
|
2767
|
+
modifiers
|
|
2291
2768
|
});
|
|
2292
2769
|
}
|
|
2293
2770
|
async selectOption(target, valueOrLabel) {
|
|
@@ -2339,46 +2816,122 @@ var PageController = class {
|
|
|
2339
2816
|
if (format !== "png" && options.quality !== void 0) {
|
|
2340
2817
|
params.quality = options.quality;
|
|
2341
2818
|
}
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
}
|
|
2345
|
-
|
|
2346
|
-
const
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
height: Math.min(size.height, 16384),
|
|
2352
|
-
scale: 1
|
|
2353
|
-
};
|
|
2354
|
-
params.captureBeyondViewport = true;
|
|
2355
|
-
} else {
|
|
2819
|
+
let region;
|
|
2820
|
+
await this.session.transport.setOverlayVisible?.(this.tabId, false).catch(() => {
|
|
2821
|
+
});
|
|
2822
|
+
await this.evaluate(`(() => {
|
|
2823
|
+
const el = document.getElementById('__browserpilot_ui_host');
|
|
2824
|
+
if (el) el.style.visibility = 'hidden';
|
|
2825
|
+
})()`).catch(() => {
|
|
2826
|
+
});
|
|
2827
|
+
try {
|
|
2356
2828
|
const metrics = await this.session.send("Page.getLayoutMetrics");
|
|
2357
|
-
|
|
2829
|
+
const vp = metrics.cssVisualViewport ?? { pageX: 0, pageY: 0, clientWidth: 1280, clientHeight: 720 };
|
|
2830
|
+
if (options.grid) {
|
|
2831
|
+
const step = options.gridStep ?? (options.zoom ? 50 : 200);
|
|
2832
|
+
const originX = options.zoom ? options.zoom.x : 0;
|
|
2833
|
+
const originY = options.zoom ? options.zoom.y : 0;
|
|
2834
|
+
const spanW = options.zoom ? options.zoom.width : vp.clientWidth;
|
|
2835
|
+
const spanH = options.zoom ? options.zoom.height : vp.clientHeight;
|
|
2836
|
+
await this.evaluate(`(() => {
|
|
2837
|
+
document.getElementById('__browserpilot_grid_overlay')?.remove();
|
|
2838
|
+
const host = document.createElement('div');
|
|
2839
|
+
host.id = '__browserpilot_grid_overlay';
|
|
2840
|
+
host.style.cssText = 'position:fixed;inset:0;pointer-events:none;z-index:2147483646;';
|
|
2841
|
+
const shadow = host.attachShadow({ mode: 'closed' });
|
|
2842
|
+
const step = ${step};
|
|
2843
|
+
const startX = Math.floor(${originX} / step) * step;
|
|
2844
|
+
const endX = ${originX + spanW};
|
|
2845
|
+
const startY = Math.floor(${originY} / step) * step;
|
|
2846
|
+
const endY = ${originY + spanH};
|
|
2847
|
+
for (let x = startX; x <= endX; x += step) {
|
|
2848
|
+
const line = document.createElement('div');
|
|
2849
|
+
line.style.cssText = 'position:fixed;top:0;bottom:0;left:' + x + 'px;width:1px;background:rgba(6,182,212,0.38);';
|
|
2850
|
+
shadow.appendChild(line);
|
|
2851
|
+
const lbl = document.createElement('div');
|
|
2852
|
+
lbl.textContent = String(x);
|
|
2853
|
+
lbl.style.cssText = 'position:fixed;top:' + (${originY} + 2) + 'px;left:' + (x + 3) + 'px;background:rgba(15,23,42,0.82);color:#67e8f9;font:700 10px/1 ui-monospace,monospace;padding:1px 3px;border-radius:3px;';
|
|
2854
|
+
shadow.appendChild(lbl);
|
|
2855
|
+
}
|
|
2856
|
+
for (let y = startY; y <= endY; y += step) {
|
|
2857
|
+
const line = document.createElement('div');
|
|
2858
|
+
line.style.cssText = 'position:fixed;left:0;right:0;top:' + y + 'px;height:1px;background:rgba(6,182,212,0.38);';
|
|
2859
|
+
shadow.appendChild(line);
|
|
2860
|
+
const lbl = document.createElement('div');
|
|
2861
|
+
lbl.textContent = String(y);
|
|
2862
|
+
lbl.style.cssText = 'position:fixed;left:' + (${originX} + 2) + 'px;top:' + (y + 3) + 'px;background:rgba(15,23,42,0.82);color:#67e8f9;font:700 10px/1 ui-monospace,monospace;padding:1px 3px;border-radius:3px;';
|
|
2863
|
+
shadow.appendChild(lbl);
|
|
2864
|
+
}
|
|
2865
|
+
(document.documentElement || document.body).appendChild(host);
|
|
2866
|
+
})()`).catch(() => {
|
|
2867
|
+
});
|
|
2868
|
+
}
|
|
2869
|
+
if (options.zoom) {
|
|
2870
|
+
const scale = Math.max(1, Math.min(4, options.zoom.scale ?? 2));
|
|
2871
|
+
params.clip = {
|
|
2872
|
+
x: vp.pageX + options.zoom.x,
|
|
2873
|
+
y: vp.pageY + options.zoom.y,
|
|
2874
|
+
width: options.zoom.width,
|
|
2875
|
+
height: options.zoom.height,
|
|
2876
|
+
scale
|
|
2877
|
+
};
|
|
2878
|
+
region = {
|
|
2879
|
+
x: options.zoom.x,
|
|
2880
|
+
y: options.zoom.y,
|
|
2881
|
+
width: options.zoom.width,
|
|
2882
|
+
height: options.zoom.height,
|
|
2883
|
+
scale
|
|
2884
|
+
};
|
|
2885
|
+
} else if (options.clip) {
|
|
2886
|
+
params.clip = { ...options.clip, scale: 1 };
|
|
2887
|
+
region = { ...options.clip, scale: 1 };
|
|
2888
|
+
} else if (options.fullPage) {
|
|
2889
|
+
const size = metrics.cssContentSize ?? metrics.contentSize ?? { width: 1280, height: 720 };
|
|
2358
2890
|
params.clip = {
|
|
2359
|
-
x:
|
|
2360
|
-
y:
|
|
2361
|
-
width:
|
|
2362
|
-
height:
|
|
2891
|
+
x: 0,
|
|
2892
|
+
y: 0,
|
|
2893
|
+
width: size.width,
|
|
2894
|
+
height: Math.min(size.height, 16384),
|
|
2895
|
+
scale: 1
|
|
2896
|
+
};
|
|
2897
|
+
params.captureBeyondViewport = true;
|
|
2898
|
+
} else if (metrics.cssVisualViewport) {
|
|
2899
|
+
params.clip = {
|
|
2900
|
+
x: vp.pageX,
|
|
2901
|
+
y: vp.pageY,
|
|
2902
|
+
width: vp.clientWidth,
|
|
2903
|
+
height: vp.clientHeight,
|
|
2363
2904
|
scale: 1
|
|
2364
2905
|
};
|
|
2365
2906
|
}
|
|
2907
|
+
const res = await this.session.send("Page.captureScreenshot", params);
|
|
2908
|
+
let base64 = res.data;
|
|
2909
|
+
const mimeType = format === "jpeg" ? "image/jpeg" : format === "webp" ? "image/webp" : "image/png";
|
|
2910
|
+
if (options.maxDimension && options.maxDimension > 0) {
|
|
2911
|
+
const resized = await this.resizeBase64InPage(base64, mimeType, options.maxDimension, options.quality);
|
|
2912
|
+
if (resized)
|
|
2913
|
+
base64 = resized;
|
|
2914
|
+
}
|
|
2915
|
+
if (options.path) {
|
|
2916
|
+
const absPath = path2.resolve(options.path);
|
|
2917
|
+
await fs2.mkdir(path2.dirname(absPath), { recursive: true });
|
|
2918
|
+
await fs2.writeFile(absPath, Buffer.from(base64, "base64"));
|
|
2919
|
+
return { base64, mimeType, path: absPath, ...region ? { region } : {} };
|
|
2920
|
+
}
|
|
2921
|
+
return { base64, mimeType, ...region ? { region } : {} };
|
|
2922
|
+
} finally {
|
|
2923
|
+
if (options.grid) {
|
|
2924
|
+
await this.evaluate(`document.getElementById('__browserpilot_grid_overlay')?.remove()`).catch(() => {
|
|
2925
|
+
});
|
|
2926
|
+
}
|
|
2927
|
+
await this.evaluate(`(() => {
|
|
2928
|
+
const el = document.getElementById('__browserpilot_ui_host');
|
|
2929
|
+
if (el) el.style.visibility = '';
|
|
2930
|
+
})()`).catch(() => {
|
|
2931
|
+
});
|
|
2932
|
+
await this.session.transport.setOverlayVisible?.(this.tabId, true).catch(() => {
|
|
2933
|
+
});
|
|
2366
2934
|
}
|
|
2367
|
-
const res = await this.session.send("Page.captureScreenshot", params);
|
|
2368
|
-
let base64 = res.data;
|
|
2369
|
-
const mimeType = format === "jpeg" ? "image/jpeg" : format === "webp" ? "image/webp" : "image/png";
|
|
2370
|
-
if (options.maxDimension && options.maxDimension > 0) {
|
|
2371
|
-
const resized = await this.resizeBase64InPage(base64, mimeType, options.maxDimension, options.quality);
|
|
2372
|
-
if (resized)
|
|
2373
|
-
base64 = resized;
|
|
2374
|
-
}
|
|
2375
|
-
if (options.path) {
|
|
2376
|
-
const absPath = path2.resolve(options.path);
|
|
2377
|
-
await fs2.mkdir(path2.dirname(absPath), { recursive: true });
|
|
2378
|
-
await fs2.writeFile(absPath, Buffer.from(base64, "base64"));
|
|
2379
|
-
return { base64, mimeType, path: absPath };
|
|
2380
|
-
}
|
|
2381
|
-
return { base64, mimeType };
|
|
2382
2935
|
}
|
|
2383
2936
|
async resizeBase64InPage(base64, mimeType, maxDimension, quality) {
|
|
2384
2937
|
try {
|
|
@@ -4993,6 +5546,14 @@ var SessionManager = class {
|
|
|
4993
5546
|
const t = await getTab(opts?.tab);
|
|
4994
5547
|
return await t.page.screenshot(opts);
|
|
4995
5548
|
},
|
|
5549
|
+
handoff: async (message, opts) => {
|
|
5550
|
+
const t = await getTab(opts?.tab);
|
|
5551
|
+
return await t.page.requestHumanHandoff(message, { timeoutMs: opts?.timeoutMs });
|
|
5552
|
+
},
|
|
5553
|
+
fillForm: async (fields, opts) => {
|
|
5554
|
+
const t = await getTab(opts?.tab);
|
|
5555
|
+
return await t.page.fillForm(fields);
|
|
5556
|
+
},
|
|
4996
5557
|
screenshotWithLabels: async (opts) => {
|
|
4997
5558
|
const t = await getTab(opts?.tab);
|
|
4998
5559
|
return await t.captureLabeledScreenshot(opts);
|
|
@@ -5214,7 +5775,7 @@ var TOOL_DEFINITIONS = [
|
|
|
5214
5775
|
},
|
|
5215
5776
|
{
|
|
5216
5777
|
name: "browser_screenshot",
|
|
5217
|
-
description: "Capture a CSS-scaled screenshot (viewport, fullPage, or
|
|
5778
|
+
description: "Capture a clean CSS-scaled screenshot (viewport, fullPage, clip, or 2x zoomed region) with optional coordinate grid overlay for Canvas/Figma/PDF targeting.",
|
|
5218
5779
|
inputSchema: {
|
|
5219
5780
|
type: "object",
|
|
5220
5781
|
properties: {
|
|
@@ -5225,9 +5786,23 @@ var TOOL_DEFINITIONS = [
|
|
|
5225
5786
|
quality: { type: "number" },
|
|
5226
5787
|
maxDimension: { type: "number", description: "Downscale longest edge to save vision tokens (default 1440)" },
|
|
5227
5788
|
path: { type: "string", description: "Optional local file path to save the screenshot" },
|
|
5789
|
+
grid: { type: "boolean", description: "Overlay a translucent pixel coordinate grid with axis labels (useful for Canvas/PDF)" },
|
|
5790
|
+
gridStep: { type: "number", description: "Grid spacing in CSS pixels (default 200, or 50 when zoomed)" },
|
|
5228
5791
|
clip: {
|
|
5229
5792
|
type: "object",
|
|
5230
5793
|
properties: { x: { type: "number" }, y: { type: "number" }, width: { type: "number" }, height: { type: "number" } }
|
|
5794
|
+
},
|
|
5795
|
+
zoom: {
|
|
5796
|
+
type: "object",
|
|
5797
|
+
description: "Crop & upscale a viewport region (e.g. 2x zoom into tiny icons/charts)",
|
|
5798
|
+
properties: {
|
|
5799
|
+
x: { type: "number" },
|
|
5800
|
+
y: { type: "number" },
|
|
5801
|
+
width: { type: "number" },
|
|
5802
|
+
height: { type: "number" },
|
|
5803
|
+
scale: { type: "number" }
|
|
5804
|
+
},
|
|
5805
|
+
required: ["x", "y", "width", "height"]
|
|
5231
5806
|
}
|
|
5232
5807
|
}
|
|
5233
5808
|
}
|
|
@@ -5290,16 +5865,17 @@ var TOOL_DEFINITIONS = [
|
|
|
5290
5865
|
},
|
|
5291
5866
|
{
|
|
5292
5867
|
name: "browser_click",
|
|
5293
|
-
description: "Perform a browser-trusted CDP mouse click on a snapshot ref, CSS selector,
|
|
5868
|
+
description: "Perform a browser-trusted CDP mouse click on a snapshot ref, CSS selector, rich locator, or (x,y) coordinates. Checks pre-click stability & occlusion automatically unless force=true.",
|
|
5294
5869
|
inputSchema: {
|
|
5295
5870
|
type: "object",
|
|
5296
5871
|
properties: {
|
|
5297
|
-
target: { type: "string", description: "Ref (e5), CSS selector, or text=..." },
|
|
5872
|
+
target: { type: "string", description: "Ref (e5), CSS selector, role=..., label=..., or text=..." },
|
|
5298
5873
|
x: { type: "number" },
|
|
5299
5874
|
y: { type: "number" },
|
|
5300
5875
|
button: { type: "string", enum: ["left", "right", "middle"] },
|
|
5301
5876
|
clickCount: { type: "number" },
|
|
5302
5877
|
modifiers: { type: "array", items: { type: "string", enum: ["Alt", "Control", "Meta", "Shift"] } },
|
|
5878
|
+
force: { type: "boolean", description: "Bypass pre-click occlusion & stability checks" },
|
|
5303
5879
|
tabId: { type: "number" },
|
|
5304
5880
|
sessionId: { type: "string" }
|
|
5305
5881
|
}
|
|
@@ -5809,6 +6385,44 @@ var TOOL_DEFINITIONS = [
|
|
|
5809
6385
|
sessionId: { type: "string" }
|
|
5810
6386
|
}
|
|
5811
6387
|
}
|
|
6388
|
+
},
|
|
6389
|
+
{
|
|
6390
|
+
name: "browser_fill_form",
|
|
6391
|
+
description: "Fill multiple form fields (text inputs, textareas, selects, checkboxes, radios) in a single round-trip with automatic field-type detection.",
|
|
6392
|
+
inputSchema: {
|
|
6393
|
+
type: "object",
|
|
6394
|
+
properties: {
|
|
6395
|
+
fields: {
|
|
6396
|
+
type: "array",
|
|
6397
|
+
items: {
|
|
6398
|
+
type: "object",
|
|
6399
|
+
properties: {
|
|
6400
|
+
target: { type: "string", description: "Ref (e4), CSS selector, label=..., or placeholder=..." },
|
|
6401
|
+
value: { description: "Text value, select option, or boolean for checkboxes" },
|
|
6402
|
+
type: { type: "string", enum: ["auto", "text", "select", "checkbox", "radio"] }
|
|
6403
|
+
},
|
|
6404
|
+
required: ["target", "value"]
|
|
6405
|
+
}
|
|
6406
|
+
},
|
|
6407
|
+
tabId: { type: "number" },
|
|
6408
|
+
sessionId: { type: "string" }
|
|
6409
|
+
},
|
|
6410
|
+
required: ["fields"]
|
|
6411
|
+
}
|
|
6412
|
+
},
|
|
6413
|
+
{
|
|
6414
|
+
name: "browser_handoff",
|
|
6415
|
+
description: 'Display an interactive in-page Human Handoff card ("BrowserPilot \xB7 Your Turn") asking the user to perform a manual step (login, 2FA, CAPTCHA, review) and wait until they click "Done, Continue AI" or "Cancel".',
|
|
6416
|
+
inputSchema: {
|
|
6417
|
+
type: "object",
|
|
6418
|
+
properties: {
|
|
6419
|
+
message: { type: "string", description: "Clear instruction shown to the user inside Chrome" },
|
|
6420
|
+
timeoutMs: { type: "number", description: "Max wait time in ms (default 300000 = 5 min)" },
|
|
6421
|
+
tabId: { type: "number" },
|
|
6422
|
+
sessionId: { type: "string" }
|
|
6423
|
+
},
|
|
6424
|
+
required: ["message"]
|
|
6425
|
+
}
|
|
5812
6426
|
}
|
|
5813
6427
|
];
|
|
5814
6428
|
async function executeTool(ctx, name, args = {}) {
|
|
@@ -5906,14 +6520,21 @@ ${formatFeedbackSuffix(feedback)}`);
|
|
|
5906
6520
|
quality: args.quality,
|
|
5907
6521
|
maxDimension: args.maxDimension ?? 1440,
|
|
5908
6522
|
path: args.path,
|
|
5909
|
-
|
|
6523
|
+
grid: args.grid,
|
|
6524
|
+
gridStep: args.gridStep,
|
|
6525
|
+
clip: args.clip,
|
|
6526
|
+
zoom: args.zoom
|
|
5910
6527
|
});
|
|
6528
|
+
const regionNote = shot.region ? ` [region: x=${shot.region.x}, y=${shot.region.y}, ${shot.region.width}x${shot.region.height}, scale=${shot.region.scale}x]` : "";
|
|
5911
6529
|
return {
|
|
5912
6530
|
content: [
|
|
5913
|
-
{
|
|
6531
|
+
{
|
|
6532
|
+
type: "text",
|
|
6533
|
+
text: `${shot.path ? `Screenshot saved to ${shot.path}` : `Captured screenshot of tab [${tab.tabId}]`}${regionNote}`
|
|
6534
|
+
},
|
|
5914
6535
|
{ type: "image", data: shot.base64, mimeType: shot.mimeType }
|
|
5915
6536
|
],
|
|
5916
|
-
meta: { path: shot.path, tabId: tab.tabId }
|
|
6537
|
+
meta: { path: shot.path, tabId: tab.tabId, region: shot.region }
|
|
5917
6538
|
};
|
|
5918
6539
|
}
|
|
5919
6540
|
case "browser_labeled_screenshot": {
|
|
@@ -5969,7 +6590,8 @@ ${som.snapshot.text}` },
|
|
|
5969
6590
|
() => tab.page.click(target, {
|
|
5970
6591
|
button: args.button,
|
|
5971
6592
|
clickCount: args.clickCount,
|
|
5972
|
-
modifiers: args.modifiers
|
|
6593
|
+
modifiers: args.modifiers,
|
|
6594
|
+
force: Boolean(args.force)
|
|
5973
6595
|
})
|
|
5974
6596
|
);
|
|
5975
6597
|
return textResult(`Clicked ${typeof target === "string" ? target : `(${target.x}, ${target.y})`}
|
|
@@ -6008,14 +6630,17 @@ ${formatFeedbackSuffix(feedback)}`);
|
|
|
6008
6630
|
}
|
|
6009
6631
|
case "browser_scroll": {
|
|
6010
6632
|
const tab = await resolveTab();
|
|
6011
|
-
await tab.page.scroll({
|
|
6633
|
+
const res = await tab.page.scroll({
|
|
6012
6634
|
target: args.target,
|
|
6013
6635
|
direction: args.direction,
|
|
6014
6636
|
amount: args.amount,
|
|
6015
6637
|
deltaX: args.deltaX,
|
|
6016
6638
|
deltaY: args.deltaY
|
|
6017
6639
|
});
|
|
6018
|
-
return textResult(
|
|
6640
|
+
return textResult(
|
|
6641
|
+
`Scrolled ${res.container} (moved=${res.scrolled}): scrollY=${res.scrollY}/${res.maxScrollY}px (${res.percentY}%)${res.atTop ? " [at top]" : ""}${res.atBottom ? " [at bottom]" : ""}`,
|
|
6642
|
+
{ ...res }
|
|
6643
|
+
);
|
|
6019
6644
|
}
|
|
6020
6645
|
case "browser_drag": {
|
|
6021
6646
|
const tab = await resolveTab();
|
|
@@ -6521,6 +7146,30 @@ React: ${JSON.stringify(reactInfo, null, 2)}` : ""
|
|
|
6521
7146
|
].filter(Boolean).join("\n")
|
|
6522
7147
|
);
|
|
6523
7148
|
}
|
|
7149
|
+
case "browser_fill_form": {
|
|
7150
|
+
const tab = await resolveTab();
|
|
7151
|
+
const fields = args.fields ?? [];
|
|
7152
|
+
if (!fields.length) throw new Error("Provide non-empty fields array");
|
|
7153
|
+
const { result, feedback } = await tab.runWithFeedback(() => tab.page.fillForm(fields));
|
|
7154
|
+
const summary = result.filled.map((f, i) => `${i + 1}. [${f.type}] ${f.target} = ${JSON.stringify(f.value)}`).join("\n");
|
|
7155
|
+
return textResult(`Filled ${result.filled.length} form field(s):
|
|
7156
|
+
${summary}
|
|
7157
|
+
${formatFeedbackSuffix(feedback)}`, {
|
|
7158
|
+
filled: result.filled
|
|
7159
|
+
});
|
|
7160
|
+
}
|
|
7161
|
+
case "browser_handoff": {
|
|
7162
|
+
const tab = await resolveTab();
|
|
7163
|
+
const msg = String(args.message ?? 'Please complete the manual step in Chrome, then click "Done, Continue AI".');
|
|
7164
|
+
const res = await tab.page.requestHumanHandoff(msg, {
|
|
7165
|
+
timeoutMs: typeof args.timeoutMs === "number" ? args.timeoutMs : void 0
|
|
7166
|
+
});
|
|
7167
|
+
const status = res.resumed ? "Resumed by user" : res.cancelled ? "Cancelled by user" : "Timed out";
|
|
7168
|
+
return textResult(
|
|
7169
|
+
`${status} after ${Math.round(res.elapsedMs / 1e3)}s \u2014 URL: ${res.url}${res.title ? ` ("${res.title}")` : ""}`,
|
|
7170
|
+
{ ...res }
|
|
7171
|
+
);
|
|
7172
|
+
}
|
|
6524
7173
|
default:
|
|
6525
7174
|
return { content: [{ type: "text", text: `Unknown tool: ${name}` }], isError: true };
|
|
6526
7175
|
}
|
|
@@ -7219,7 +7868,7 @@ BrowserPilot controls the user's running Chrome browser (existing logins, cookie
|
|
|
7219
7868
|
- First call returns full AXTree with \`e1\`, \`e2\`... refs + viewport/scroll info; subsequent calls return diffs (pass \`--full\` / \`showDiffSinceLastCall: false\` for full output).
|
|
7220
7869
|
- Use \`browser_labeled_screenshot\` / \`browserpilot labeled-screenshot <path>\` when spatial layout or icon-only buttons matter.
|
|
7221
7870
|
3. **Act (trusted CDP input)**:
|
|
7222
|
-
- \`click
|
|
7871
|
+
- \`click\` (checks occlusion & stability; pass \`force: true\` to override), \`fill\`, \`fill_form\`, \`type\`, \`press_key\` (supports macOS/Chrome \`Cmd+a/c/v/z\` editing commands), \`hover\`, \`scroll\` (smart inner SPA container detection + position telemetry), \`drag\`, \`select_option\`, \`upload_file\`, \`batch\`, \`handoff\` (in-page "Your Turn" card until user clicks Continue).
|
|
7223
7872
|
- Targets accept refs (\`e5\`), CSS selectors (\`#submit\`), \`role=button[name="Save"]\`, \`label=...\`, \`placeholder=...\`, \`text=...\`, \`>> nth=N\`, or \`pinned\`.
|
|
7224
7873
|
4. **DevTools Debugging**:
|
|
7225
7874
|
- \`browser_console\` / \`browserpilot console --level error --since-last\`
|