web-forager 0.0.0-stage → 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.
Files changed (82) hide show
  1. package/.claude-plugin/marketplace.json +16 -0
  2. package/.claude-plugin/plugin.json +20 -0
  3. package/.clinerules/web-forager.md +85 -0
  4. package/.codebuddy-plugin/marketplace.json +14 -0
  5. package/.codebuddy-plugin/plugin.json +25 -0
  6. package/.codex-plugin/marketplace.json +12 -0
  7. package/.codex-plugin/mcp.json +20 -0
  8. package/.codex-plugin/plugin.json +15 -0
  9. package/.continue/rules/web-forager.md +90 -0
  10. package/.cursor/rules/web-forager.mdc +80 -0
  11. package/.dsh-plugin/index.js +11 -0
  12. package/.dsh-plugin/plugin.json +5 -0
  13. package/.github/copilot-instructions.md +85 -0
  14. package/.mcp.json +12 -0
  15. package/.roo/rules/web-forager.md +85 -0
  16. package/.windsurf/rules/web-forager.md +90 -0
  17. package/AGENTS.md +112 -0
  18. package/CHANGELOG.md +454 -0
  19. package/LICENSE +21 -0
  20. package/README.md +477 -3
  21. package/README_zh.md +477 -0
  22. package/SKILL.md +351 -0
  23. package/THIRD_PARTY_LICENSES.txt +35 -0
  24. package/browser/bridges/playwright-bridge.js +375 -0
  25. package/browser/launch_chrome.py +144 -0
  26. package/browser/lib/backend.js +302 -0
  27. package/browser/lib/bili-api.js +79 -0
  28. package/browser/lib/bridge.js +83 -0
  29. package/browser/lib/context.js +60 -0
  30. package/browser/lib/doctor.js +97 -0
  31. package/browser/lib/errors.js +17 -0
  32. package/browser/lib/netguard.js +149 -0
  33. package/browser/lib/playbooks.js +30 -0
  34. package/browser/lib/record.js +109 -0
  35. package/browser/lib/robots.js +195 -0
  36. package/browser/lib/safety.js +115 -0
  37. package/browser/lib/selectors.js +80 -0
  38. package/browser/lib/session.js +116 -0
  39. package/browser/lib/snapshot.js +229 -0
  40. package/browser/lib/untrusted.js +56 -0
  41. package/browser/lib/workspace.js +40 -0
  42. package/browser/mcp_server.js +272 -0
  43. package/browser/package.json +40 -0
  44. package/browser/playbooks/bili-chain.js +58 -0
  45. package/browser/playbooks/bili-danmaku.js +102 -0
  46. package/browser/playbooks/bili-feed.js +66 -0
  47. package/browser/playbooks/bili-video.js +38 -0
  48. package/browser/playbooks/search-dig.js +105 -0
  49. package/browser/playbooks/shared.js +138 -0
  50. package/browser/playbooks/wechat-dig.js +78 -0
  51. package/browser/playbooks/xhs-dig.js +89 -0
  52. package/browser/playbooks/zhihu-dig.js +89 -0
  53. package/browser/run.js +432 -0
  54. package/commands/browse.md +53 -0
  55. package/commands/fetch.md +29 -0
  56. package/commands/map.md +24 -0
  57. package/commands/scholar.md +28 -0
  58. package/commands/surf.md +28 -0
  59. package/cordis.patch.yml +19 -0
  60. package/docs/ACKNOWLEDGEMENTS.md +15 -0
  61. package/docs/ACKNOWLEDGEMENTS_zh.md +15 -0
  62. package/docs/DEPENDENCIES.md +40 -0
  63. package/docs/INSTALL_COMMANDS.md +113 -0
  64. package/docs/INSTALL_COMMANDS_zh.md +113 -0
  65. package/docs/REFERENCE.md +308 -0
  66. package/docs/REFERENCE_zh.md +309 -0
  67. package/index.js +7 -0
  68. package/install.py +254 -0
  69. package/manifest.json +97 -0
  70. package/mcp_server.py +597 -0
  71. package/package.json +80 -6
  72. package/requirements.txt +12 -0
  73. package/run.py +852 -0
  74. package/scripts/__init__.py +1 -0
  75. package/scripts/bilibili.py +874 -0
  76. package/scripts/documents.py +437 -0
  77. package/scripts/engine.py +4220 -0
  78. package/scripts/feeds.py +180 -0
  79. package/scripts/pdf.py +135 -0
  80. package/scripts/scholar.py +354 -0
  81. package/scripts/sitemap.py +180 -0
  82. package/scripts/untrusted.py +95 -0
