bb-browser 0.5.1 → 0.6.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/dist/cli.js CHANGED
@@ -126,6 +126,21 @@ async function isDaemonRunning() {
126
126
  return false;
127
127
  }
128
128
  }
129
+ async function isExtensionConnected() {
130
+ try {
131
+ const controller = new AbortController();
132
+ const timeoutId = setTimeout(() => controller.abort(), 2e3);
133
+ const response = await fetch(`${DAEMON_BASE_URL}/status`, {
134
+ signal: controller.signal
135
+ });
136
+ clearTimeout(timeoutId);
137
+ if (!response.ok) return false;
138
+ const data = await response.json();
139
+ return !!data.extensionConnected;
140
+ } catch {
141
+ return false;
142
+ }
143
+ }
129
144
  async function waitForDaemon(timeoutMs) {
130
145
  const startTime = Date.now();
131
146
  while (Date.now() - startTime < timeoutMs) {
@@ -156,6 +171,11 @@ async function ensureDaemonRunning() {
156
171
  "\u65E0\u6CD5\u542F\u52A8 Daemon\u3002\u8BF7\u624B\u52A8\u8FD0\u884C bb-browser daemon \u6216 bb-daemon \u542F\u52A8\u670D\u52A1"
157
172
  );
158
173
  }
174
+ const extStart = Date.now();
175
+ while (Date.now() - extStart < 1e4) {
176
+ if (await isExtensionConnected()) return;
177
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL));
178
+ }
159
179
  }
160
180
  async function stopDaemon() {
161
181
  try {
@@ -561,6 +581,58 @@ async function siteRun(name, args, options) {
561
581
  const jsBody = jsContent.replace(/\/\*\s*@meta[\s\S]*?\*\//, "").trim();
562
582
  const argsJson = JSON.stringify(argMap);
563
583
  const script = `(${jsBody})(${argsJson})`;
584
+ if (options.openclaw) {
585
+ const { ocGetTabs, ocFindTabByDomain, ocOpenTab, ocEvaluate } = await import("./openclaw-bridge-P3G4KGYM.js");
586
+ let targetId;
587
+ if (site.domain) {
588
+ const tabs = ocGetTabs();
589
+ const existing = ocFindTabByDomain(tabs, site.domain);
590
+ if (existing) {
591
+ targetId = existing.targetId;
592
+ } else {
593
+ targetId = ocOpenTab(`https://${site.domain}`);
594
+ await new Promise((resolve2) => setTimeout(resolve2, 3e3));
595
+ }
596
+ } else {
597
+ const tabs = ocGetTabs();
598
+ if (tabs.length === 0) {
599
+ throw new Error("No tabs open in OpenClaw browser");
600
+ }
601
+ targetId = tabs[0].targetId;
602
+ }
603
+ const wrappedFn = `async () => { const __fn = ${jsBody}; return await __fn(${argsJson}); }`;
604
+ const parsed2 = ocEvaluate(targetId, wrappedFn);
605
+ if (typeof parsed2 === "object" && parsed2 !== null && "error" in parsed2) {
606
+ const errObj = parsed2;
607
+ const checkText = `${errObj.error} ${errObj.hint || ""}`;
608
+ const isAuthError = /401|403|unauthorized|forbidden|not.?logged|login.?required|sign.?in|auth/i.test(checkText);
609
+ const loginHint = isAuthError && site.domain ? `Please log in to https://${site.domain} in your OpenClaw browser first, then retry.` : void 0;
610
+ const hint = loginHint || errObj.hint;
611
+ const reportHint = `If this is an adapter bug, report via: gh issue create --repo epiral/bb-sites --title "[${name}] <description>" OR: bb-browser site github/issue-create epiral/bb-sites --title "[${name}] <description>"`;
612
+ if (options.json) {
613
+ console.log(JSON.stringify({ id: "openclaw", success: false, error: errObj.error, hint, reportHint }));
614
+ } else {
615
+ console.error(`[error] site ${name}: ${errObj.error}`);
616
+ if (hint) console.error(` Hint: ${hint}`);
617
+ console.error(` Report: gh issue create --repo epiral/bb-sites --title "[${name}] ..."`);
618
+ console.error(` or: bb-browser site github/issue-create epiral/bb-sites --title "[${name}] ..."`);
619
+ }
620
+ process.exit(1);
621
+ }
622
+ if (options.jq) {
623
+ const { applyJq: applyJq2 } = await import("./jq-HHMLHEPA.js");
624
+ const expr = options.jq.replace(/^\.data\./, ".");
625
+ const results = applyJq2(parsed2, expr);
626
+ for (const r of results) {
627
+ console.log(typeof r === "string" ? r : JSON.stringify(r));
628
+ }
629
+ } else if (options.json) {
630
+ console.log(JSON.stringify({ id: "openclaw", success: true, data: parsed2 }));
631
+ } else {
632
+ console.log(JSON.stringify(parsed2, null, 2));
633
+ }
634
+ return;
635
+ }
564
636
  await ensureDaemonRunning();
565
637
  let targetTabId = options.tabId;
566
638
  if (!targetTabId && site.domain) {
@@ -2318,6 +2390,8 @@ function parseArgs(argv) {
2318
2390
  result.flags.jq = args[nextIdx];
2319
2391
  result.flags.json = true;
2320
2392
  }
2393
+ } else if (arg === "--openclaw") {
2394
+ result.flags.openclaw = true;
2321
2395
  } else if (arg === "--help" || arg === "-h") {
2322
2396
  result.flags.help = true;
2323
2397
  } else if (arg === "--version" || arg === "-v") {
@@ -2721,7 +2795,8 @@ async function main() {
2721
2795
  json: parsed.flags.json,
2722
2796
  jq: parsed.flags.jq,
2723
2797
  days: parsed.flags.days,
2724
- tabId: globalTabId
2798
+ tabId: globalTabId,
2799
+ openclaw: parsed.flags.openclaw
2725
2800
  });
2726
2801
  break;
2727
2802
  }