@generativereality/browser-automation 0.4.16 → 0.4.18
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/.claude-plugin/plugin.json +1 -1
- package/README.md +3 -2
- package/dist/index.js +300 -44
- package/package.json +1 -1
- package/skills/browser/SKILL.md +13 -5
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "browser-automation",
|
|
3
3
|
"description": "Daemonless, per-tab-isolated browser automation for Claude Code via the `browser-automation` CLI. Drives one shared headed Chrome (persistent profile + extensions) over per-target CDP — many parallel sessions, no focus stealing, nothing long-lived to crash.",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.18",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "generativereality"
|
|
7
7
|
},
|
package/README.md
CHANGED
|
@@ -170,7 +170,8 @@ same one-action-per-snapshot rule as Playwright refs.
|
|
|
170
170
|
button inside the zone works even when the zone div itself isn't
|
|
171
171
|
snapshot-interactive. After dropping, confirm with `read`/`screenshot` and pull
|
|
172
172
|
the result (often a `download --url` endpoint).
|
|
173
|
-
- `launch`
|
|
173
|
+
- `launch` works on macOS, Linux and Windows (on Windows it needs only Node; set
|
|
174
|
+
`BROWSER_AUTOMATION_CHROME` if `chrome.exe` is somewhere unusual). Anywhere else, start Chrome with
|
|
174
175
|
`--remote-debugging-port=<your port> --user-data-dir="<profile>"` — `browser-automation doctor` prints the port.
|
|
175
176
|
|
|
176
177
|
## Environment
|
|
@@ -186,7 +187,7 @@ same one-action-per-snapshot rule as Playwright refs.
|
|
|
186
187
|
`doctor` prints it too, and warns if another user holds it.
|
|
187
188
|
- `BROWSER_AUTOMATION_CDP` — full CDP host, overriding the port entirely.
|
|
188
189
|
- `BROWSER_AUTOMATION_PROFILE` — Chrome profile dir for `launch`. Default `~/.browser-automation/chrome-profile`; a profile still at the old location inside Chrome's own folder is moved there on launch (`browser-automation profile --migrate` to do it by hand).
|
|
189
|
-
- `BAC_SHOW_TAB=1` — `--show` on every `goto`/`new`: select the tab in its window when the automation Chrome is already in front (never raises Chrome over another app).
|
|
190
|
+
- `BAC_SHOW_TAB=1` — `--show` on every `goto`/`new`: select the tab in its window when the automation Chrome is already in front (never raises Chrome over another app). macOS and Windows.
|
|
190
191
|
- `BAC_SESSION` — default session name for page commands.
|
|
191
192
|
|
|
192
193
|
## License
|
package/dist/index.js
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
import updateNotifier from "update-notifier";
|
|
3
3
|
import { cli, define } from "gunshi";
|
|
4
4
|
import consola, { consola as consola$1 } from "consola";
|
|
5
|
-
import { homedir } from "node:os";
|
|
5
|
+
import { homedir, tmpdir } from "node:os";
|
|
6
6
|
import { basename, dirname, extname, join, resolve } from "node:path";
|
|
7
|
-
import { appendFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
8
|
-
import { execFileSync, spawnSync } from "node:child_process";
|
|
7
|
+
import { appendFileSync, existsSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { execFileSync, spawn, spawnSync } from "node:child_process";
|
|
9
9
|
import { fileURLToPath } from "node:url";
|
|
10
10
|
//#region package.json
|
|
11
11
|
var name = "@generativereality/browser-automation";
|
|
12
|
-
var version = "0.4.
|
|
12
|
+
var version = "0.4.18";
|
|
13
13
|
var description = "Daemonless, per-tab-isolated browser automation CLI for Claude Code. Drives one shared headed Chrome (persistent profile + extensions) over per-target CDP — many parallel sessions, no focus stealing, nothing long-lived to crash.";
|
|
14
14
|
var package_default = {
|
|
15
15
|
name,
|
|
@@ -957,7 +957,7 @@ function gatherEvidence() {
|
|
|
957
957
|
}
|
|
958
958
|
/** Where `launch` writes Chrome's stdout/stderr — Chrome logs the real reason there. */
|
|
959
959
|
function chromeLogPath() {
|
|
960
|
-
const tmp = process.env.TMPDIR?.replace(/\/$/, "") || "/tmp";
|
|
960
|
+
const tmp = process.platform === "win32" ? tmpdir() : process.env.TMPDIR?.replace(/\/$/, "") || "/tmp";
|
|
961
961
|
return process.env.BROWSER_AUTOMATION_LOG || `${tmp}/chrome-${targetPort()}.log`;
|
|
962
962
|
}
|
|
963
963
|
function probeLogPath() {
|
|
@@ -1098,6 +1098,68 @@ async function withRendererDiagnosis(fn) {
|
|
|
1098
1098
|
}
|
|
1099
1099
|
}
|
|
1100
1100
|
//#endregion
|
|
1101
|
+
//#region src/core/windows-foreground.ts
|
|
1102
|
+
/** The script, with the port substituted. Exported so the test can read it. */
|
|
1103
|
+
function probeScript(port) {
|
|
1104
|
+
return [
|
|
1105
|
+
"$ErrorActionPreference = 'Stop'",
|
|
1106
|
+
"$ProgressPreference = 'SilentlyContinue'",
|
|
1107
|
+
"Add-Type -Namespace BrowserAutomation -Name User32 -MemberDefinition @'",
|
|
1108
|
+
"[DllImport(\"user32.dll\")] public static extern System.IntPtr GetForegroundWindow();",
|
|
1109
|
+
"[DllImport(\"user32.dll\")] public static extern uint GetWindowThreadProcessId(System.IntPtr hWnd, out uint processId);",
|
|
1110
|
+
"'@",
|
|
1111
|
+
"$U = [BrowserAutomation.User32]",
|
|
1112
|
+
"$h = $U::GetForegroundWindow()",
|
|
1113
|
+
"$fg = [uint32]0",
|
|
1114
|
+
"if ($h -ne [IntPtr]::Zero) { [void]$U::GetWindowThreadProcessId($h, [ref]$fg) }",
|
|
1115
|
+
`$re = '--remote-debugging-port=${port}(\\s|"|$)'`,
|
|
1116
|
+
"$sid = [System.Diagnostics.Process]::GetCurrentProcess().SessionId",
|
|
1117
|
+
"$b = Get-CimInstance Win32_Process -Filter \"Name = 'chrome.exe'\" |",
|
|
1118
|
+
" Where-Object { $_.SessionId -eq $sid -and $_.CommandLine -and $_.CommandLine -match $re -and $_.CommandLine -notmatch '--type=' } |",
|
|
1119
|
+
" Select-Object -First 1 -ExpandProperty ProcessId",
|
|
1120
|
+
"Write-Output ('BA-FG ' + $fg + ' ' + [int]$b)"
|
|
1121
|
+
].join("\n");
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Read the answer out of PowerShell's stdout.
|
|
1125
|
+
*
|
|
1126
|
+
* Scans for the line with the expected SHAPE rather than taking the first line:
|
|
1127
|
+
* a PowerShell child process can put other things on its streams (CLIXML
|
|
1128
|
+
* progress records, "Preparing modules for first use"), and keying on line one
|
|
1129
|
+
* is how a parser starts failing the day something precedes the answer.
|
|
1130
|
+
*/
|
|
1131
|
+
function parseProbe(stdout) {
|
|
1132
|
+
const m = /^BA-FG (\d+) (\d+)\s*$/m.exec(stdout);
|
|
1133
|
+
return m ? {
|
|
1134
|
+
foreground: Number(m[1]),
|
|
1135
|
+
browser: Number(m[2])
|
|
1136
|
+
} : null;
|
|
1137
|
+
}
|
|
1138
|
+
function windowsForeground(port) {
|
|
1139
|
+
const encoded = Buffer.from(probeScript(port), "utf16le").toString("base64");
|
|
1140
|
+
try {
|
|
1141
|
+
return parseProbe(execFileSync("powershell.exe", [
|
|
1142
|
+
"-NoProfile",
|
|
1143
|
+
"-NonInteractive",
|
|
1144
|
+
"-ExecutionPolicy",
|
|
1145
|
+
"Bypass",
|
|
1146
|
+
"-EncodedCommand",
|
|
1147
|
+
encoded
|
|
1148
|
+
], {
|
|
1149
|
+
encoding: "utf8",
|
|
1150
|
+
stdio: [
|
|
1151
|
+
"ignore",
|
|
1152
|
+
"pipe",
|
|
1153
|
+
"ignore"
|
|
1154
|
+
],
|
|
1155
|
+
timeout: 15e3,
|
|
1156
|
+
windowsHide: true
|
|
1157
|
+
}));
|
|
1158
|
+
} catch {
|
|
1159
|
+
return null;
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
//#endregion
|
|
1101
1163
|
//#region src/core/show.ts
|
|
1102
1164
|
/** On for this command: `--show`, or `BAC_SHOW_TAB=1` set once by a skill. */
|
|
1103
1165
|
function wantsShow(flag) {
|
|
@@ -1113,6 +1175,11 @@ function wantsShow(flag) {
|
|
|
1113
1175
|
function ourChromeIsFrontmost() {
|
|
1114
1176
|
const injected = process.env.BROWSER_AUTOMATION_TEST_FRONTMOST;
|
|
1115
1177
|
if (injected) return injected === "chrome";
|
|
1178
|
+
if (process.platform === "win32") {
|
|
1179
|
+
const w = windowsForeground(targetPort());
|
|
1180
|
+
if (!w || !w.foreground || !w.browser) return null;
|
|
1181
|
+
return w.foreground === w.browser;
|
|
1182
|
+
}
|
|
1116
1183
|
if (process.platform !== "darwin") return null;
|
|
1117
1184
|
try {
|
|
1118
1185
|
const asn = execFileSync("lsappinfo", ["front"], {
|
|
@@ -1156,7 +1223,7 @@ async function showTab(targetId) {
|
|
|
1156
1223
|
/** One line for the agent when it asked and did not get it — never silent, never loud. */
|
|
1157
1224
|
function describeNotShown(outcome) {
|
|
1158
1225
|
if (outcome === "not-frontmost") return "Tab not shown: another app (or another Chrome) is in front, and --show never brings this Chrome over it. It selects the tab once the automation Chrome is in front.";
|
|
1159
|
-
if (outcome === "unsupported") return "Tab not shown: cannot tell which app is in front here (macOS
|
|
1226
|
+
if (outcome === "unsupported") return "Tab not shown: cannot tell which app is in front here (supported on macOS and Windows), so --show did nothing rather than risk taking the screen.";
|
|
1160
1227
|
return null;
|
|
1161
1228
|
}
|
|
1162
1229
|
//#endregion
|
|
@@ -1622,7 +1689,7 @@ const snapshotCommand = define({
|
|
|
1622
1689
|
});
|
|
1623
1690
|
//#endregion
|
|
1624
1691
|
//#region src/commands/click.ts
|
|
1625
|
-
const sleep$
|
|
1692
|
+
const sleep$3 = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
1626
1693
|
const clickCommand = define({
|
|
1627
1694
|
name: "click",
|
|
1628
1695
|
description: "Click an element by ref (from the latest snapshot)",
|
|
@@ -1663,7 +1730,7 @@ const clickCommand = define({
|
|
|
1663
1730
|
while (Date.now() < focusDeadline) {
|
|
1664
1731
|
const f = await evalIn(`({f:document.hasFocus(),v:document.visibilityState})`);
|
|
1665
1732
|
if (f && f.f && f.v === "visible") break;
|
|
1666
|
-
await sleep$
|
|
1733
|
+
await sleep$3(50);
|
|
1667
1734
|
}
|
|
1668
1735
|
let prev = null;
|
|
1669
1736
|
let point = null;
|
|
@@ -1689,7 +1756,7 @@ const clickCommand = define({
|
|
|
1689
1756
|
x: a.x,
|
|
1690
1757
|
y: a.y
|
|
1691
1758
|
};
|
|
1692
|
-
await sleep$
|
|
1759
|
+
await sleep$3(60);
|
|
1693
1760
|
}
|
|
1694
1761
|
if (!point) {
|
|
1695
1762
|
consola$1.error(`<${ref}> not actionable for a trusted click: ${lastReason}`);
|
|
@@ -1704,7 +1771,7 @@ const clickCommand = define({
|
|
|
1704
1771
|
buttons: 0,
|
|
1705
1772
|
pointerType: "mouse"
|
|
1706
1773
|
});
|
|
1707
|
-
await sleep$
|
|
1774
|
+
await sleep$3(20);
|
|
1708
1775
|
await s.send("Input.dispatchMouseEvent", {
|
|
1709
1776
|
type: "mousePressed",
|
|
1710
1777
|
x,
|
|
@@ -1838,7 +1905,7 @@ const readCommand = define({
|
|
|
1838
1905
|
});
|
|
1839
1906
|
//#endregion
|
|
1840
1907
|
//#region src/commands/focus.ts
|
|
1841
|
-
const sleep$
|
|
1908
|
+
const sleep$2 = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
1842
1909
|
/**
|
|
1843
1910
|
* Make a tab believe it is the visible one.
|
|
1844
1911
|
*
|
|
@@ -1907,7 +1974,7 @@ const focusCommand = define({
|
|
|
1907
1974
|
returnByValue: true
|
|
1908
1975
|
})).result?.value ?? last;
|
|
1909
1976
|
if (last.focused && last.visibility === "visible") break;
|
|
1910
|
-
await sleep$
|
|
1977
|
+
await sleep$2(50);
|
|
1911
1978
|
}
|
|
1912
1979
|
return last;
|
|
1913
1980
|
});
|
|
@@ -2273,7 +2340,7 @@ const uploadCommand = define({
|
|
|
2273
2340
|
});
|
|
2274
2341
|
//#endregion
|
|
2275
2342
|
//#region src/commands/drop.ts
|
|
2276
|
-
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
2343
|
+
const sleep$1 = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
2277
2344
|
/** Resolve each path to absolute and verify it's an existing file. */
|
|
2278
2345
|
function absFiles(paths) {
|
|
2279
2346
|
return paths.map((p) => {
|
|
@@ -2375,7 +2442,7 @@ const dropCommand = define({
|
|
|
2375
2442
|
while (Date.now() < focusDeadline) {
|
|
2376
2443
|
const f = await evalIn(`({f:document.hasFocus(),v:document.visibilityState})`);
|
|
2377
2444
|
if (f && f.f && f.v === "visible") break;
|
|
2378
|
-
await sleep(50);
|
|
2445
|
+
await sleep$1(50);
|
|
2379
2446
|
}
|
|
2380
2447
|
let prev = null;
|
|
2381
2448
|
let point = null;
|
|
@@ -2401,7 +2468,7 @@ const dropCommand = define({
|
|
|
2401
2468
|
x: a.x,
|
|
2402
2469
|
y: a.y
|
|
2403
2470
|
};
|
|
2404
|
-
await sleep(60);
|
|
2471
|
+
await sleep$1(60);
|
|
2405
2472
|
}
|
|
2406
2473
|
if (!point) {
|
|
2407
2474
|
consola$1.error(`<${ref}> not actionable for a drop: ${lastReason}`);
|
|
@@ -2425,7 +2492,7 @@ const dropCommand = define({
|
|
|
2425
2492
|
y,
|
|
2426
2493
|
data
|
|
2427
2494
|
});
|
|
2428
|
-
await sleep(20);
|
|
2495
|
+
await sleep$1(20);
|
|
2429
2496
|
await s.send("Input.dispatchDragEvent", {
|
|
2430
2497
|
type: "drop",
|
|
2431
2498
|
x,
|
|
@@ -2864,6 +2931,140 @@ function migrateProfile() {
|
|
|
2864
2931
|
};
|
|
2865
2932
|
}
|
|
2866
2933
|
//#endregion
|
|
2934
|
+
//#region src/core/launch-windows.ts
|
|
2935
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
2936
|
+
/** chrome.exe: an explicit override, then the per-machine and per-user installs. */
|
|
2937
|
+
function windowsChromeExe() {
|
|
2938
|
+
const explicit = process.env.BROWSER_AUTOMATION_CHROME || process.env.CHROME;
|
|
2939
|
+
if (explicit) return existsSync(explicit) ? explicit : null;
|
|
2940
|
+
return [
|
|
2941
|
+
process.env.PROGRAMFILES && join(process.env.PROGRAMFILES, "Google", "Chrome", "Application", "chrome.exe"),
|
|
2942
|
+
process.env["PROGRAMFILES(X86)"] && join(process.env["PROGRAMFILES(X86)"], "Google", "Chrome", "Application", "chrome.exe"),
|
|
2943
|
+
process.env.LOCALAPPDATA && join(process.env.LOCALAPPDATA, "Google", "Chrome", "Application", "chrome.exe")
|
|
2944
|
+
].filter(Boolean).find((p) => existsSync(p)) ?? null;
|
|
2945
|
+
}
|
|
2946
|
+
/** Is anything answering CDP on this port? (The script's `is_up`.) */
|
|
2947
|
+
async function cdpUp(port) {
|
|
2948
|
+
try {
|
|
2949
|
+
return (await fetch(`http://127.0.0.1:${port}/json/version`, { signal: AbortSignal.timeout(2e3) })).ok;
|
|
2950
|
+
} catch {
|
|
2951
|
+
return false;
|
|
2952
|
+
}
|
|
2953
|
+
}
|
|
2954
|
+
/**
|
|
2955
|
+
* pid of OUR automation Chrome's browser process on this port, or 0.
|
|
2956
|
+
*
|
|
2957
|
+
* PowerShell for the command lines: Node cannot read another process's command
|
|
2958
|
+
* line on Windows, and `wmic` is gone from Windows 11 25H2.
|
|
2959
|
+
*/
|
|
2960
|
+
function ourChromePid(port) {
|
|
2961
|
+
const script = [
|
|
2962
|
+
"$ProgressPreference = 'SilentlyContinue'",
|
|
2963
|
+
`$re = '--remote-debugging-port=${port}(\\s|"|$)'`,
|
|
2964
|
+
"$sid = [System.Diagnostics.Process]::GetCurrentProcess().SessionId",
|
|
2965
|
+
"$b = Get-CimInstance Win32_Process -Filter \"Name = 'chrome.exe'\" |",
|
|
2966
|
+
" Where-Object { $_.SessionId -eq $sid -and $_.CommandLine -and $_.CommandLine -match $re -and $_.CommandLine -notmatch '--type=' } |",
|
|
2967
|
+
" Select-Object -First 1 -ExpandProperty ProcessId",
|
|
2968
|
+
"Write-Output ('BA-PID ' + [int]$b)"
|
|
2969
|
+
].join("\n");
|
|
2970
|
+
try {
|
|
2971
|
+
const out = execFileSync("powershell.exe", [
|
|
2972
|
+
"-NoProfile",
|
|
2973
|
+
"-NonInteractive",
|
|
2974
|
+
"-ExecutionPolicy",
|
|
2975
|
+
"Bypass",
|
|
2976
|
+
"-EncodedCommand",
|
|
2977
|
+
Buffer.from(script, "utf16le").toString("base64")
|
|
2978
|
+
], {
|
|
2979
|
+
encoding: "utf8",
|
|
2980
|
+
stdio: [
|
|
2981
|
+
"ignore",
|
|
2982
|
+
"pipe",
|
|
2983
|
+
"ignore"
|
|
2984
|
+
],
|
|
2985
|
+
timeout: 2e4,
|
|
2986
|
+
windowsHide: true
|
|
2987
|
+
});
|
|
2988
|
+
return Number(/^BA-PID (\d+)\s*$/m.exec(out)?.[1] ?? 0);
|
|
2989
|
+
} catch {
|
|
2990
|
+
return 0;
|
|
2991
|
+
}
|
|
2992
|
+
}
|
|
2993
|
+
function alive(pid) {
|
|
2994
|
+
try {
|
|
2995
|
+
process.kill(pid, 0);
|
|
2996
|
+
return true;
|
|
2997
|
+
} catch {
|
|
2998
|
+
return false;
|
|
2999
|
+
}
|
|
3000
|
+
}
|
|
3001
|
+
/**
|
|
3002
|
+
* Quit OUR Chrome on this port and wait until it has really exited.
|
|
3003
|
+
*
|
|
3004
|
+
* CDP Browser.close first — Chrome's own quit, which flushes the profile that
|
|
3005
|
+
* holds the logins. On Windows the process does exit after it (unlike macOS,
|
|
3006
|
+
* where it lingers windowless). taskkill /F only if it will not go.
|
|
3007
|
+
*/
|
|
3008
|
+
async function stopOurChrome(port, say) {
|
|
3009
|
+
const pid = ourChromePid(port);
|
|
3010
|
+
if (!pid) return await cdpUp(port) ? "not-ours" : "none";
|
|
3011
|
+
await closeBrowser().catch(() => {});
|
|
3012
|
+
for (let i = 0; i < 40 && alive(pid); i++) await sleep(250);
|
|
3013
|
+
if (alive(pid)) {
|
|
3014
|
+
say(`Chrome (pid ${pid}) did not exit within 10s of being asked to quit; ending it.`);
|
|
3015
|
+
try {
|
|
3016
|
+
execFileSync("taskkill", [
|
|
3017
|
+
"/PID",
|
|
3018
|
+
String(pid),
|
|
3019
|
+
"/T",
|
|
3020
|
+
"/F"
|
|
3021
|
+
], { stdio: "ignore" });
|
|
3022
|
+
} catch {}
|
|
3023
|
+
for (let i = 0; i < 20 && alive(pid); i++) await sleep(250);
|
|
3024
|
+
}
|
|
3025
|
+
if (alive(pid)) throw new Error(`Chrome on :${port} (pid ${pid}) would not exit. Not relaunching on top of it.`);
|
|
3026
|
+
for (let i = 0; i < 20 && await cdpUp(port); i++) await sleep(250);
|
|
3027
|
+
return "stopped";
|
|
3028
|
+
}
|
|
3029
|
+
/** Start the automation Chrome unless ours is already serving the port. */
|
|
3030
|
+
async function startChrome(port, profile, log) {
|
|
3031
|
+
if (await cdpUp(port)) return ourChromePid(port) ? { outcome: "already-running" } : { outcome: "not-ours" };
|
|
3032
|
+
const exe = windowsChromeExe();
|
|
3033
|
+
if (!exe) return { outcome: "no-chrome" };
|
|
3034
|
+
mkdirSync(profile, { recursive: true });
|
|
3035
|
+
mkdirSync(dirname(log), { recursive: true });
|
|
3036
|
+
try {
|
|
3037
|
+
if (statSync(log).size > 0) renameSync(log, `${log}.prev`);
|
|
3038
|
+
} catch {}
|
|
3039
|
+
const fd = openSync(log, "a");
|
|
3040
|
+
spawn(exe, [
|
|
3041
|
+
`--remote-debugging-port=${port}`,
|
|
3042
|
+
`--user-data-dir=${profile}`,
|
|
3043
|
+
"--no-first-run",
|
|
3044
|
+
"--no-default-browser-check",
|
|
3045
|
+
"about:blank"
|
|
3046
|
+
], {
|
|
3047
|
+
detached: true,
|
|
3048
|
+
stdio: [
|
|
3049
|
+
"ignore",
|
|
3050
|
+
fd,
|
|
3051
|
+
fd
|
|
3052
|
+
]
|
|
3053
|
+
}).unref();
|
|
3054
|
+
const started = Date.now();
|
|
3055
|
+
for (let i = 0; i < 240; i++) {
|
|
3056
|
+
if (await cdpUp(port)) return {
|
|
3057
|
+
outcome: "launched",
|
|
3058
|
+
seconds: Math.round((Date.now() - started) / 1e3)
|
|
3059
|
+
};
|
|
3060
|
+
await sleep(250);
|
|
3061
|
+
}
|
|
3062
|
+
return {
|
|
3063
|
+
outcome: "timeout",
|
|
3064
|
+
log
|
|
3065
|
+
};
|
|
3066
|
+
}
|
|
3067
|
+
//#endregion
|
|
2867
3068
|
//#region src/commands/launch.ts
|
|
2868
3069
|
const launchCommand = define({
|
|
2869
3070
|
name: "launch",
|
|
@@ -2879,35 +3080,40 @@ const launchCommand = define({
|
|
|
2879
3080
|
}
|
|
2880
3081
|
},
|
|
2881
3082
|
async run(ctx) {
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
|
|
2890
|
-
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
3083
|
+
if (process.platform === "win32") {
|
|
3084
|
+
if (!await launchWindows(ctx.values.status === true, ctx.values.restart === true)) process.exit(1);
|
|
3085
|
+
if (ctx.values.status) return;
|
|
3086
|
+
} else {
|
|
3087
|
+
const script = launchScriptPath();
|
|
3088
|
+
if (!existsSync(script)) {
|
|
3089
|
+
consola$1.error(`launch-chrome.sh not found at ${script}. Reinstall the package.`);
|
|
3090
|
+
process.exit(1);
|
|
3091
|
+
}
|
|
3092
|
+
const port = ["--port", String(cdpPort())];
|
|
3093
|
+
const bash = (extra) => spawnSync("bash", [
|
|
3094
|
+
script,
|
|
3095
|
+
...port,
|
|
3096
|
+
...extra
|
|
3097
|
+
], { stdio: "inherit" });
|
|
3098
|
+
if (ctx.values.status) process.exit(bash(["--status"]).status ?? 1);
|
|
3099
|
+
if (ctx.values.restart) {
|
|
3100
|
+
const tabs = await listPageTargets().then((t) => t.length).catch(() => null);
|
|
3101
|
+
if (tabs !== null) consola$1.info(`Restarting Chrome on :${cdpPort()} — closing ${tabs} open tab(s).`);
|
|
3102
|
+
await closeBrowser().catch(() => {});
|
|
3103
|
+
const stop = bash([
|
|
3104
|
+
"--stop",
|
|
3105
|
+
"--profile",
|
|
3106
|
+
resolveProfile().dir
|
|
3107
|
+
]);
|
|
3108
|
+
if (stop.status !== 0) process.exit(stop.status ?? 1);
|
|
3109
|
+
}
|
|
3110
|
+
const m = migrateProfile();
|
|
3111
|
+
if (m.outcome === "moved") consola$1.success(`Profile ${m.detail} — logins travel with it.`);
|
|
3112
|
+
else if (m.outcome === "refused" || m.outcome === "failed") consola$1.warn(`Profile not moved: ${m.detail}`);
|
|
3113
|
+
else if (m.outcome === "in-use") consola$1.info(`Profile not moved yet: ${m.detail}`);
|
|
3114
|
+
const r = bash(["--profile", m.dir]);
|
|
3115
|
+
if (r.status !== 0) process.exit(r.status ?? 1);
|
|
2904
3116
|
}
|
|
2905
|
-
const m = migrateProfile();
|
|
2906
|
-
if (m.outcome === "moved") consola$1.success(`Profile ${m.detail} — logins travel with it.`);
|
|
2907
|
-
else if (m.outcome === "refused" || m.outcome === "failed") consola$1.warn(`Profile not moved: ${m.detail}`);
|
|
2908
|
-
else if (m.outcome === "in-use") consola$1.info(`Profile not moved yet: ${m.detail}`);
|
|
2909
|
-
const r = bash(["--profile", m.dir]);
|
|
2910
|
-
if (r.status !== 0) process.exit(r.status ?? 1);
|
|
2911
3117
|
const health = await probeRenderer().catch((e) => ({
|
|
2912
3118
|
ok: false,
|
|
2913
3119
|
ms: 0,
|
|
@@ -2928,6 +3134,56 @@ const launchCommand = define({
|
|
|
2928
3134
|
process.exit(1);
|
|
2929
3135
|
}
|
|
2930
3136
|
});
|
|
3137
|
+
/**
|
|
3138
|
+
* Windows: the launch script's contract in TypeScript. Prints the same lines
|
|
3139
|
+
* the script prints, so an agent reading either learns the same thing.
|
|
3140
|
+
* Returns false on a failure that must stop `launch` before the health check.
|
|
3141
|
+
*/
|
|
3142
|
+
async function launchWindows(statusOnly, restart) {
|
|
3143
|
+
const port = cdpPort();
|
|
3144
|
+
if (statusOnly) {
|
|
3145
|
+
const up = await cdpUp(port);
|
|
3146
|
+
consola$1.log(up ? `Chrome CDP on :${port} is up` : `Chrome CDP on :${port} is NOT running`);
|
|
3147
|
+
return up;
|
|
3148
|
+
}
|
|
3149
|
+
if (restart) {
|
|
3150
|
+
const tabs = await listPageTargets().then((t) => t.length).catch(() => null);
|
|
3151
|
+
if (tabs !== null) consola$1.info(`Restarting Chrome on :${port} — closing ${tabs} open tab(s).`);
|
|
3152
|
+
try {
|
|
3153
|
+
if (await stopOurChrome(port, (s) => consola$1.warn(s)) === "not-ours") {
|
|
3154
|
+
consola$1.error(`Refusing to restart a Chrome this account did not start (something else serves :${port}).`);
|
|
3155
|
+
return false;
|
|
3156
|
+
}
|
|
3157
|
+
} catch (e) {
|
|
3158
|
+
consola$1.error(String(e?.message ?? e));
|
|
3159
|
+
return false;
|
|
3160
|
+
}
|
|
3161
|
+
}
|
|
3162
|
+
const m = migrateProfile();
|
|
3163
|
+
if (m.outcome === "moved") consola$1.success(`Profile ${m.detail} — logins travel with it.`);
|
|
3164
|
+
else if (m.outcome === "refused" || m.outcome === "failed") consola$1.warn(`Profile not moved: ${m.detail}`);
|
|
3165
|
+
const r = await startChrome(port, m.dir, chromeLogPath());
|
|
3166
|
+
switch (r.outcome) {
|
|
3167
|
+
case "already-running":
|
|
3168
|
+
consola$1.log(`Already running on :${port} — the port is held.`);
|
|
3169
|
+
consola$1.log("Drive it with: browser-automation goto -s <session> <url>");
|
|
3170
|
+
consola$1.log("(Port held is not health. Check it works: browser-automation doctor)");
|
|
3171
|
+
return true;
|
|
3172
|
+
case "launched":
|
|
3173
|
+
consola$1.log(`Chrome launched on :${port} in ${r.seconds}s (profile: ${m.dir}, log: ${chromeLogPath()})`);
|
|
3174
|
+
consola$1.log("Drive it with: browser-automation goto -s <session> <url>");
|
|
3175
|
+
return true;
|
|
3176
|
+
case "not-ours":
|
|
3177
|
+
consola$1.error(`Something on this computer is already serving CDP on port ${port}, and it is not a Chrome this account started. Driving it would act in ANOTHER user's browser — quit Chrome in that account, or set BROWSER_AUTOMATION_PORT to a free port for this one.`);
|
|
3178
|
+
return false;
|
|
3179
|
+
case "no-chrome":
|
|
3180
|
+
consola$1.error("Chrome not found (looked in Program Files, Program Files (x86) and %LOCALAPPDATA%). Install it — winget install --id Google.Chrome -e — or set BROWSER_AUTOMATION_CHROME to chrome.exe.");
|
|
3181
|
+
return false;
|
|
3182
|
+
case "timeout":
|
|
3183
|
+
consola$1.error(`Chrome did not serve CDP on :${port} within 60s. Chrome's log: ${r.log}`);
|
|
3184
|
+
return false;
|
|
3185
|
+
}
|
|
3186
|
+
}
|
|
2931
3187
|
//#endregion
|
|
2932
3188
|
//#region src/commands/doctor.ts
|
|
2933
3189
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@generativereality/browser-automation",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.18",
|
|
4
4
|
"description": "Daemonless, per-tab-isolated browser automation CLI for Claude Code. Drives one shared headed Chrome (persistent profile + extensions) over per-target CDP — many parallel sessions, no focus stealing, nothing long-lived to crash.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
package/skills/browser/SKILL.md
CHANGED
|
@@ -62,8 +62,10 @@ file under `~/.browser-automation/sessions/`.
|
|
|
62
62
|
case of someone watching it. The person's everyday Chrome counts as "another
|
|
63
63
|
app": it is checked by process, not by name. ⛔ Do not reach for `focus --raise`
|
|
64
64
|
to "make it show" — that is the screen-taking version; keep it for the moments
|
|
65
|
-
a person must look (a sign-in, a CAPTCHA). macOS
|
|
66
|
-
reports that it cannot tell and does nothing.
|
|
65
|
+
a person must look (a sign-in, a CAPTCHA). **macOS and Windows**; on Linux
|
|
66
|
+
`--show` reports that it cannot tell and does nothing. On Windows each
|
|
67
|
+
`--show` costs about 0.9 s (a PowerShell probe of the foreground window), so
|
|
68
|
+
turn it on where someone may be watching, not by reflex for batch work.
|
|
67
69
|
- **Self-healing.** If the tab was closed (or Chrome restarted and reissued
|
|
68
70
|
targetIds), the next `goto` just opens a fresh background tab for that session.
|
|
69
71
|
- **Refs live in the DOM.** `snapshot` stamps `data-ba-ref="e7"` onto each
|
|
@@ -284,9 +286,15 @@ There's no `state-save`/`state-load` to manage — the profile *is* the auth sto
|
|
|
284
286
|
- **Page still loading.** `goto` waits for the load event, but SPAs render after.
|
|
285
287
|
If a `read`/`snapshot` looks empty, re-run after a moment, or snapshot again
|
|
286
288
|
once a known element should be present.
|
|
287
|
-
- **`launch`
|
|
288
|
-
|
|
289
|
-
|
|
289
|
+
- **`launch` works on macOS, Linux and Windows.** On Windows it needs only Node
|
|
290
|
+
(no Git Bash): it finds `chrome.exe` in Program Files or `%LOCALAPPDATA%`
|
|
291
|
+
(override with `BROWSER_AUTOMATION_CHROME`), uses the same profile
|
|
292
|
+
(`%USERPROFILE%\.browser-automation\chrome-profile`) and the same rules —
|
|
293
|
+
idempotent, refuses a port held by a Chrome this account did not start, and
|
|
294
|
+
`--restart` quits Chrome cleanly so the logins survive. ⛔ So do not start
|
|
295
|
+
Chrome by hand on Windows any more; that was the workaround for CLIs older than
|
|
296
|
+
0.4.18, which refused with `unsupported OS MINGW64_NT`. A cold start brings
|
|
297
|
+
Chrome to the front once, on every platform.
|
|
290
298
|
|
|
291
299
|
## Network insights — find the API behind a page
|
|
292
300
|
|