@@ -0,0 +1,375 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+
4
+ // Optional reference bridge for web-forager-browser.
5
+ //
6
+ // web-forager-browser itself has no runtime dependencies. This file only works when
7
+ // `playwright` is installed next to it (or globally resolvable). Modes:
8
+ //
9
+ // node bridges/playwright-bridge.js fresh chromium
10
+ // WEB_FORAGER_BROWSER_HEADFUL=1 node bridges/playwright-bridge.js visible window
11
+ // WEB_FORAGER_BROWSER_PROFILE=/path/to/profile node bridges/... persistent profile
12
+ // WEB_FORAGER_BROWSER_CDP=http://127.0.0.1:9222 node bridges/... attach to your browser
13
+ //
14
+ // Protocol: one JSON-RPC 2.0 request per line on stdin, one response per line
15
+ // on stdout. Methods: goto, snapshot, click, scroll, screenshot, tabs, observe.
16
+
17
+ const fs = require("node:fs");
18
+ const os = require("node:os");
19
+ const path = require("node:path");
20
+
21
+ let playwright;
22
+ try {
23
+ playwright = require("playwright");
24
+ } catch (error) {
25
+ process.stderr.write(
26
+ "playwright-bridge: playwright is not installed.\n" +
27
+ "Install it with `npm i playwright && npx playwright install chromium`, " +
28
+ "or point WEB_FORAGER_BRIDGE at your own bridge implementing the same protocol.\n",
29
+ );
30
+ process.exit(3);
31
+ }
32
+
33
+ // Every request the page makes — the document itself and each subresource —
34
+ // is checked against the private-address guard. A hostile page therefore
35
+ // cannot use the browser to read http://127.0.0.1:... or a metadata endpoint.
36
+ const { isPublicRequestUrl } = require("../lib/netguard");
37
+
38
+ const SNAPSHOT_SCRIPT = () => {
39
+ const nodes = [];
40
+ let seq = 0;
41
+ const visible = (el) => {
42
+ const rect = el.getBoundingClientRect();
43
+ const style = window.getComputedStyle(el);
44
+ return rect.width > 1 && rect.height > 1 && style.visibility !== "hidden" && style.display !== "none";
45
+ };
46
+ const push = (role, el) => {
47
+ if (!el || !visible(el)) return;
48
+ const name = (el.getAttribute("aria-label") || el.innerText || el.textContent || "")
49
+ .replace(/\s+/g, " ")
50
+ .trim()
51
+ .slice(0, 300);
52
+ if (!name) return;
53
+ const id = String(++seq);
54
+ el.setAttribute("data-web-forager-browser-id", id);
55
+ const node = { role, name, locator: `[data-web-forager-browser-id="${id}"]` };
56
+ if (el.tagName === "A") node.href = el.href || "";
57
+ nodes.push(node);
58
+ };
59
+ const scope = document.body || document.documentElement;
60
+ // WeChat renders the whole article inside #js_content. Hand it over as one
61
+ // node so the caller does not have to reassemble paragraph fragments; kept
62
+ // to WeChat so link/card detection on other sites is unchanged.
63
+ const pushContent = (el) => {
64
+ if (!el || !visible(el)) return;
65
+ const text = (el.innerText || el.textContent || "").replace(/\s+/g, " ").trim();
66
+ if (text.length < 80) return;
67
+ const id = String(++seq);
68
+ el.setAttribute("data-web-forager-browser-id", id);
69
+ nodes.push({
70
+ role: "content",
71
+ name: text.slice(0, 20000),
72
+ locator: `[data-web-forager-browser-id="${id}"]`,
73
+ });
74
+ };
75
+ if (/(^|\.)weixin\.qq\.com$/i.test(location.hostname)) {
76
+ pushContent(document.querySelector("#js_content"));
77
+ }
78
+ for (const el of scope.querySelectorAll("h1,h2,h3,h4")) push("heading", el);
79
+ for (const el of scope.querySelectorAll("a[href]")) push("link", el);
80
+ for (const el of scope.querySelectorAll("button,[role=button]")) push("button", el);
81
+ for (const el of scope.querySelectorAll("p,li,span,div")) {
82
+ if (el.children.length === 0 && (el.textContent || "").trim().length > 4) push("text", el);
83
+ }
84
+ return { url: location.href, title: document.title, nodes: nodes.slice(0, 800) };
85
+ };
86
+
87
+ let browser = null;
88
+ let context = null;
89
+
90
+ async function getPage() {
91
+ if (!context) throw new Error("no page: call goto first");
92
+ const pages = context.pages();
93
+ return pages.length ? pages[pages.length - 1] : context.newPage();
94
+ }
95
+
96
+ const DEFAULT_PROFILE = path.join(os.homedir(), ".web-forager", "browser", "profile");
97
+ // Sites fingerprint the default automation flags; drop the loudest one and
98
+ // keep navigator.webdriver from being set where the browser allows it.
99
+ const LAUNCH_ARGS = ["--disable-blink-features=AutomationControlled"];
100
+ const LAUNCH_OPTIONS = {
101
+ ignoreDefaultArgs: ["--enable-automation"],
102
+ args: LAUNCH_ARGS,
103
+ viewport: { width: 1280, height: 900 },
104
+ };
105
+
106
+ function profilePath() {
107
+ const configured = (process.env.WEB_FORAGER_BROWSER_PROFILE || "").trim();
108
+ if (["off", "0", "false", "none"].includes(configured.toLowerCase())) return "";
109
+ return path.resolve(configured || DEFAULT_PROFILE);
110
+ }
111
+
112
+ async function installNetGuard(active) {
113
+ try {
114
+ await active.route("**/*", async (route) => {
115
+ const url = route.request().url();
116
+ if (/^https?:/i.test(url) && !(await isPublicRequestUrl(url))) {
117
+ return route.abort("blockedbyclient");
118
+ }
119
+ return route.continue();
120
+ });
121
+ } catch (error) {
122
+ process.stderr.write(
123
+ `playwright-bridge: could not install the private-address guard (${error.message}); ` +
124
+ "navigation checks in the caller still apply.\n",
125
+ );
126
+ }
127
+ }
128
+
129
+ async function start() {
130
+ if (context) return;
131
+ const storageState = process.env.WEB_FORAGER_STORAGE_STATE || undefined;
132
+ if (process.env.WEB_FORAGER_BROWSER_CDP) {
133
+ browser = await playwright.chromium.connectOverCDP(process.env.WEB_FORAGER_BROWSER_CDP);
134
+ context = browser.contexts()[0] || (await browser.newContext());
135
+ await installNetGuard(context);
136
+ return;
137
+ }
138
+ const headless = process.env.WEB_FORAGER_BROWSER_HEADFUL !== "1";
139
+ // A fresh context per run has no cookies, and Bilibili, Sogou and Zhihu turn
140
+ // that into a risk-control page immediately. The profile persists by default;
141
+ // WEB_FORAGER_BROWSER_PROFILE=off gives the old throwaway browser.
142
+ const profile = profilePath();
143
+ if (profile) {
144
+ fs.mkdirSync(profile, { recursive: true });
145
+ context = await playwright.chromium.launchPersistentContext(profile, {
146
+ headless,
147
+ ...(storageState ? { storageState } : {}),
148
+ ...LAUNCH_OPTIONS,
149
+ });
150
+ await installNetGuard(context);
151
+ return;
152
+ }
153
+ browser = await playwright.chromium.launch({ headless, ...LAUNCH_OPTIONS });
154
+ context = await browser.newContext({
155
+ viewport: { width: 1280, height: 900 },
156
+ ...(storageState ? { storageState } : {}),
157
+ });
158
+ await installNetGuard(context);
159
+ }
160
+
161
+ const SETTLE_MS = 500;
162
+
163
+ async function goto(params) {
164
+ await start();
165
+ const page = context.pages()[0] || (await context.newPage());
166
+ await page.goto(String(params.url), { waitUntil: "domcontentloaded", timeout: 45000 });
167
+ // domcontentloaded fires before the page's own redirects run; a login wall or
168
+ // verification page then swaps the document out from under the next call.
169
+ // Wait for the load event when it comes, then let the redirect settle.
170
+ await page.waitForLoadState("load", { timeout: 5000 }).catch(() => {});
171
+ await page.waitForTimeout(SETTLE_MS);
172
+ const title = await page.title().catch(() => "");
173
+ return { url: page.url(), title };
174
+ }
175
+
176
+ async function snapshot() {
177
+ const page = await getPage();
178
+ try {
179
+ return await page.evaluate(SNAPSHOT_SCRIPT);
180
+ } catch (error) {
181
+ const message = String(error && error.message ? error.message : error);
182
+ if (!/context was destroyed|navigat|target closed|frame was detached/i.test(message)) throw error;
183
+ // The document changed after goto: wait for the new one and read it once
184
+ // more, reporting where the page actually ended up if that also fails.
185
+ await page.waitForTimeout(2000);
186
+ try {
187
+ return await page.evaluate(SNAPSHOT_SCRIPT);
188
+ } catch (retryError) {
189
+ const url = page.url();
190
+ const wrapped = new Error(
191
+ `snapshot failed after a redirect (page is at ${url}): ${retryError.message || retryError}`,
192
+ );
193
+ wrapped.url = url;
194
+ throw wrapped;
195
+ }
196
+ }
197
+ }
198
+
199
+ function locate(page, locator) {
200
+ const value = String(locator || "");
201
+ if (/^[.#[\]]/.test(value) || value.includes("[data-web-forager-browser-id")) return page.locator(value).first();
202
+ return page.getByText(value, { exact: false }).first();
203
+ }
204
+
205
+ async function click(params) {
206
+ const page = await getPage();
207
+ await locate(page, params.locator).click({ timeout: 15000 });
208
+ return { ok: true, url: page.url() };
209
+ }
210
+
211
+ async function scroll(params) {
212
+ const page = await getPage();
213
+ await page.mouse.wheel(0, Number(params.dy || 600));
214
+ await page.waitForTimeout(400);
215
+ return { ok: true };
216
+ }
217
+
218
+ async function screenshot() {
219
+ const page = await getPage();
220
+ const dir = path.join(os.homedir(), ".web-forager/browser", "screenshots");
221
+ fs.mkdirSync(dir, { recursive: true });
222
+ const file = path.join(dir, `${Date.now()}.png`);
223
+ await page.screenshot({ path: file, fullPage: false });
224
+ return { path: file };
225
+ }
226
+
227
+ async function currentUrl() {
228
+ if (!context) return { url: "" };
229
+ const page = await getPage();
230
+ return { url: page.url(), title: await page.title().catch(() => "") };
231
+ }
232
+
233
+ async function tabs() {
234
+ if (!context) return { tabs: [] };
235
+ const pages = context.pages();
236
+ const out = [];
237
+ for (const page of pages) out.push({ url: page.url(), title: await page.title() });
238
+ return { tabs: out };
239
+ }
240
+
241
+ function stamp(seconds) {
242
+ const total = Math.max(0, Math.floor(seconds));
243
+ const minutes = String(Math.floor(total / 60)).padStart(2, "0");
244
+ const rest = String(total % 60).padStart(2, "0");
245
+ return `${minutes}:${rest}`;
246
+ }
247
+
248
+ async function observeDanmaku(page, ms) {
249
+ const seen = new Set();
250
+ const entries = [];
251
+ const deadline = Date.now() + ms;
252
+ while (Date.now() < deadline) {
253
+ const batch = await page.evaluate(() => {
254
+ const video = document.querySelector("video");
255
+ const time = video && Number.isFinite(video.currentTime) ? video.currentTime : 0;
256
+ const nodes = Array.from(
257
+ document.querySelectorAll('[class*="danmaku"], [class*="danmaku-item"], [class*="bili-danmaku"]'),
258
+ );
259
+ const texts = nodes.map((el) => (el.textContent || "").replace(/\s+/g, " ").trim()).filter(Boolean);
260
+ return { time, texts };
261
+ });
262
+ for (const text of batch.texts) {
263
+ const key = text.slice(0, 120);
264
+ if (seen.has(key)) continue;
265
+ seen.add(key);
266
+ entries.push({ time: stamp(batch.time), text: key });
267
+ }
268
+ await page.waitForTimeout(300);
269
+ }
270
+ return { topic: "danmaku", entries: entries.slice(0, 500) };
271
+ }
272
+
273
+ async function observeNetwork(page, ms) {
274
+ const events = [];
275
+ const listener = (response) => {
276
+ if (events.length >= 300) return;
277
+ const request = response.request();
278
+ events.push({
279
+ url: response.url(),
280
+ status: response.status(),
281
+ method: request.method(),
282
+ resource_type: request.resourceType(),
283
+ content_type: (response.headers() || {})["content-type"] || "",
284
+ });
285
+ };
286
+ page.on("response", listener);
287
+ try {
288
+ await page.waitForTimeout(ms);
289
+ } finally {
290
+ page.off("response", listener);
291
+ }
292
+ return { topic: "network", events };
293
+ }
294
+
295
+ async function observe(params) {
296
+ const page = await getPage();
297
+ const topic = String(params.topic || "danmaku");
298
+ const ms = Math.min(Math.max(Number(params.ms || 3000), 0), 60000);
299
+ if (topic === "network") return observeNetwork(page, ms);
300
+ return observeDanmaku(page, ms);
301
+ }
302
+
303
+ // Declare what this bridge actually implements, so web-forager-browser never
304
+ // assumes a capability the bridge cannot serve.
305
+ function describe() {
306
+ return {
307
+ protocol: 1,
308
+ capabilities: { loginState: true, screenshot: true, cua: true, observe: true },
309
+ modes: {
310
+ headful: process.env.WEB_FORAGER_BROWSER_HEADFUL === "1",
311
+ profile: Boolean(process.env.WEB_FORAGER_BROWSER_PROFILE),
312
+ cdp: Boolean(process.env.WEB_FORAGER_BROWSER_CDP),
313
+ },
314
+ };
315
+ }
316
+
317
+ async function storageState(params) {
318
+ await start();
319
+ const target = params.path ? String(params.path) : "";
320
+ const state = await context.storageState(target ? { path: target } : {});
321
+ // Never return the cookie values themselves; the caller only needs to know
322
+ // that the file was written.
323
+ return {
324
+ saved: Boolean(target),
325
+ path: target,
326
+ cookies: Array.isArray(state.cookies) ? state.cookies.length : 0,
327
+ origins: Array.isArray(state.origins) ? state.origins.length : 0,
328
+ };
329
+ }
330
+
331
+ const METHODS = { describe, goto, snapshot, click, scroll, screenshot, tabs, url: currentUrl, observe, storage_state: storageState };
332
+
333
+ function send(message) {
334
+ process.stdout.write(`${JSON.stringify(message)}\n`);
335
+ }
336
+
337
+ async function run() {
338
+ let buffer = "";
339
+ process.stdin.setEncoding("utf8");
340
+ for await (const chunk of process.stdin) {
341
+ buffer += chunk;
342
+ let newline = buffer.indexOf("\n");
343
+ while (newline >= 0) {
344
+ const line = buffer.slice(0, newline).trim();
345
+ buffer = buffer.slice(newline + 1);
346
+ newline = buffer.indexOf("\n");
347
+ if (!line) continue;
348
+ let message;
349
+ try {
350
+ message = JSON.parse(line);
351
+ } catch {
352
+ send({ jsonrpc: "2.0", id: null, error: { code: "protocol", message: "parse error" } });
353
+ continue;
354
+ }
355
+ const handler = METHODS[message.method];
356
+ if (!handler) {
357
+ send({ jsonrpc: "2.0", id: message.id, error: { code: "protocol", message: `unknown method: ${message.method}` } });
358
+ continue;
359
+ }
360
+ try {
361
+ const result = await handler(message.params || {});
362
+ send({ jsonrpc: "2.0", id: message.id, result });
363
+ } catch (error) {
364
+ send({ jsonrpc: "2.0", id: message.id, error: { code: "navigation", message: error.message } });
365
+ }
366
+ }
367
+ }
368
+ if (context) await context.close();
369
+ if (browser) await browser.close();
370
+ }
371
+
372
+ run().catch((error) => {
373
+ process.stderr.write(`playwright-bridge: ${error.stack || error.message}\n`);
374
+ process.exit(1);
375
+ });
@@ -0,0 +1,144 @@
1
+ #!/usr/bin/env python3
2
+ """Start a Chrome/Chromium with a dedicated profile for browser mode.
3
+
4
+ The profile is the login state: sign in once in the window this opens, and
5
+ later runs reuse it. Nothing here touches the browser you use every day.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import argparse
11
+ import json
12
+ import os
13
+ import shutil
14
+ import subprocess
15
+ import sys
16
+ import time
17
+ import urllib.error
18
+ import urllib.request
19
+ from pathlib import Path
20
+
21
+
22
+ def default_profile() -> Path:
23
+ return Path.home() / ".web-forager" / "browser" / "profile"
24
+
25
+
26
+ def candidates() -> list[str]:
27
+ found: list[str] = []
28
+ if sys.platform == "win32":
29
+ roots = [
30
+ os.environ.get("PROGRAMFILES", r"C:\Program Files"),
31
+ os.environ.get("PROGRAMFILES(X86)", r"C:\Program Files (x86)"),
32
+ os.environ.get("LOCALAPPDATA", ""),
33
+ ]
34
+ relative = [
35
+ r"Google\Chrome\Application\chrome.exe",
36
+ r"Microsoft\Edge\Application\msedge.exe",
37
+ r"Chromium\Application\chrome.exe",
38
+ ]
39
+ for root in roots:
40
+ if not root:
41
+ continue
42
+ for tail in relative:
43
+ found.append(str(Path(root) / tail))
44
+ elif sys.platform == "darwin":
45
+ found.extend(
46
+ [
47
+ "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
48
+ "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
49
+ "/Applications/Chromium.app/Contents/MacOS/Chromium",
50
+ ]
51
+ )
52
+ else:
53
+ for name in ("google-chrome", "google-chrome-stable", "chromium", "chromium-browser", "microsoft-edge"):
54
+ path = shutil.which(name)
55
+ if path:
56
+ found.append(path)
57
+ return found
58
+
59
+
60
+ def find_chrome(explicit: str = "") -> str:
61
+ if explicit:
62
+ return explicit
63
+ for path in candidates():
64
+ if Path(path).is_file():
65
+ return path
66
+ for name in ("chrome", "google-chrome", "chromium", "msedge"):
67
+ path = shutil.which(name)
68
+ if path:
69
+ return path
70
+ raise SystemExit(
71
+ "Chrome/Chromium not found. Pass --chrome <path>, or install Chrome, "
72
+ "Chromium or Edge first."
73
+ )
74
+
75
+
76
+ def launch_command(chrome: str, profile: Path, port: int) -> list[str]:
77
+ return [
78
+ chrome,
79
+ f"--user-data-dir={profile}",
80
+ f"--remote-debugging-port={port}",
81
+ "--no-first-run",
82
+ "--no-default-browser-check",
83
+ "about:blank",
84
+ ]
85
+
86
+
87
+ def wait_for_devtools(port: int, timeout: float = 15.0) -> bool:
88
+ endpoint = f"http://127.0.0.1:{port}/json/version"
89
+ deadline = time.monotonic() + timeout
90
+ while time.monotonic() < deadline:
91
+ try:
92
+ with urllib.request.urlopen(endpoint, timeout=1.0) as response:
93
+ payload = json.loads(response.read().decode("utf-8"))
94
+ if payload.get("webSocketDebuggerUrl"):
95
+ return True
96
+ except (urllib.error.URLError, TimeoutError, ValueError, OSError):
97
+ pass
98
+ time.sleep(0.25)
99
+ return False
100
+
101
+
102
+ def main(argv: list[str] | None = None) -> int:
103
+ parser = argparse.ArgumentParser(description=__doc__)
104
+ parser.add_argument("--chrome", default="", help="path to chrome.exe / Chrome binary")
105
+ parser.add_argument("--profile", default=str(default_profile()), help="profile directory")
106
+ parser.add_argument("--port", type=int, default=9222, help="DevTools port (default: 9222)")
107
+ parser.add_argument("--dry-run", action="store_true", help="print the command and exit")
108
+ parser.add_argument("--timeout", type=float, default=15.0, help="seconds to wait for DevTools")
109
+ args = parser.parse_args(argv)
110
+
111
+ chrome = find_chrome(args.chrome)
112
+ profile = Path(args.profile).expanduser().resolve()
113
+ command = launch_command(chrome, profile, args.port)
114
+
115
+ if args.dry_run:
116
+ print(" ".join(f'"{part}"' if " " in part else part for part in command))
117
+ print(f"WEB_FORAGER_BROWSER_CDP=http://127.0.0.1:{args.port}")
118
+ return 0
119
+
120
+ profile.mkdir(parents=True, exist_ok=True)
121
+ if os.name != "nt":
122
+ try:
123
+ profile.chmod(0o700)
124
+ except OSError:
125
+ pass
126
+
127
+ child = subprocess.Popen(command, close_fds=True)
128
+ if wait_for_devtools(args.port, timeout=args.timeout):
129
+ print(f"Chrome is ready with the dedicated profile: {profile}")
130
+ print(f"WEB_FORAGER_BROWSER_CDP=http://127.0.0.1:{args.port}")
131
+ print("Sign in once in that window; later runs reuse the profile.")
132
+ print("Ctrl+C here stops this launcher; the browser keeps running.")
133
+ return 0
134
+
135
+ print(
136
+ f"Chrome started (pid {child.pid}) but DevTools did not answer on port {args.port}.\n"
137
+ "Close other Chrome windows using the same profile, or choose another --port.",
138
+ file=sys.stderr,
139
+ )
140
+ return 3
141
+
142
+
143
+ if __name__ == "__main__":
144
+ raise SystemExit(main())