@generativereality/browser-automation 0.4.5 → 0.4.6
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 +18 -0
- package/dist/index.js +246 -34
- package/package.json +1 -1
- package/skills/browser/SKILL.md +23 -1
|
@@ -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.6",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "generativereality"
|
|
7
7
|
},
|
package/README.md
CHANGED
|
@@ -106,6 +106,7 @@ browser-automation bind -s bank -m nordnet # …or adopt it into a session
|
|
|
106
106
|
| `download (-s\|-m\|-t) (--click <ref>\|--url <href>)` | Capture a file/CSV download, wait for completion, print the path |
|
|
107
107
|
| `setfiles (-s\|-m\|-t) <ref> <path…>` | Set files on a known `<input type=file>` by ref (fires input/change) |
|
|
108
108
|
| `upload (-s\|-m\|-t) --click <ref> <path…>` | Upload via a button that opens a file chooser (transient/custom inputs) |
|
|
109
|
+
| `drop (-s\|-m\|-t) [--js] <ref> <path…>` | Drop file(s) onto a drag-and-drop zone by ref (trusted CDP drag by default; `--js` for a synthetic drop) |
|
|
109
110
|
| `network (-s\|-m\|-t) [--reload\|--click\|--nav] [--filter --headers --body]` | Capture network requests (find the API, headers, response bodies) |
|
|
110
111
|
| `screenshot (-s\|-m\|-t) [--full] [-o path]` | Save a PNG screenshot (viewport or full page) |
|
|
111
112
|
| `close (-s\|-m\|-t) [--tab]` | Forget the session (tab stays open); `--tab` also closes the browser tab |
|
|
@@ -139,6 +140,23 @@ same one-action-per-snapshot rule as Playwright refs.
|
|
|
139
140
|
re-snapshot/screenshot and don't blindly retry:** the file may stage as a row in
|
|
140
141
|
an attachment *list* (not a single chip), and each successful run adds another
|
|
141
142
|
attachment, so retrying can silently create duplicates.
|
|
143
|
+
- **Drag-and-drop upload** is a third path, for zones with no `<input type=file>`
|
|
144
|
+
at all — a `drop` listener reading `e.dataTransfer.files` (vocalremover-style
|
|
145
|
+
audio tools, many image/video drop zones). `drop <ref> <path…>` fires a
|
|
146
|
+
genuinely-**trusted** CDP drag (`Input.dispatchDragEvent`) carrying the real
|
|
147
|
+
files from disk: it force-fronts the tab (bringToFront + focus emulation +
|
|
148
|
+
active lifecycle, so the renderer reports focused+visible even when the Chrome
|
|
149
|
+
window is occluded), waits for the zone to be actionable, then
|
|
150
|
+
dragEnter→dragOver→drop — same discipline as `click --trusted`. The force-front
|
|
151
|
+
matters because drop-zone uploaders often act only inside a **user activation**,
|
|
152
|
+
which CDP input can't grant on a tab the renderer considers `hidden`
|
|
153
|
+
(vocalremover.org is the canonical case). `--js` instead dispatches a synthetic
|
|
154
|
+
(isTrusted=false) `DataTransfer` drop without force-fronting/stealing focus, for
|
|
155
|
+
zones that accept synthetic events. The `<ref>` is any snapshot element over the
|
|
156
|
+
drop region — the drop bubbles to the zone/document handler, so a heading or
|
|
157
|
+
button inside the zone works even when the zone div itself isn't
|
|
158
|
+
snapshot-interactive. After dropping, confirm with `read`/`screenshot` and pull
|
|
159
|
+
the result (often a `download --url` endpoint).
|
|
142
160
|
- `launch` resolves Chrome on macOS/Linux; elsewhere start Chrome manually with
|
|
143
161
|
`--remote-debugging-port=9223 --user-data-dir="<profile>"`.
|
|
144
162
|
|
package/dist/index.js
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
import { cli, define } from "gunshi";
|
|
3
3
|
import { consola } from "consola";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
|
-
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
import { basename, dirname, extname, join, resolve } from "node:path";
|
|
6
6
|
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
7
7
|
import { spawnSync } from "node:child_process";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
9
9
|
//#region package.json
|
|
10
|
-
var version = "0.4.
|
|
10
|
+
var version = "0.4.6";
|
|
11
11
|
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.";
|
|
12
12
|
//#endregion
|
|
13
13
|
//#region src/core/session.ts
|
|
@@ -150,6 +150,27 @@ async function withPage(targetId, fn) {
|
|
|
150
150
|
s.close();
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
|
+
/** Make a page target behave as the foreground tab for trusted CDP input,
|
|
154
|
+
* WITHOUT stealing OS focus from the user's other apps.
|
|
155
|
+
*
|
|
156
|
+
* CDP `Input.*` events (and the user-activation they grant) are dropped by the
|
|
157
|
+
* renderer when the tab's `visibilityState` is `hidden` — which it is whenever
|
|
158
|
+
* the automation Chrome window isn't the frontmost OS window (the normal case:
|
|
159
|
+
* the user is in their terminal). `Page.bringToFront` alone does NOT fix this;
|
|
160
|
+
* the tab still reports `hidden`. `Emulation.setFocusEmulationEnabled` forces
|
|
161
|
+
* the renderer to treat itself as focused+visible regardless of the real window
|
|
162
|
+
* state (this is how Playwright drives backgrounded/headless pages), and
|
|
163
|
+
* `Page.setWebLifecycleState: active` un-throttles a frozen tab. Together they
|
|
164
|
+
* flip `document.hasFocus()`+`visibilityState:visible` on, so a trusted click /
|
|
165
|
+
* drag actually lands and grants user activation — which activation-gated flows
|
|
166
|
+
* (e.g. drop-zone uploaders that only start work inside a user gesture) require.
|
|
167
|
+
*
|
|
168
|
+
* All three are best-effort: older Chrome builds may not expose every command. */
|
|
169
|
+
async function forceForeground(s) {
|
|
170
|
+
await s.send("Page.bringToFront").catch(() => {});
|
|
171
|
+
await s.send("Emulation.setFocusEmulationEnabled", { enabled: true }).catch(() => {});
|
|
172
|
+
await s.send("Page.setWebLifecycleState", { state: "active" }).catch(() => {});
|
|
173
|
+
}
|
|
153
174
|
/** Run `fn` against the browser-level endpoint (Target.* commands), then close. */
|
|
154
175
|
async function withBrowser(fn) {
|
|
155
176
|
const s = await connect(await browserWsUrl());
|
|
@@ -579,7 +600,19 @@ function focusAndSelectExpr(ref) {
|
|
|
579
600
|
if(!el)return {err:'ref not found: '+${r}+' (re-run snapshot)'};
|
|
580
601
|
el.scrollIntoView({block:'center',inline:'center'});
|
|
581
602
|
el.focus();
|
|
582
|
-
|
|
603
|
+
// Select-all in JS so the subsequent Input.insertText REPLACES the field
|
|
604
|
+
// rather than appending. Doing this here (not via a synthetic Cmd+A key
|
|
605
|
+
// chord) keeps the trusted-fill path from ever dispatching a Meta-modified
|
|
606
|
+
// key event — an unhandled one of those leaks to the macOS main menu and
|
|
607
|
+
// pops system UI (e.g. "About This Mac"). Inputs/textareas: .select();
|
|
608
|
+
// contenteditable: the Selection API.
|
|
609
|
+
try{
|
|
610
|
+
if(typeof el.select==='function') el.select();
|
|
611
|
+
else if(el.isContentEditable){
|
|
612
|
+
const range=document.createRange();range.selectNodeContents(el);
|
|
613
|
+
const sel=window.getSelection();sel.removeAllRanges();sel.addRange(range);
|
|
614
|
+
}
|
|
615
|
+
}catch(e){}
|
|
583
616
|
return {ok:true,tag:el.tagName.toLowerCase(),len:el.value!==undefined?String(el.value).length:0};
|
|
584
617
|
})()`;
|
|
585
618
|
}
|
|
@@ -610,6 +643,36 @@ function readValueAndTrackerExpr(ref) {
|
|
|
610
643
|
function findRefExpr(ref) {
|
|
611
644
|
return WALK + `(() => __baFind(${JSON.stringify(ref)}))()`;
|
|
612
645
|
}
|
|
646
|
+
/** JS-dispatched drag-and-drop of injected files onto an element by ref. Builds
|
|
647
|
+
* real File objects from base64 bytes, assembles a DataTransfer, and fires the
|
|
648
|
+
* full dragenter→dragover→drop sequence (each carrying dataTransfer.files) so
|
|
649
|
+
* drop-zone handlers that read `e.dataTransfer.files` fire. The events are
|
|
650
|
+
* synthetic (isTrusted=false) — drop zones that gate on isTrusted need the
|
|
651
|
+
* trusted CDP path (`drop` without --js) instead. `files` is [{name,type,b64}]. */
|
|
652
|
+
function dropExpr(ref, files) {
|
|
653
|
+
const r = JSON.stringify(ref);
|
|
654
|
+
return WALK + `(() => {
|
|
655
|
+
const el=__baFind(${r});
|
|
656
|
+
if(!el)return {err:'ref not found: '+${r}+' (re-run snapshot)'};
|
|
657
|
+
el.scrollIntoView({block:'center',inline:'center'});
|
|
658
|
+
const dt=new DataTransfer();
|
|
659
|
+
for(const spec of ${JSON.stringify(files)}){
|
|
660
|
+
const bin=atob(spec.b64); const u8=new Uint8Array(bin.length);
|
|
661
|
+
for(let i=0;i<bin.length;i++) u8[i]=bin.charCodeAt(i);
|
|
662
|
+
dt.items.add(new File([u8],spec.name,{type:spec.type}));
|
|
663
|
+
}
|
|
664
|
+
const b=el.getBoundingClientRect();
|
|
665
|
+
const cx=b.left+b.width/2, cy=b.top+b.height/2;
|
|
666
|
+
const fire=(type)=>{
|
|
667
|
+
const ev=new DragEvent(type,{bubbles:true,cancelable:true,clientX:cx,clientY:cy});
|
|
668
|
+
try{ Object.defineProperty(ev,'dataTransfer',{value:dt}); }catch(e){}
|
|
669
|
+
el.dispatchEvent(ev);
|
|
670
|
+
return ev.defaultPrevented;
|
|
671
|
+
};
|
|
672
|
+
fire('dragenter'); fire('dragover'); fire('drop');
|
|
673
|
+
return {ok:true,tag:el.tagName.toLowerCase(),count:dt.files.length};
|
|
674
|
+
})()`;
|
|
675
|
+
}
|
|
613
676
|
function readExpr(selector) {
|
|
614
677
|
return WALK + `(() => { const r=${selector ? `__baQuery(${JSON.stringify(selector)})` : `document.body`}; return r ? __baText(r) : ${selector ? `'(no element matches ' + ${JSON.stringify(selector)} + ')'` : `''`}; })()`;
|
|
615
678
|
}
|
|
@@ -643,7 +706,7 @@ const snapshotCommand = define({
|
|
|
643
706
|
});
|
|
644
707
|
//#endregion
|
|
645
708
|
//#region src/commands/click.ts
|
|
646
|
-
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
709
|
+
const sleep$1 = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
647
710
|
const clickCommand = define({
|
|
648
711
|
name: "click",
|
|
649
712
|
description: "Click an element by ref (from the latest snapshot)",
|
|
@@ -679,12 +742,12 @@ const clickCommand = define({
|
|
|
679
742
|
}
|
|
680
743
|
return res.result?.value;
|
|
681
744
|
};
|
|
682
|
-
await s
|
|
745
|
+
await forceForeground(s);
|
|
683
746
|
const focusDeadline = Date.now() + 2e3;
|
|
684
747
|
while (Date.now() < focusDeadline) {
|
|
685
748
|
const f = await evalIn(`({f:document.hasFocus(),v:document.visibilityState})`);
|
|
686
749
|
if (f && f.f && f.v === "visible") break;
|
|
687
|
-
await sleep(50);
|
|
750
|
+
await sleep$1(50);
|
|
688
751
|
}
|
|
689
752
|
let prev = null;
|
|
690
753
|
let point = null;
|
|
@@ -710,7 +773,7 @@ const clickCommand = define({
|
|
|
710
773
|
x: a.x,
|
|
711
774
|
y: a.y
|
|
712
775
|
};
|
|
713
|
-
await sleep(60);
|
|
776
|
+
await sleep$1(60);
|
|
714
777
|
}
|
|
715
778
|
if (!point) {
|
|
716
779
|
consola.error(`<${ref}> not actionable for a trusted click: ${lastReason}`);
|
|
@@ -725,7 +788,7 @@ const clickCommand = define({
|
|
|
725
788
|
buttons: 0,
|
|
726
789
|
pointerType: "mouse"
|
|
727
790
|
});
|
|
728
|
-
await sleep(20);
|
|
791
|
+
await sleep$1(20);
|
|
729
792
|
await s.send("Input.dispatchMouseEvent", {
|
|
730
793
|
type: "mousePressed",
|
|
731
794
|
x,
|
|
@@ -760,7 +823,6 @@ const clickCommand = define({
|
|
|
760
823
|
});
|
|
761
824
|
//#endregion
|
|
762
825
|
//#region src/commands/fill.ts
|
|
763
|
-
const SELECT_ALL_MOD = process.platform === "darwin" ? 4 : 2;
|
|
764
826
|
const fillCommand = define({
|
|
765
827
|
name: "fill",
|
|
766
828
|
description: "Type a value into an input/textarea/contenteditable by ref",
|
|
@@ -808,38 +870,19 @@ const fillCommand = define({
|
|
|
808
870
|
process.exit(1);
|
|
809
871
|
}
|
|
810
872
|
await withPage(targetId, async (s) => {
|
|
811
|
-
await s.send("Input.dispatchKeyEvent", {
|
|
812
|
-
type: "rawKeyDown",
|
|
813
|
-
key: "a",
|
|
814
|
-
code: "KeyA",
|
|
815
|
-
windowsVirtualKeyCode: 65,
|
|
816
|
-
nativeVirtualKeyCode: 65,
|
|
817
|
-
modifiers: SELECT_ALL_MOD,
|
|
818
|
-
commands: ["selectAll"]
|
|
819
|
-
});
|
|
820
|
-
await s.send("Input.dispatchKeyEvent", {
|
|
821
|
-
type: "keyUp",
|
|
822
|
-
key: "a",
|
|
823
|
-
code: "KeyA",
|
|
824
|
-
windowsVirtualKeyCode: 65,
|
|
825
|
-
nativeVirtualKeyCode: 65,
|
|
826
|
-
modifiers: SELECT_ALL_MOD
|
|
827
|
-
});
|
|
828
873
|
await s.send("Input.insertText", { text: value });
|
|
829
874
|
if (submit) {
|
|
830
875
|
await s.send("Input.dispatchKeyEvent", {
|
|
831
876
|
type: "rawKeyDown",
|
|
832
877
|
key: "Enter",
|
|
833
878
|
code: "Enter",
|
|
834
|
-
windowsVirtualKeyCode: 13
|
|
835
|
-
nativeVirtualKeyCode: 13
|
|
879
|
+
windowsVirtualKeyCode: 13
|
|
836
880
|
});
|
|
837
881
|
await s.send("Input.dispatchKeyEvent", {
|
|
838
882
|
type: "keyUp",
|
|
839
883
|
key: "Enter",
|
|
840
884
|
code: "Enter",
|
|
841
|
-
windowsVirtualKeyCode: 13
|
|
842
|
-
nativeVirtualKeyCode: 13
|
|
885
|
+
windowsVirtualKeyCode: 13
|
|
843
886
|
});
|
|
844
887
|
}
|
|
845
888
|
});
|
|
@@ -1129,7 +1172,7 @@ async function uploadViaChooser(targetId, clickRef, files, timeout = 15e3) {
|
|
|
1129
1172
|
//#endregion
|
|
1130
1173
|
//#region src/commands/setfiles.ts
|
|
1131
1174
|
/** Resolve each path to absolute and verify it's an existing file. */
|
|
1132
|
-
function absFiles$
|
|
1175
|
+
function absFiles$2(paths) {
|
|
1133
1176
|
return paths.map((p) => {
|
|
1134
1177
|
const abs = resolve(p);
|
|
1135
1178
|
if (!existsSync(abs) || !statSync(abs).isFile()) {
|
|
@@ -1164,7 +1207,7 @@ const setfilesCommand = define({
|
|
|
1164
1207
|
consola.error("At least one file path is required");
|
|
1165
1208
|
process.exit(1);
|
|
1166
1209
|
}
|
|
1167
|
-
const files = absFiles$
|
|
1210
|
+
const files = absFiles$2(paths);
|
|
1168
1211
|
const r = await setFilesByRef(await resolveExistingTargetId(targetOpts(ctx.values)), ref, files);
|
|
1169
1212
|
consola.success(`set ${r.files.length} file(s) on ${ref} <${r.tag} type=${r.type}>:`);
|
|
1170
1213
|
for (const f of r.files) process.stdout.write(f + "\n");
|
|
@@ -1173,7 +1216,7 @@ const setfilesCommand = define({
|
|
|
1173
1216
|
//#endregion
|
|
1174
1217
|
//#region src/commands/upload.ts
|
|
1175
1218
|
/** Resolve each path to absolute and verify it's an existing file. */
|
|
1176
|
-
function absFiles(paths) {
|
|
1219
|
+
function absFiles$1(paths) {
|
|
1177
1220
|
return paths.map((p) => {
|
|
1178
1221
|
const abs = resolve(p);
|
|
1179
1222
|
if (!existsSync(abs) || !statSync(abs).isFile()) {
|
|
@@ -1216,7 +1259,7 @@ const uploadCommand = define({
|
|
|
1216
1259
|
consola.error("At least one file path is required");
|
|
1217
1260
|
process.exit(1);
|
|
1218
1261
|
}
|
|
1219
|
-
const files = absFiles(paths);
|
|
1262
|
+
const files = absFiles$1(paths);
|
|
1220
1263
|
const r = await uploadViaChooser(await resolveExistingTargetId(targetOpts(ctx.values)), click, files, ctx.values.timeout ?? 15e3);
|
|
1221
1264
|
if (r.delivered === 0) {
|
|
1222
1265
|
consola.error(`the file chooser opened (backendNodeId ${r.backendNodeId}) but the file never reached the page's change handler — the page likely replaced/destroyed the transient input before it could be set. Re-snapshot and confirm --click points at the real trigger, or try again.`);
|
|
@@ -1230,6 +1273,174 @@ const uploadCommand = define({
|
|
|
1230
1273
|
}
|
|
1231
1274
|
});
|
|
1232
1275
|
//#endregion
|
|
1276
|
+
//#region src/commands/drop.ts
|
|
1277
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
1278
|
+
/** Resolve each path to absolute and verify it's an existing file. */
|
|
1279
|
+
function absFiles(paths) {
|
|
1280
|
+
return paths.map((p) => {
|
|
1281
|
+
const abs = resolve(p);
|
|
1282
|
+
if (!existsSync(abs) || !statSync(abs).isFile()) {
|
|
1283
|
+
consola.error(`no such file: ${abs}`);
|
|
1284
|
+
process.exit(1);
|
|
1285
|
+
}
|
|
1286
|
+
return abs;
|
|
1287
|
+
});
|
|
1288
|
+
}
|
|
1289
|
+
/** Minimal extension→mime map for the --js path (where the page builds the File
|
|
1290
|
+
* from bytes and needs a type). The trusted CDP path doesn't need this — Chrome
|
|
1291
|
+
* reads the real file off disk and sets the type itself. */
|
|
1292
|
+
function guessMime(p) {
|
|
1293
|
+
return {
|
|
1294
|
+
".mp3": "audio/mpeg",
|
|
1295
|
+
".wav": "audio/wav",
|
|
1296
|
+
".ogg": "audio/ogg",
|
|
1297
|
+
".flac": "audio/flac",
|
|
1298
|
+
".m4a": "audio/mp4",
|
|
1299
|
+
".aac": "audio/aac",
|
|
1300
|
+
".mp4": "video/mp4",
|
|
1301
|
+
".mov": "video/quicktime",
|
|
1302
|
+
".webm": "video/webm",
|
|
1303
|
+
".png": "image/png",
|
|
1304
|
+
".jpg": "image/jpeg",
|
|
1305
|
+
".jpeg": "image/jpeg",
|
|
1306
|
+
".gif": "image/gif",
|
|
1307
|
+
".webp": "image/webp",
|
|
1308
|
+
".svg": "image/svg+xml",
|
|
1309
|
+
".pdf": "application/pdf",
|
|
1310
|
+
".zip": "application/zip",
|
|
1311
|
+
".json": "application/json",
|
|
1312
|
+
".csv": "text/csv",
|
|
1313
|
+
".txt": "text/plain"
|
|
1314
|
+
}[extname(p).toLowerCase()] || "application/octet-stream";
|
|
1315
|
+
}
|
|
1316
|
+
const dropCommand = define({
|
|
1317
|
+
name: "drop",
|
|
1318
|
+
description: "Drop local file(s) onto a drop-zone element by ref — for uploaders wired to drag-and-drop (a `drop` listener reading `e.dataTransfer.files`) rather than an <input type=file>. Default fires a genuinely-trusted CDP drag sequence (Input.dispatchDragEvent) carrying the real files from disk: brings the tab to front, waits for it to be focused+visible, waits for the target to be actionable (stable + unoccluded), then dragEnter→dragOver→drop — mirroring `click --trusted`. Use --js for a synthetic (isTrusted=false) DataTransfer drop that does NOT steal focus, for zones that accept synthetic events. For a static <input type=file> use `setfiles`; for a button that opens a file chooser use `upload`.",
|
|
1319
|
+
args: {
|
|
1320
|
+
...targetArgs,
|
|
1321
|
+
on: {
|
|
1322
|
+
type: "positional",
|
|
1323
|
+
description: "Ref of the drop-zone element (run snapshot first), e.g. e7"
|
|
1324
|
+
},
|
|
1325
|
+
files: {
|
|
1326
|
+
type: "positional",
|
|
1327
|
+
description: "One or more file paths (absolute, or resolved from cwd)"
|
|
1328
|
+
},
|
|
1329
|
+
js: {
|
|
1330
|
+
type: "boolean",
|
|
1331
|
+
description: "Dispatch a synthetic (isTrusted=false) DataTransfer drop instead of the trusted CDP path. Does not bring the tab to front (no focus steal). Injects each file's bytes into the page to build the File objects. Use for drop zones that accept synthetic events; falls short on zones that gate on isTrusted."
|
|
1332
|
+
}
|
|
1333
|
+
},
|
|
1334
|
+
async run(ctx) {
|
|
1335
|
+
const ref = ctx.positionals[1];
|
|
1336
|
+
const paths = ctx.positionals.slice(2);
|
|
1337
|
+
if (!ref || !isValidRef(ref)) {
|
|
1338
|
+
consola.error("A ref like \"e7\" is required (run `browser-automation snapshot` first)");
|
|
1339
|
+
process.exit(1);
|
|
1340
|
+
}
|
|
1341
|
+
if (paths.length === 0) {
|
|
1342
|
+
consola.error("At least one file path is required");
|
|
1343
|
+
process.exit(1);
|
|
1344
|
+
}
|
|
1345
|
+
const files = absFiles(paths);
|
|
1346
|
+
const targetId = await resolveExistingTargetId(targetOpts(ctx.values));
|
|
1347
|
+
if (ctx.values.js ?? false) {
|
|
1348
|
+
const r = await evaluate(targetId, dropExpr(ref, files.map((p) => ({
|
|
1349
|
+
name: basename(p),
|
|
1350
|
+
type: guessMime(p),
|
|
1351
|
+
b64: readFileSync(p).toString("base64")
|
|
1352
|
+
}))));
|
|
1353
|
+
if (r.err) {
|
|
1354
|
+
consola.error(r.err);
|
|
1355
|
+
process.exit(1);
|
|
1356
|
+
}
|
|
1357
|
+
consola.success(`dropped ${r.count} file(s) on ${ref} <${r.tag}> (synthetic — re-snapshot/screenshot to confirm the zone accepted them):`);
|
|
1358
|
+
for (const f of files) process.stdout.write(f + "\n");
|
|
1359
|
+
return;
|
|
1360
|
+
}
|
|
1361
|
+
const tag = await withPage(targetId, async (s) => {
|
|
1362
|
+
const evalIn = async (expr) => {
|
|
1363
|
+
const res = await s.send("Runtime.evaluate", {
|
|
1364
|
+
expression: expr,
|
|
1365
|
+
returnByValue: true,
|
|
1366
|
+
awaitPromise: true
|
|
1367
|
+
});
|
|
1368
|
+
if (res.exceptionDetails) {
|
|
1369
|
+
const e = res.exceptionDetails;
|
|
1370
|
+
throw new Error("page JS exception: " + (e.exception?.description || e.text || "unknown"));
|
|
1371
|
+
}
|
|
1372
|
+
return res.result?.value;
|
|
1373
|
+
};
|
|
1374
|
+
await forceForeground(s);
|
|
1375
|
+
const focusDeadline = Date.now() + 2e3;
|
|
1376
|
+
while (Date.now() < focusDeadline) {
|
|
1377
|
+
const f = await evalIn(`({f:document.hasFocus(),v:document.visibilityState})`);
|
|
1378
|
+
if (f && f.f && f.v === "visible") break;
|
|
1379
|
+
await sleep(50);
|
|
1380
|
+
}
|
|
1381
|
+
let prev = null;
|
|
1382
|
+
let point = null;
|
|
1383
|
+
let lastReason = "never became actionable";
|
|
1384
|
+
const actDeadline = Date.now() + 5e3;
|
|
1385
|
+
while (Date.now() < actDeadline) {
|
|
1386
|
+
const a = await evalIn(actionabilityExpr(ref));
|
|
1387
|
+
if (a?.err) {
|
|
1388
|
+
consola.error(a.err);
|
|
1389
|
+
process.exit(1);
|
|
1390
|
+
}
|
|
1391
|
+
const stable = prev !== null && Math.abs(prev.x - a.x) < .5 && Math.abs(prev.y - a.y) < .5;
|
|
1392
|
+
if (a.hit && stable) {
|
|
1393
|
+
point = {
|
|
1394
|
+
x: a.x,
|
|
1395
|
+
y: a.y,
|
|
1396
|
+
tag: a.tag
|
|
1397
|
+
};
|
|
1398
|
+
break;
|
|
1399
|
+
}
|
|
1400
|
+
lastReason = a.hit ? "position not yet stable (still animating)" : "drop-point occluded (hit-test miss)";
|
|
1401
|
+
prev = {
|
|
1402
|
+
x: a.x,
|
|
1403
|
+
y: a.y
|
|
1404
|
+
};
|
|
1405
|
+
await sleep(60);
|
|
1406
|
+
}
|
|
1407
|
+
if (!point) {
|
|
1408
|
+
consola.error(`<${ref}> not actionable for a drop: ${lastReason}`);
|
|
1409
|
+
process.exit(1);
|
|
1410
|
+
}
|
|
1411
|
+
const { x, y } = point;
|
|
1412
|
+
const data = {
|
|
1413
|
+
items: [],
|
|
1414
|
+
files,
|
|
1415
|
+
dragOperationsMask: 1
|
|
1416
|
+
};
|
|
1417
|
+
await s.send("Input.dispatchDragEvent", {
|
|
1418
|
+
type: "dragEnter",
|
|
1419
|
+
x,
|
|
1420
|
+
y,
|
|
1421
|
+
data
|
|
1422
|
+
});
|
|
1423
|
+
await s.send("Input.dispatchDragEvent", {
|
|
1424
|
+
type: "dragOver",
|
|
1425
|
+
x,
|
|
1426
|
+
y,
|
|
1427
|
+
data
|
|
1428
|
+
});
|
|
1429
|
+
await sleep(20);
|
|
1430
|
+
await s.send("Input.dispatchDragEvent", {
|
|
1431
|
+
type: "drop",
|
|
1432
|
+
x,
|
|
1433
|
+
y,
|
|
1434
|
+
data
|
|
1435
|
+
});
|
|
1436
|
+
return point.tag;
|
|
1437
|
+
});
|
|
1438
|
+
consola.success(`dropped ${files.length} file(s) on ${ref} <${tag}> (trusted):`);
|
|
1439
|
+
for (const f of files) process.stdout.write(f + "\n");
|
|
1440
|
+
consola.info("Re-snapshot or screenshot to confirm the zone accepted the drop — the DOM changed.");
|
|
1441
|
+
}
|
|
1442
|
+
});
|
|
1443
|
+
//#endregion
|
|
1233
1444
|
//#region src/core/network.ts
|
|
1234
1445
|
const INTERESTING = new Set([
|
|
1235
1446
|
"XHR",
|
|
@@ -1605,6 +1816,7 @@ const subCommands = new Map([
|
|
|
1605
1816
|
["download", downloadCommand],
|
|
1606
1817
|
["setfiles", setfilesCommand],
|
|
1607
1818
|
["upload", uploadCommand],
|
|
1819
|
+
["drop", dropCommand],
|
|
1608
1820
|
["network", networkCommand],
|
|
1609
1821
|
["screenshot", screenshotCommand],
|
|
1610
1822
|
["shot", screenshotCommand],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@generativereality/browser-automation",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.6",
|
|
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
|
@@ -85,6 +85,7 @@ Page commands take a tab selector — `-s <session>` (default `$BAC_SESSION`, el
|
|
|
85
85
|
| Download a file / CSV export | `browser-automation download -m bank --click e42` (or `--url <href>`) |
|
|
86
86
|
| Set files on a known `<input type=file>` | `browser-automation setfiles -m app e7 ~/Desktop/clip.mp4` |
|
|
87
87
|
| Upload via a button that opens a file chooser | `browser-automation upload -m app --click e9 ~/Desktop/clip.mp4` |
|
|
88
|
+
| Drop file(s) onto a drag-and-drop zone | `browser-automation drop -m app e7 ~/Desktop/clip.mp4` (add `--js` for a synthetic drop) |
|
|
88
89
|
| Inspect network (find the API, headers, bodies) | `browser-automation network -m bank --reload --filter api --headers --body` |
|
|
89
90
|
| Screenshot a tab | `browser-automation screenshot -m op.fi --full -o shot.png` |
|
|
90
91
|
| Forget a session (tab stays open) | `browser-automation close -s work` |
|
|
@@ -186,7 +187,7 @@ There's no `state-save`/`state-load` to manage — the profile *is* the auth sto
|
|
|
186
187
|
whose JS handler builds a client-side **blob** and clicks it via a *nested*
|
|
187
188
|
synthetic click won't fire (Chrome activation quirk) — for those, grab the
|
|
188
189
|
underlying export URL/endpoint and use `--url`, or the site's API.
|
|
189
|
-
- **File upload.**
|
|
190
|
+
- **File upload.** Three paths. The first two mirror Playwright. For a static, snapshot-able
|
|
190
191
|
`<input type=file>`, use `setfiles <ref> <path…>` — it resolves the ref and
|
|
191
192
|
calls `DOM.setFileInputFiles` (a file input's `.files` is read-only to page JS,
|
|
192
193
|
so a value-setter/`eval` can't populate it; CDP can, and fires trusted
|
|
@@ -207,6 +208,27 @@ There's no `state-save`/`state-load` to manage — the profile *is* the auth sto
|
|
|
207
208
|
successful run **adds another attachment** — retrying a "did it work?" upload a
|
|
208
209
|
few times silently produces duplicates (this cost us a dozen copies on a live ASC
|
|
209
210
|
reply). Upload once, verify, and only re-run if verification shows nothing staged.
|
|
211
|
+
The **third path is drag-and-drop**, for zones with NO `<input type=file>` at
|
|
212
|
+
all — a `drop` listener reading `e.dataTransfer.files` (vocalremover-style audio
|
|
213
|
+
tools, many image/video drop zones). Neither `setfiles` nor `upload` applies;
|
|
214
|
+
use `drop <ref> <path…>`. By default it fires a genuinely-**trusted** CDP drag
|
|
215
|
+
(`Input.dispatchDragEvent`) carrying the real files from disk: it force-fronts
|
|
216
|
+
the tab (bringToFront + focus emulation + active lifecycle — so the tab reports
|
|
217
|
+
focused+visible even when the Chrome window isn't the frontmost OS window),
|
|
218
|
+
waits for the zone to be actionable, then dragEnter→dragOver→drop. The
|
|
219
|
+
force-front matters: drop-zone uploaders commonly do their work only inside a
|
|
220
|
+
**user activation**, and CDP input grants no activation on a tab the renderer
|
|
221
|
+
considers `hidden` — which it is whenever the window is occluded (the usual
|
|
222
|
+
case while you work in the terminal). vocalremover.org is the canonical example:
|
|
223
|
+
the file reaches its `change`/`drop` handler either way, but it only uploads +
|
|
224
|
+
separates once activation is present. `--js` instead dispatches a synthetic
|
|
225
|
+
(isTrusted=false) `DataTransfer` drop **without** force-fronting/stealing focus,
|
|
226
|
+
for zones that accept synthetic events. The `<ref>` can be any snapshot element
|
|
227
|
+
sitting over the drop region (a heading/button inside the zone) — the drop
|
|
228
|
+
bubbles to the zone/document handler, so it works even when the drop div itself
|
|
229
|
+
isn't snapshot-interactive. After the drop, `read`/`screenshot`/`network` to
|
|
230
|
+
confirm processing started, then grab the result (often a download endpoint you
|
|
231
|
+
can pull with `download --url`).
|
|
210
232
|
- **Page still loading.** `goto` waits for the load event, but SPAs render after.
|
|
211
233
|
If a `read`/`snapshot` looks empty, re-run after a moment, or snapshot again
|
|
212
234
|
once a known element should be present.
|