browser-cdp 0.1.0 → 0.2.1
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 +10 -10
- package/cli.js +15 -3
- package/nav.js +8 -3
- package/package.json +1 -1
- package/start.js +43 -27
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ npm install -g browser-cdp
|
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
14
|
# Start browser with CDP enabled
|
|
15
|
-
browser-cdp start [browser] [--
|
|
15
|
+
browser-cdp start [browser] [--isolated] [--port=PORT]
|
|
16
16
|
|
|
17
17
|
# Navigate to URL
|
|
18
18
|
browser-cdp nav <url> [--new]
|
|
@@ -38,8 +38,8 @@ browser-cdp pick '<message>'
|
|
|
38
38
|
## Examples
|
|
39
39
|
|
|
40
40
|
```bash
|
|
41
|
-
# Start Brave with
|
|
42
|
-
browser-cdp start brave
|
|
41
|
+
# Start Brave with real profile
|
|
42
|
+
browser-cdp start brave
|
|
43
43
|
|
|
44
44
|
# Start Chrome on custom port
|
|
45
45
|
DEBUG_PORT=9333 browser-cdp start
|
|
@@ -74,12 +74,12 @@ browser-cdp nav https://example.com
|
|
|
74
74
|
|
|
75
75
|
## Supported Browsers
|
|
76
76
|
|
|
77
|
-
| Browser | Command |
|
|
78
|
-
|
|
79
|
-
| Chrome | `chrome` (default) |
|
|
80
|
-
| Brave | `brave` |
|
|
81
|
-
| Comet | `comet` |
|
|
82
|
-
| Edge | `edge` |
|
|
77
|
+
| Browser | Command |
|
|
78
|
+
|---------|---------|
|
|
79
|
+
| Chrome | `chrome` (default) |
|
|
80
|
+
| Brave | `brave` |
|
|
81
|
+
| Comet | `comet` |
|
|
82
|
+
| Edge | `edge` |
|
|
83
83
|
|
|
84
84
|
## Platform Support
|
|
85
85
|
|
|
@@ -97,7 +97,7 @@ Use `BROWSER_PATH` env var to override if your browser is installed elsewhere.
|
|
|
97
97
|
| Aspect | browser-cdp | Playwright Test Mode |
|
|
98
98
|
|--------|-------------|---------------------|
|
|
99
99
|
| Browser | Your installed Chrome/Brave/etc | Bundled Chromium |
|
|
100
|
-
| Profile |
|
|
100
|
+
| Profile | Real cookies/logins by default | Fresh test profile |
|
|
101
101
|
| Detection | Not detectable as automation | Automation flags present |
|
|
102
102
|
| Use case | Real-world testing, scraping | Isolated E2E tests |
|
|
103
103
|
|
package/cli.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
|
|
7
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
const pkg = JSON.parse(readFileSync(join(__dirname, "package.json"), "utf8"));
|
|
9
|
+
|
|
3
10
|
const command = process.argv[2];
|
|
4
11
|
const args = process.argv.slice(3);
|
|
5
12
|
|
|
@@ -12,12 +19,12 @@ const commands = {
|
|
|
12
19
|
};
|
|
13
20
|
|
|
14
21
|
function printUsage() {
|
|
15
|
-
console.log(
|
|
22
|
+
console.log(`browser-cdp v${pkg.version} - Browser automation via Chrome DevTools Protocol`);
|
|
16
23
|
console.log("");
|
|
17
24
|
console.log("Usage: browser-cdp <command> [options]");
|
|
18
25
|
console.log("");
|
|
19
26
|
console.log("Commands:");
|
|
20
|
-
console.log(" start [browser] Start browser with CDP
|
|
27
|
+
console.log(" start [browser] Start browser with CDP (uses real profile)");
|
|
21
28
|
console.log(" nav <url> Navigate to URL");
|
|
22
29
|
console.log(" eval '<code>' Evaluate JavaScript in page");
|
|
23
30
|
console.log(" screenshot Take screenshot of current page");
|
|
@@ -29,13 +36,18 @@ function printUsage() {
|
|
|
29
36
|
console.log(" BROWSER_PATH Custom browser executable path");
|
|
30
37
|
console.log("");
|
|
31
38
|
console.log("Examples:");
|
|
32
|
-
console.log(" browser-cdp start brave
|
|
39
|
+
console.log(" browser-cdp start brave");
|
|
33
40
|
console.log(" browser-cdp nav https://google.com");
|
|
34
41
|
console.log(" browser-cdp eval 'document.title'");
|
|
35
42
|
console.log(" browser-cdp screenshot");
|
|
36
43
|
process.exit(0);
|
|
37
44
|
}
|
|
38
45
|
|
|
46
|
+
if (command === "--version" || command === "-v") {
|
|
47
|
+
console.log(pkg.version);
|
|
48
|
+
process.exit(0);
|
|
49
|
+
}
|
|
50
|
+
|
|
39
51
|
if (!command || command === "--help" || command === "-h") {
|
|
40
52
|
printUsage();
|
|
41
53
|
}
|
package/nav.js
CHANGED
|
@@ -4,14 +4,19 @@ import { chromium } from "playwright";
|
|
|
4
4
|
|
|
5
5
|
const DEFAULT_PORT = process.env.DEBUG_PORT || 9222;
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
let url = process.argv[2];
|
|
8
8
|
const newTab = process.argv[3] === "--new";
|
|
9
9
|
|
|
10
|
+
// Add protocol if missing
|
|
11
|
+
if (url && !url.match(/^https?:\/\//i)) {
|
|
12
|
+
url = "https://" + url;
|
|
13
|
+
}
|
|
14
|
+
|
|
10
15
|
if (!url) {
|
|
11
16
|
console.log("Usage: nav.js <url> [--new]");
|
|
12
17
|
console.log("\nExamples:");
|
|
13
|
-
console.log(" nav.js
|
|
14
|
-
console.log(" nav.js
|
|
18
|
+
console.log(" nav.js example.com # Navigate current tab");
|
|
19
|
+
console.log(" nav.js example.com --new # Open in new tab");
|
|
15
20
|
process.exit(1);
|
|
16
21
|
}
|
|
17
22
|
|
package/package.json
CHANGED
package/start.js
CHANGED
|
@@ -51,23 +51,23 @@ const BROWSERS = {
|
|
|
51
51
|
const DEFAULT_PORT = 9222;
|
|
52
52
|
|
|
53
53
|
function printUsage() {
|
|
54
|
-
console.log("Usage: start.js [browser] [--
|
|
54
|
+
console.log("Usage: start.js [browser] [--isolated] [--port=PORT]");
|
|
55
55
|
console.log("\nBrowsers:");
|
|
56
56
|
console.log(" chrome - Google Chrome (default)");
|
|
57
57
|
console.log(" brave - Brave Browser");
|
|
58
58
|
console.log(" comet - Comet Browser");
|
|
59
59
|
console.log(" edge - Microsoft Edge");
|
|
60
60
|
console.log("\nOptions:");
|
|
61
|
-
console.log(" --
|
|
62
|
-
console.log(" --port=N
|
|
61
|
+
console.log(" --isolated Use isolated profile (default: real profile)");
|
|
62
|
+
console.log(" --port=N Use custom debugging port (default: 9222)");
|
|
63
63
|
console.log("\nEnvironment variables:");
|
|
64
64
|
console.log(" BROWSER Default browser (chrome, brave, comet, edge)");
|
|
65
65
|
console.log(" BROWSER_PATH Custom browser executable path");
|
|
66
66
|
console.log(" DEBUG_PORT Custom debugging port");
|
|
67
67
|
console.log("\nExamples:");
|
|
68
|
-
console.log(" start.js # Start Chrome with
|
|
69
|
-
console.log(" start.js brave
|
|
70
|
-
console.log(" start.js comet
|
|
68
|
+
console.log(" start.js # Start Chrome with real profile");
|
|
69
|
+
console.log(" start.js brave # Start Brave with real profile");
|
|
70
|
+
console.log(" start.js comet --isolated # Start Comet with isolated profile");
|
|
71
71
|
console.log(" start.js --port=9333 # Start Chrome on port 9333");
|
|
72
72
|
process.exit(1);
|
|
73
73
|
}
|
|
@@ -75,14 +75,14 @@ function printUsage() {
|
|
|
75
75
|
// Parse arguments
|
|
76
76
|
const args = process.argv.slice(2);
|
|
77
77
|
let browserName = process.env.BROWSER || "chrome";
|
|
78
|
-
let
|
|
78
|
+
let isolated = false;
|
|
79
79
|
let port = parseInt(process.env.DEBUG_PORT) || DEFAULT_PORT;
|
|
80
80
|
|
|
81
81
|
for (const arg of args) {
|
|
82
82
|
if (arg === "--help" || arg === "-h") {
|
|
83
83
|
printUsage();
|
|
84
|
-
} else if (arg === "--
|
|
85
|
-
|
|
84
|
+
} else if (arg === "--isolated") {
|
|
85
|
+
isolated = true;
|
|
86
86
|
} else if (arg.startsWith("--port=")) {
|
|
87
87
|
port = parseInt(arg.split("=")[1]);
|
|
88
88
|
} else if (BROWSERS[arg]) {
|
|
@@ -137,28 +137,46 @@ try {
|
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
-
//
|
|
141
|
-
|
|
142
|
-
? `${process.env.HOME}/Library/Caches`
|
|
143
|
-
: `${process.env.HOME}/.cache`;
|
|
144
|
-
const profileDir = `${cacheBase}/browser-cdp/${browserName}`;
|
|
145
|
-
execFileSync("mkdir", ["-p", profileDir], { stdio: "ignore" });
|
|
146
|
-
|
|
147
|
-
if (useProfile && browserConfig.profileSource) {
|
|
148
|
-
console.log(`Syncing profile from ${browserConfig.profileSource}...`);
|
|
140
|
+
// Check if browser is already running without CDP (only matters for real profile)
|
|
141
|
+
if (!isolated && browserConfig.process) {
|
|
149
142
|
try {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
143
|
+
const pgrepArgs = isMac
|
|
144
|
+
? ["-x", browserConfig.process]
|
|
145
|
+
: ["-f", browserConfig.path];
|
|
146
|
+
const result = execFileSync("pgrep", pgrepArgs, { encoding: "utf8" }).trim();
|
|
147
|
+
if (result) {
|
|
148
|
+
console.error(`Error: ${browserConfig.name} is already running without CDP enabled.`);
|
|
149
|
+
console.error("");
|
|
150
|
+
console.error("When a browser is already open, launching it again just opens a new");
|
|
151
|
+
console.error("window in the existing process - the CDP flag is ignored.");
|
|
152
|
+
console.error("");
|
|
153
|
+
console.error("Options:");
|
|
154
|
+
console.error(` 1. Quit ${browserConfig.name} and run this command again`);
|
|
155
|
+
console.error(` 2. Use --isolated flag: browser-cdp start ${browserName} --isolated`);
|
|
156
|
+
console.error(" (creates separate instance, but without your cookies/logins)");
|
|
157
|
+
process.exit(1);
|
|
158
|
+
}
|
|
153
159
|
} catch {
|
|
154
|
-
|
|
160
|
+
// pgrep returns non-zero if no match - browser not running, proceed
|
|
155
161
|
}
|
|
156
162
|
}
|
|
157
163
|
|
|
164
|
+
// Build browser arguments
|
|
165
|
+
const browserArgs = [`--remote-debugging-port=${port}`];
|
|
166
|
+
|
|
167
|
+
if (isolated) {
|
|
168
|
+
const cacheBase = isMac
|
|
169
|
+
? `${process.env.HOME}/Library/Caches`
|
|
170
|
+
: `${process.env.HOME}/.cache`;
|
|
171
|
+
const profileDir = `${cacheBase}/browser-cdp/${browserName}`;
|
|
172
|
+
execFileSync("mkdir", ["-p", profileDir], { stdio: "ignore" });
|
|
173
|
+
browserArgs.push(`--user-data-dir=${profileDir}`);
|
|
174
|
+
}
|
|
175
|
+
|
|
158
176
|
// Start browser
|
|
159
|
-
console.log(`Starting ${browserConfig.name} on port ${port}...`);
|
|
177
|
+
console.log(`Starting ${browserConfig.name} on port ${port}${isolated ? " (isolated)" : ""}...`);
|
|
160
178
|
|
|
161
|
-
spawn(browserPath,
|
|
179
|
+
spawn(browserPath, browserArgs, {
|
|
162
180
|
detached: true,
|
|
163
181
|
stdio: "ignore",
|
|
164
182
|
}).unref();
|
|
@@ -181,6 +199,4 @@ if (!connected) {
|
|
|
181
199
|
process.exit(1);
|
|
182
200
|
}
|
|
183
201
|
|
|
184
|
-
console.log(
|
|
185
|
-
`${browserConfig.name} started on :${port}${useProfile ? " with your profile" : ""}`
|
|
186
|
-
);
|
|
202
|
+
console.log(`${browserConfig.name} started on :${port}${isolated ? " (isolated)" : ""}`);
|