@jackwener/opencli 1.5.7 → 1.5.9
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/CHANGELOG.md +29 -0
- package/README.md +17 -1
- package/README.zh-CN.md +17 -1
- package/dist/browser/base-page.d.ts +48 -0
- package/dist/browser/base-page.js +160 -0
- package/dist/browser/cdp.js +4 -106
- package/dist/browser/daemon-client.d.ts +1 -7
- package/dist/browser/daemon-client.js +2 -9
- package/dist/browser/discover.d.ts +1 -4
- package/dist/browser/discover.js +1 -4
- package/dist/browser/errors.d.ts +4 -0
- package/dist/browser/errors.js +20 -0
- package/dist/browser/index.d.ts +1 -1
- package/dist/browser/index.js +1 -1
- package/dist/browser/page.d.ts +6 -35
- package/dist/browser/page.js +10 -189
- package/dist/browser/tabs.js +5 -5
- package/dist/browser.test.js +15 -15
- package/dist/cli-manifest.json +294 -22
- package/dist/clis/amazon/bestsellers.d.ts +21 -0
- package/dist/clis/amazon/bestsellers.js +130 -0
- package/dist/clis/amazon/bestsellers.test.js +20 -0
- package/dist/clis/amazon/discussion.d.ts +20 -0
- package/dist/clis/amazon/discussion.js +91 -0
- package/dist/clis/amazon/discussion.test.js +36 -0
- package/dist/clis/amazon/offer.d.ts +23 -0
- package/dist/clis/amazon/offer.js +140 -0
- package/dist/clis/amazon/offer.test.d.ts +1 -0
- package/dist/clis/amazon/offer.test.js +29 -0
- package/dist/clis/amazon/product.d.ts +18 -0
- package/dist/clis/amazon/product.js +92 -0
- package/dist/clis/amazon/product.test.d.ts +1 -0
- package/dist/clis/amazon/product.test.js +24 -0
- package/dist/clis/amazon/search.d.ts +18 -0
- package/dist/clis/amazon/search.js +87 -0
- package/dist/clis/amazon/search.test.d.ts +1 -0
- package/dist/clis/amazon/search.test.js +22 -0
- package/dist/clis/amazon/shared.d.ts +64 -0
- package/dist/clis/amazon/shared.js +255 -0
- package/dist/clis/amazon/shared.test.d.ts +1 -0
- package/dist/clis/amazon/shared.test.js +33 -0
- package/dist/clis/gemini/ask.d.ts +1 -0
- package/dist/clis/gemini/ask.js +40 -0
- package/dist/clis/gemini/image.d.ts +1 -0
- package/dist/clis/gemini/image.js +105 -0
- package/dist/clis/gemini/new.d.ts +1 -0
- package/dist/clis/gemini/new.js +20 -0
- package/dist/clis/gemini/utils.d.ts +34 -0
- package/dist/clis/gemini/utils.js +463 -0
- package/dist/clis/gemini/utils.test.d.ts +1 -0
- package/dist/clis/gemini/utils.test.js +31 -0
- package/dist/clis/notebooklm/compat.test.d.ts +1 -1
- package/dist/clis/notebooklm/compat.test.js +3 -3
- package/dist/clis/notebooklm/current.js +2 -3
- package/dist/clis/notebooklm/get.js +2 -3
- package/dist/clis/notebooklm/history.js +2 -3
- package/dist/clis/notebooklm/note-list.js +2 -3
- package/dist/clis/notebooklm/notes-get.js +2 -3
- package/dist/clis/notebooklm/open.d.ts +1 -0
- package/dist/clis/notebooklm/open.js +41 -0
- package/dist/clis/notebooklm/open.test.d.ts +1 -0
- package/dist/clis/notebooklm/open.test.js +63 -0
- package/dist/clis/notebooklm/source-fulltext.js +2 -3
- package/dist/clis/notebooklm/source-get.js +2 -3
- package/dist/clis/notebooklm/source-guide.js +2 -3
- package/dist/clis/notebooklm/source-list.js +2 -3
- package/dist/clis/notebooklm/status.js +1 -2
- package/dist/clis/notebooklm/summary.js +2 -3
- package/dist/clis/notebooklm/utils.d.ts +2 -1
- package/dist/clis/notebooklm/utils.js +20 -21
- package/dist/clis/xiaohongshu/creator-note-detail.test.js +11 -11
- package/dist/clis/xiaohongshu/creator-notes-summary.test.js +6 -6
- package/dist/clis/xiaohongshu/creator-notes.test.js +22 -22
- package/dist/commanderAdapter.js +6 -3
- package/dist/commanderAdapter.test.js +33 -0
- package/dist/commands/daemon.js +1 -1
- package/dist/commands/daemon.test.js +1 -1
- package/dist/doctor.d.ts +1 -2
- package/dist/doctor.js +7 -8
- package/dist/explore.js +1 -1
- package/dist/extension-manifest-regression.test.js +1 -0
- package/dist/output.js +28 -0
- package/dist/output.test.js +15 -0
- package/dist/pipeline/executor.js +2 -7
- package/dist/pipeline/steps/browser.js +1 -1
- package/dist/pipeline/template.js +25 -3
- package/dist/record.d.ts +50 -0
- package/dist/record.js +298 -57
- package/dist/record.test.d.ts +1 -0
- package/dist/record.test.js +293 -0
- package/dist/registry.d.ts +2 -0
- package/dist/registry.js +1 -0
- package/dist/registry.test.js +10 -0
- package/dist/runtime.js +3 -3
- package/dist/snapshotFormatter.d.ts +1 -1
- package/dist/snapshotFormatter.js +4 -4
- package/dist/snapshotFormatter.test.d.ts +1 -1
- package/dist/snapshotFormatter.test.js +2 -2
- package/dist/types.d.ts +3 -1
- package/dist/types.js +1 -1
- package/docs/.vitepress/config.mts +2 -0
- package/docs/adapters/browser/amazon.md +53 -0
- package/docs/adapters/browser/gemini.md +72 -0
- package/docs/adapters/browser/notebooklm.md +5 -5
- package/docs/adapters/index.md +3 -1
- package/extension/dist/background.js +614 -794
- package/extension/manifest.json +2 -1
- package/extension/src/background.test.ts +7 -163
- package/extension/src/background.ts +7 -156
- package/extension/src/cdp.test.ts +75 -0
- package/extension/src/cdp.ts +77 -3
- package/extension/src/protocol.ts +1 -5
- package/package.json +1 -1
- package/skills/opencli-explorer/SKILL.md +847 -0
- package/skills/opencli-oneshot/SKILL.md +216 -0
- package/skills/opencli-usage/SKILL.md +71 -0
- package/skills/opencli-usage/browser.md +429 -0
- package/skills/opencli-usage/desktop.md +118 -0
- package/skills/opencli-usage/plugins.md +82 -0
- package/skills/opencli-usage/public-api.md +149 -0
- package/src/browser/base-page.ts +197 -0
- package/src/browser/cdp.ts +7 -131
- package/src/browser/daemon-client.ts +3 -14
- package/src/browser/discover.ts +1 -4
- package/src/browser/errors.ts +22 -0
- package/src/browser/index.ts +1 -1
- package/src/browser/page.ts +13 -212
- package/src/browser/tabs.ts +5 -5
- package/src/browser.test.ts +15 -15
- package/src/clis/amazon/bestsellers.test.ts +22 -0
- package/src/clis/amazon/bestsellers.ts +180 -0
- package/src/clis/amazon/discussion.test.ts +38 -0
- package/src/clis/amazon/discussion.ts +131 -0
- package/src/clis/amazon/offer.test.ts +35 -0
- package/src/clis/amazon/offer.ts +185 -0
- package/src/clis/amazon/product.test.ts +26 -0
- package/src/clis/amazon/product.ts +131 -0
- package/src/clis/amazon/search.test.ts +24 -0
- package/src/clis/amazon/search.ts +128 -0
- package/src/clis/amazon/shared.test.ts +37 -0
- package/src/clis/amazon/shared.ts +316 -0
- package/src/clis/gemini/ask.ts +46 -0
- package/src/clis/gemini/image.ts +115 -0
- package/src/clis/gemini/new.ts +22 -0
- package/src/clis/gemini/utils.test.ts +36 -0
- package/src/clis/gemini/utils.ts +523 -0
- package/src/clis/notebooklm/compat.test.ts +3 -3
- package/src/clis/notebooklm/current.ts +2 -3
- package/src/clis/notebooklm/get.ts +1 -3
- package/src/clis/notebooklm/history.ts +1 -3
- package/src/clis/notebooklm/note-list.ts +1 -3
- package/src/clis/notebooklm/notes-get.ts +1 -3
- package/src/clis/notebooklm/open.test.ts +78 -0
- package/src/clis/notebooklm/open.ts +61 -0
- package/src/clis/notebooklm/source-fulltext.ts +1 -3
- package/src/clis/notebooklm/source-get.ts +1 -3
- package/src/clis/notebooklm/source-guide.ts +1 -3
- package/src/clis/notebooklm/source-list.ts +1 -3
- package/src/clis/notebooklm/status.ts +1 -2
- package/src/clis/notebooklm/summary.ts +1 -3
- package/src/clis/notebooklm/utils.ts +29 -20
- package/src/clis/xiaohongshu/creator-note-detail.test.ts +11 -11
- package/src/clis/xiaohongshu/creator-notes-summary.test.ts +6 -6
- package/src/clis/xiaohongshu/creator-notes.test.ts +22 -22
- package/src/commanderAdapter.test.ts +47 -0
- package/src/commanderAdapter.ts +7 -3
- package/src/commands/daemon.test.ts +1 -1
- package/src/commands/daemon.ts +1 -1
- package/src/doctor.ts +7 -8
- package/src/explore.ts +1 -1
- package/src/extension-manifest-regression.test.ts +1 -0
- package/src/output.test.ts +17 -0
- package/src/output.ts +27 -0
- package/src/pipeline/executor.ts +2 -7
- package/src/pipeline/steps/browser.ts +1 -1
- package/src/pipeline/template.ts +27 -4
- package/src/record.test.ts +362 -0
- package/src/record.ts +341 -62
- package/src/registry.test.ts +12 -0
- package/src/registry.ts +3 -0
- package/src/runtime.ts +3 -3
- package/src/snapshotFormatter.test.ts +2 -2
- package/src/snapshotFormatter.ts +4 -4
- package/src/types.ts +3 -1
- package/.agents/skills/cross-project-adapter-migration/SKILL.md +0 -249
- package/.agents/workflows/cross-project-adapter-migration.md +0 -54
- package/SKILL.md +0 -879
- package/dist/clis/notebooklm/bind-current.js +0 -29
- package/dist/clis/notebooklm/bind-current.test.d.ts +0 -1
- package/dist/clis/notebooklm/bind-current.test.js +0 -35
- package/dist/clis/notebooklm/binding.test.js +0 -44
- package/src/clis/notebooklm/bind-current.test.ts +0 -43
- package/src/clis/notebooklm/bind-current.ts +0 -36
- package/src/clis/notebooklm/binding.test.ts +0 -53
- /package/dist/browser/{mcp.d.ts → bridge.d.ts} +0 -0
- /package/dist/browser/{mcp.js → bridge.js} +0 -0
- /package/dist/clis/{notebooklm/bind-current.d.ts → amazon/bestsellers.test.d.ts} +0 -0
- /package/dist/clis/{notebooklm/binding.test.d.ts → amazon/discussion.test.d.ts} +0 -0
- /package/src/browser/{mcp.ts → bridge.ts} +0 -0
|
@@ -1,861 +1,681 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
//#endregion
|
|
13
|
-
//#region src/cdp.ts
|
|
14
|
-
/**
|
|
15
|
-
* CDP execution via chrome.debugger API.
|
|
16
|
-
*
|
|
17
|
-
* chrome.debugger only needs the "debugger" permission — no host_permissions.
|
|
18
|
-
* It can attach to any http/https tab. Avoid chrome:// and chrome-extension://
|
|
19
|
-
* tabs (resolveTabId in background.ts filters them).
|
|
20
|
-
*/
|
|
21
|
-
var attached = /* @__PURE__ */ new Set();
|
|
22
|
-
/** Internal blank page used when no user URL is provided. */
|
|
23
|
-
var BLANK_PAGE$1 = "data:text/html,<html></html>";
|
|
24
|
-
/** Check if a URL can be attached via CDP — only allow http(s) and our internal blank page. */
|
|
1
|
+
const DAEMON_PORT = 19825;
|
|
2
|
+
const DAEMON_HOST = "localhost";
|
|
3
|
+
const DAEMON_WS_URL = `ws://${DAEMON_HOST}:${DAEMON_PORT}/ext`;
|
|
4
|
+
const DAEMON_PING_URL = `http://${DAEMON_HOST}:${DAEMON_PORT}/ping`;
|
|
5
|
+
const WS_RECONNECT_BASE_DELAY = 2e3;
|
|
6
|
+
const WS_RECONNECT_MAX_DELAY = 5e3;
|
|
7
|
+
|
|
8
|
+
const attached = /* @__PURE__ */ new Set();
|
|
9
|
+
const BLANK_PAGE$1 = "data:text/html,<html></html>";
|
|
10
|
+
const FOREIGN_EXTENSION_URL_PREFIX = "chrome-extension://";
|
|
11
|
+
const ATTACH_RECOVERY_DELAY_MS = 120;
|
|
25
12
|
function isDebuggableUrl$1(url) {
|
|
26
|
-
|
|
27
|
-
|
|
13
|
+
if (!url) return true;
|
|
14
|
+
return url.startsWith("http://") || url.startsWith("https://") || url === BLANK_PAGE$1;
|
|
15
|
+
}
|
|
16
|
+
async function removeForeignExtensionEmbeds(tabId) {
|
|
17
|
+
const tab = await chrome.tabs.get(tabId);
|
|
18
|
+
if (!tab.url || !tab.url.startsWith("http://") && !tab.url.startsWith("https://")) {
|
|
19
|
+
return { removed: 0 };
|
|
20
|
+
}
|
|
21
|
+
if (!chrome.scripting?.executeScript) return { removed: 0 };
|
|
22
|
+
try {
|
|
23
|
+
const [result] = await chrome.scripting.executeScript({
|
|
24
|
+
target: { tabId },
|
|
25
|
+
args: [`${FOREIGN_EXTENSION_URL_PREFIX}${chrome.runtime.id}/`],
|
|
26
|
+
func: (ownExtensionPrefix) => {
|
|
27
|
+
const extensionPrefix = "chrome-extension://";
|
|
28
|
+
const selectors = ["iframe", "frame", "embed", "object"];
|
|
29
|
+
const visitedRoots = /* @__PURE__ */ new Set();
|
|
30
|
+
const roots = [document];
|
|
31
|
+
let removed = 0;
|
|
32
|
+
while (roots.length > 0) {
|
|
33
|
+
const root = roots.pop();
|
|
34
|
+
if (!root || visitedRoots.has(root)) continue;
|
|
35
|
+
visitedRoots.add(root);
|
|
36
|
+
for (const selector of selectors) {
|
|
37
|
+
const nodes = root.querySelectorAll(selector);
|
|
38
|
+
for (const node of nodes) {
|
|
39
|
+
const src = node.getAttribute("src") || node.getAttribute("data") || "";
|
|
40
|
+
if (!src.startsWith(extensionPrefix) || src.startsWith(ownExtensionPrefix)) continue;
|
|
41
|
+
node.remove();
|
|
42
|
+
removed++;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
|
46
|
+
let current = walker.nextNode();
|
|
47
|
+
while (current) {
|
|
48
|
+
const element = current;
|
|
49
|
+
if (element.shadowRoot) roots.push(element.shadowRoot);
|
|
50
|
+
current = walker.nextNode();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { removed };
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return result?.result ?? { removed: 0 };
|
|
57
|
+
} catch {
|
|
58
|
+
return { removed: 0 };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function delay(ms) {
|
|
62
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
63
|
+
}
|
|
64
|
+
async function tryAttach(tabId) {
|
|
65
|
+
await chrome.debugger.attach({ tabId }, "1.3");
|
|
28
66
|
}
|
|
29
67
|
async function ensureAttached(tabId) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
try {
|
|
69
|
+
const tab = await chrome.tabs.get(tabId);
|
|
70
|
+
if (!isDebuggableUrl$1(tab.url)) {
|
|
71
|
+
attached.delete(tabId);
|
|
72
|
+
throw new Error(`Cannot debug tab ${tabId}: URL is ${tab.url ?? "unknown"}`);
|
|
73
|
+
}
|
|
74
|
+
} catch (e) {
|
|
75
|
+
if (e instanceof Error && e.message.startsWith("Cannot debug tab")) throw e;
|
|
76
|
+
attached.delete(tabId);
|
|
77
|
+
throw new Error(`Tab ${tabId} no longer exists`);
|
|
78
|
+
}
|
|
79
|
+
if (attached.has(tabId)) {
|
|
80
|
+
try {
|
|
81
|
+
await chrome.debugger.sendCommand({ tabId }, "Runtime.evaluate", {
|
|
82
|
+
expression: "1",
|
|
83
|
+
returnByValue: true
|
|
84
|
+
});
|
|
85
|
+
return;
|
|
86
|
+
} catch {
|
|
87
|
+
attached.delete(tabId);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
await tryAttach(tabId);
|
|
92
|
+
} catch (e) {
|
|
93
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
94
|
+
const hint = msg.includes("chrome-extension://") ? ". Tip: another Chrome extension may be interfering — try disabling other extensions" : "";
|
|
95
|
+
if (msg.includes("chrome-extension://")) {
|
|
96
|
+
const recoveryCleanup = await removeForeignExtensionEmbeds(tabId);
|
|
97
|
+
if (recoveryCleanup.removed > 0) {
|
|
98
|
+
console.warn(`[opencli] Removed ${recoveryCleanup.removed} foreign extension frame(s) after attach failure on tab ${tabId}`);
|
|
99
|
+
}
|
|
100
|
+
await delay(ATTACH_RECOVERY_DELAY_MS);
|
|
101
|
+
try {
|
|
102
|
+
await tryAttach(tabId);
|
|
103
|
+
} catch {
|
|
104
|
+
throw new Error(`attach failed: ${msg}${hint}`);
|
|
105
|
+
}
|
|
106
|
+
} else if (msg.includes("Another debugger is already attached")) {
|
|
107
|
+
try {
|
|
108
|
+
await chrome.debugger.detach({ tabId });
|
|
109
|
+
} catch {
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
await tryAttach(tabId);
|
|
113
|
+
} catch {
|
|
114
|
+
throw new Error(`attach failed: ${msg}${hint}`);
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
throw new Error(`attach failed: ${msg}${hint}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
attached.add(tabId);
|
|
121
|
+
try {
|
|
122
|
+
await chrome.debugger.sendCommand({ tabId }, "Runtime.enable");
|
|
123
|
+
} catch {
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
await chrome.debugger.sendCommand({ tabId }, "Debugger.enable");
|
|
127
|
+
await chrome.debugger.sendCommand({ tabId }, "Debugger.setBreakpointsActive", { active: false });
|
|
128
|
+
} catch {
|
|
129
|
+
}
|
|
70
130
|
}
|
|
71
131
|
async function evaluate(tabId, expression) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Capture a screenshot via CDP Page.captureScreenshot.
|
|
87
|
-
* Returns base64-encoded image data.
|
|
88
|
-
*/
|
|
132
|
+
await ensureAttached(tabId);
|
|
133
|
+
const result = await chrome.debugger.sendCommand({ tabId }, "Runtime.evaluate", {
|
|
134
|
+
expression,
|
|
135
|
+
returnByValue: true,
|
|
136
|
+
awaitPromise: true
|
|
137
|
+
});
|
|
138
|
+
if (result.exceptionDetails) {
|
|
139
|
+
const errMsg = result.exceptionDetails.exception?.description || result.exceptionDetails.text || "Eval error";
|
|
140
|
+
throw new Error(errMsg);
|
|
141
|
+
}
|
|
142
|
+
return result.result?.value;
|
|
143
|
+
}
|
|
144
|
+
const evaluateAsync = evaluate;
|
|
89
145
|
async function screenshot(tabId, options = {}) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
*/
|
|
146
|
+
await ensureAttached(tabId);
|
|
147
|
+
const format = options.format ?? "png";
|
|
148
|
+
if (options.fullPage) {
|
|
149
|
+
const metrics = await chrome.debugger.sendCommand({ tabId }, "Page.getLayoutMetrics");
|
|
150
|
+
const size = metrics.cssContentSize || metrics.contentSize;
|
|
151
|
+
if (size) {
|
|
152
|
+
await chrome.debugger.sendCommand({ tabId }, "Emulation.setDeviceMetricsOverride", {
|
|
153
|
+
mobile: false,
|
|
154
|
+
width: Math.ceil(size.width),
|
|
155
|
+
height: Math.ceil(size.height),
|
|
156
|
+
deviceScaleFactor: 1
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
const params = { format };
|
|
162
|
+
if (format === "jpeg" && options.quality !== void 0) {
|
|
163
|
+
params.quality = Math.max(0, Math.min(100, options.quality));
|
|
164
|
+
}
|
|
165
|
+
const result = await chrome.debugger.sendCommand({ tabId }, "Page.captureScreenshot", params);
|
|
166
|
+
return result.data;
|
|
167
|
+
} finally {
|
|
168
|
+
if (options.fullPage) {
|
|
169
|
+
await chrome.debugger.sendCommand({ tabId }, "Emulation.clearDeviceMetricsOverride").catch(() => {
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
119
174
|
async function setFileInputFiles(tabId, files, selector) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
175
|
+
await ensureAttached(tabId);
|
|
176
|
+
await chrome.debugger.sendCommand({ tabId }, "DOM.enable");
|
|
177
|
+
const doc = await chrome.debugger.sendCommand({ tabId }, "DOM.getDocument");
|
|
178
|
+
const query = selector || 'input[type="file"]';
|
|
179
|
+
const result = await chrome.debugger.sendCommand({ tabId }, "DOM.querySelector", {
|
|
180
|
+
nodeId: doc.root.nodeId,
|
|
181
|
+
selector: query
|
|
182
|
+
});
|
|
183
|
+
if (!result.nodeId) {
|
|
184
|
+
throw new Error(`No element found matching selector: ${query}`);
|
|
185
|
+
}
|
|
186
|
+
await chrome.debugger.sendCommand({ tabId }, "DOM.setFileInputFiles", {
|
|
187
|
+
files,
|
|
188
|
+
nodeId: result.nodeId
|
|
189
|
+
});
|
|
133
190
|
}
|
|
134
191
|
async function detach(tabId) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
192
|
+
if (!attached.has(tabId)) return;
|
|
193
|
+
attached.delete(tabId);
|
|
194
|
+
try {
|
|
195
|
+
await chrome.debugger.detach({ tabId });
|
|
196
|
+
} catch {
|
|
197
|
+
}
|
|
140
198
|
}
|
|
141
199
|
function registerListeners() {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
200
|
+
chrome.tabs.onRemoved.addListener((tabId) => {
|
|
201
|
+
attached.delete(tabId);
|
|
202
|
+
});
|
|
203
|
+
chrome.debugger.onDetach.addListener((source) => {
|
|
204
|
+
if (source.tabId) attached.delete(source.tabId);
|
|
205
|
+
});
|
|
206
|
+
chrome.tabs.onUpdated.addListener(async (tabId, info) => {
|
|
207
|
+
if (info.url && !isDebuggableUrl$1(info.url)) {
|
|
208
|
+
await detach(tabId);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
let ws = null;
|
|
214
|
+
let reconnectTimer = null;
|
|
215
|
+
let reconnectAttempts = 0;
|
|
216
|
+
const _origLog = console.log.bind(console);
|
|
217
|
+
const _origWarn = console.warn.bind(console);
|
|
218
|
+
const _origError = console.error.bind(console);
|
|
160
219
|
function forwardLog(level, args) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
msg,
|
|
168
|
-
ts: Date.now()
|
|
169
|
-
}));
|
|
170
|
-
} catch {}
|
|
220
|
+
if (!ws || ws.readyState !== WebSocket.OPEN) return;
|
|
221
|
+
try {
|
|
222
|
+
const msg = args.map((a) => typeof a === "string" ? a : JSON.stringify(a)).join(" ");
|
|
223
|
+
ws.send(JSON.stringify({ type: "log", level, msg, ts: Date.now() }));
|
|
224
|
+
} catch {
|
|
225
|
+
}
|
|
171
226
|
}
|
|
172
227
|
console.log = (...args) => {
|
|
173
|
-
|
|
174
|
-
|
|
228
|
+
_origLog(...args);
|
|
229
|
+
forwardLog("info", args);
|
|
175
230
|
};
|
|
176
231
|
console.warn = (...args) => {
|
|
177
|
-
|
|
178
|
-
|
|
232
|
+
_origWarn(...args);
|
|
233
|
+
forwardLog("warn", args);
|
|
179
234
|
};
|
|
180
235
|
console.error = (...args) => {
|
|
181
|
-
|
|
182
|
-
|
|
236
|
+
_origError(...args);
|
|
237
|
+
forwardLog("error", args);
|
|
183
238
|
};
|
|
184
|
-
/**
|
|
185
|
-
* Probe the daemon via its /ping HTTP endpoint before attempting a WebSocket
|
|
186
|
-
* connection. fetch() failures are silently catchable; new WebSocket() is not
|
|
187
|
-
* — Chrome logs ERR_CONNECTION_REFUSED to the extension error page before any
|
|
188
|
-
* JS handler can intercept it. By keeping the probe inside connect() every
|
|
189
|
-
* call site remains unchanged and the guard can never be accidentally skipped.
|
|
190
|
-
*/
|
|
191
239
|
async function connect() {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* After MAX_EAGER_ATTEMPTS (reaching 60s backoff), stop scheduling reconnects.
|
|
235
|
-
* The keepalive alarm (~24s) will still call connect() periodically, but at a
|
|
236
|
-
* much lower frequency — reducing console noise when the daemon is not running.
|
|
237
|
-
*/
|
|
238
|
-
var MAX_EAGER_ATTEMPTS = 6;
|
|
240
|
+
if (ws?.readyState === WebSocket.OPEN || ws?.readyState === WebSocket.CONNECTING) return;
|
|
241
|
+
try {
|
|
242
|
+
const res = await fetch(DAEMON_PING_URL, { signal: AbortSignal.timeout(1e3) });
|
|
243
|
+
if (!res.ok) return;
|
|
244
|
+
} catch {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
try {
|
|
248
|
+
ws = new WebSocket(DAEMON_WS_URL);
|
|
249
|
+
} catch {
|
|
250
|
+
scheduleReconnect();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
ws.onopen = () => {
|
|
254
|
+
console.log("[opencli] Connected to daemon");
|
|
255
|
+
reconnectAttempts = 0;
|
|
256
|
+
if (reconnectTimer) {
|
|
257
|
+
clearTimeout(reconnectTimer);
|
|
258
|
+
reconnectTimer = null;
|
|
259
|
+
}
|
|
260
|
+
ws?.send(JSON.stringify({ type: "hello", version: chrome.runtime.getManifest().version }));
|
|
261
|
+
};
|
|
262
|
+
ws.onmessage = async (event) => {
|
|
263
|
+
try {
|
|
264
|
+
const command = JSON.parse(event.data);
|
|
265
|
+
const result = await handleCommand(command);
|
|
266
|
+
ws?.send(JSON.stringify(result));
|
|
267
|
+
} catch (err) {
|
|
268
|
+
console.error("[opencli] Message handling error:", err);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
ws.onclose = () => {
|
|
272
|
+
console.log("[opencli] Disconnected from daemon");
|
|
273
|
+
ws = null;
|
|
274
|
+
scheduleReconnect();
|
|
275
|
+
};
|
|
276
|
+
ws.onerror = () => {
|
|
277
|
+
ws?.close();
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
const MAX_EAGER_ATTEMPTS = 6;
|
|
239
281
|
function scheduleReconnect() {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
|
|
282
|
+
if (reconnectTimer) return;
|
|
283
|
+
reconnectAttempts++;
|
|
284
|
+
if (reconnectAttempts > MAX_EAGER_ATTEMPTS) return;
|
|
285
|
+
const delay = Math.min(WS_RECONNECT_BASE_DELAY * Math.pow(2, reconnectAttempts - 1), WS_RECONNECT_MAX_DELAY);
|
|
286
|
+
reconnectTimer = setTimeout(() => {
|
|
287
|
+
reconnectTimer = null;
|
|
288
|
+
void connect();
|
|
289
|
+
}, delay);
|
|
290
|
+
}
|
|
291
|
+
const automationSessions = /* @__PURE__ */ new Map();
|
|
292
|
+
const WINDOW_IDLE_TIMEOUT = 3e4;
|
|
251
293
|
function getWorkspaceKey(workspace) {
|
|
252
|
-
|
|
294
|
+
return workspace?.trim() || "default";
|
|
253
295
|
}
|
|
254
296
|
function resetWindowIdleTimer(workspace) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
} catch {}
|
|
271
|
-
automationSessions.delete(workspace);
|
|
272
|
-
}, WINDOW_IDLE_TIMEOUT);
|
|
273
|
-
}
|
|
274
|
-
/** Get or create the dedicated automation window. */
|
|
297
|
+
const session = automationSessions.get(workspace);
|
|
298
|
+
if (!session) return;
|
|
299
|
+
if (session.idleTimer) clearTimeout(session.idleTimer);
|
|
300
|
+
session.idleDeadlineAt = Date.now() + WINDOW_IDLE_TIMEOUT;
|
|
301
|
+
session.idleTimer = setTimeout(async () => {
|
|
302
|
+
const current = automationSessions.get(workspace);
|
|
303
|
+
if (!current) return;
|
|
304
|
+
try {
|
|
305
|
+
await chrome.windows.remove(current.windowId);
|
|
306
|
+
console.log(`[opencli] Automation window ${current.windowId} (${workspace}) closed (idle timeout)`);
|
|
307
|
+
} catch {
|
|
308
|
+
}
|
|
309
|
+
automationSessions.delete(workspace);
|
|
310
|
+
}, WINDOW_IDLE_TIMEOUT);
|
|
311
|
+
}
|
|
275
312
|
async function getAutomationWindow(workspace) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
313
|
+
const existing = automationSessions.get(workspace);
|
|
314
|
+
if (existing) {
|
|
315
|
+
try {
|
|
316
|
+
await chrome.windows.get(existing.windowId);
|
|
317
|
+
return existing.windowId;
|
|
318
|
+
} catch {
|
|
319
|
+
automationSessions.delete(workspace);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const win = await chrome.windows.create({
|
|
323
|
+
url: BLANK_PAGE,
|
|
324
|
+
focused: false,
|
|
325
|
+
width: 1280,
|
|
326
|
+
height: 900,
|
|
327
|
+
type: "normal"
|
|
328
|
+
});
|
|
329
|
+
const session = {
|
|
330
|
+
windowId: win.id,
|
|
331
|
+
idleTimer: null,
|
|
332
|
+
idleDeadlineAt: Date.now() + WINDOW_IDLE_TIMEOUT
|
|
333
|
+
};
|
|
334
|
+
automationSessions.set(workspace, session);
|
|
335
|
+
console.log(`[opencli] Created automation window ${session.windowId} (${workspace})`);
|
|
336
|
+
resetWindowIdleTimer(workspace);
|
|
337
|
+
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
338
|
+
return session.windowId;
|
|
301
339
|
}
|
|
302
340
|
chrome.windows.onRemoved.addListener((windowId) => {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
341
|
+
for (const [workspace, session] of automationSessions.entries()) {
|
|
342
|
+
if (session.windowId === windowId) {
|
|
343
|
+
console.log(`[opencli] Automation window closed (${workspace})`);
|
|
344
|
+
if (session.idleTimer) clearTimeout(session.idleTimer);
|
|
345
|
+
automationSessions.delete(workspace);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
308
348
|
});
|
|
309
|
-
|
|
349
|
+
let initialized = false;
|
|
310
350
|
function initialize() {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
351
|
+
if (initialized) return;
|
|
352
|
+
initialized = true;
|
|
353
|
+
chrome.alarms.create("keepalive", { periodInMinutes: 0.4 });
|
|
354
|
+
registerListeners();
|
|
355
|
+
void connect();
|
|
356
|
+
console.log("[opencli] OpenCLI extension initialized");
|
|
317
357
|
}
|
|
318
358
|
chrome.runtime.onInstalled.addListener(() => {
|
|
319
|
-
|
|
359
|
+
initialize();
|
|
320
360
|
});
|
|
321
361
|
chrome.runtime.onStartup.addListener(() => {
|
|
322
|
-
|
|
362
|
+
initialize();
|
|
323
363
|
});
|
|
324
364
|
chrome.alarms.onAlarm.addListener((alarm) => {
|
|
325
|
-
|
|
365
|
+
if (alarm.name === "keepalive") void connect();
|
|
326
366
|
});
|
|
327
367
|
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
368
|
+
if (msg?.type === "getStatus") {
|
|
369
|
+
sendResponse({
|
|
370
|
+
connected: ws?.readyState === WebSocket.OPEN,
|
|
371
|
+
reconnecting: reconnectTimer !== null
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
return false;
|
|
333
375
|
});
|
|
334
376
|
async function handleCommand(cmd) {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
377
|
+
const workspace = getWorkspaceKey(cmd.workspace);
|
|
378
|
+
resetWindowIdleTimer(workspace);
|
|
379
|
+
try {
|
|
380
|
+
switch (cmd.action) {
|
|
381
|
+
case "exec":
|
|
382
|
+
return await handleExec(cmd, workspace);
|
|
383
|
+
case "navigate":
|
|
384
|
+
return await handleNavigate(cmd, workspace);
|
|
385
|
+
case "tabs":
|
|
386
|
+
return await handleTabs(cmd, workspace);
|
|
387
|
+
case "cookies":
|
|
388
|
+
return await handleCookies(cmd);
|
|
389
|
+
case "screenshot":
|
|
390
|
+
return await handleScreenshot(cmd, workspace);
|
|
391
|
+
case "close-window":
|
|
392
|
+
return await handleCloseWindow(cmd, workspace);
|
|
393
|
+
case "sessions":
|
|
394
|
+
return await handleSessions(cmd);
|
|
395
|
+
case "set-file-input":
|
|
396
|
+
return await handleSetFileInput(cmd, workspace);
|
|
397
|
+
default:
|
|
398
|
+
return { id: cmd.id, ok: false, error: `Unknown action: ${cmd.action}` };
|
|
399
|
+
}
|
|
400
|
+
} catch (err) {
|
|
401
|
+
return {
|
|
402
|
+
id: cmd.id,
|
|
403
|
+
ok: false,
|
|
404
|
+
error: err instanceof Error ? err.message : String(err)
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
const BLANK_PAGE = "data:text/html,<html></html>";
|
|
365
409
|
function isDebuggableUrl(url) {
|
|
366
|
-
|
|
367
|
-
|
|
410
|
+
if (!url) return true;
|
|
411
|
+
return url.startsWith("http://") || url.startsWith("https://") || url === BLANK_PAGE;
|
|
368
412
|
}
|
|
369
|
-
/** Check if a URL is safe for user-facing navigation (http/https only). */
|
|
370
413
|
function isSafeNavigationUrl(url) {
|
|
371
|
-
|
|
414
|
+
return url.startsWith("http://") || url.startsWith("https://");
|
|
372
415
|
}
|
|
373
|
-
/** Minimal URL normalization for same-page comparison: root slash + default port only. */
|
|
374
416
|
function normalizeUrlForComparison(url) {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
417
|
+
if (!url) return "";
|
|
418
|
+
try {
|
|
419
|
+
const parsed = new URL(url);
|
|
420
|
+
if (parsed.protocol === "https:" && parsed.port === "443" || parsed.protocol === "http:" && parsed.port === "80") {
|
|
421
|
+
parsed.port = "";
|
|
422
|
+
}
|
|
423
|
+
const pathname = parsed.pathname === "/" ? "" : parsed.pathname;
|
|
424
|
+
return `${parsed.protocol}//${parsed.host}${pathname}${parsed.search}${parsed.hash}`;
|
|
425
|
+
} catch {
|
|
426
|
+
return url;
|
|
427
|
+
}
|
|
384
428
|
}
|
|
385
429
|
function isTargetUrl(currentUrl, targetUrl) {
|
|
386
|
-
|
|
387
|
-
}
|
|
388
|
-
function matchesDomain(url, domain) {
|
|
389
|
-
if (!url) return false;
|
|
390
|
-
try {
|
|
391
|
-
const parsed = new URL(url);
|
|
392
|
-
return parsed.hostname === domain || parsed.hostname.endsWith(`.${domain}`);
|
|
393
|
-
} catch {
|
|
394
|
-
return false;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
function matchesBindCriteria(tab, cmd) {
|
|
398
|
-
if (!tab.id || !isDebuggableUrl(tab.url)) return false;
|
|
399
|
-
if (cmd.matchDomain && !matchesDomain(tab.url, cmd.matchDomain)) return false;
|
|
400
|
-
if (cmd.matchPathPrefix) try {
|
|
401
|
-
if (!new URL(tab.url).pathname.startsWith(cmd.matchPathPrefix)) return false;
|
|
402
|
-
} catch {
|
|
403
|
-
return false;
|
|
404
|
-
}
|
|
405
|
-
return true;
|
|
406
|
-
}
|
|
407
|
-
function isNotebooklmWorkspace(workspace) {
|
|
408
|
-
return workspace === "site:notebooklm";
|
|
409
|
-
}
|
|
410
|
-
function classifyNotebooklmUrl(url) {
|
|
411
|
-
if (!url) return "other";
|
|
412
|
-
try {
|
|
413
|
-
const parsed = new URL(url);
|
|
414
|
-
if (parsed.hostname !== "notebooklm.google.com") return "other";
|
|
415
|
-
return parsed.pathname.startsWith("/notebook/") ? "notebook" : "home";
|
|
416
|
-
} catch {
|
|
417
|
-
return "other";
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
function scoreWorkspaceTab(workspace, tab) {
|
|
421
|
-
if (!tab.id || !isDebuggableUrl(tab.url)) return -1;
|
|
422
|
-
if (isNotebooklmWorkspace(workspace)) {
|
|
423
|
-
const kind = classifyNotebooklmUrl(tab.url);
|
|
424
|
-
if (kind === "other") return -1;
|
|
425
|
-
if (kind === "notebook") return tab.active ? 400 : 300;
|
|
426
|
-
return tab.active ? 200 : 100;
|
|
427
|
-
}
|
|
428
|
-
return -1;
|
|
429
|
-
}
|
|
430
|
-
function setWorkspaceSession(workspace, session) {
|
|
431
|
-
const existing = automationSessions.get(workspace);
|
|
432
|
-
if (existing?.idleTimer) clearTimeout(existing.idleTimer);
|
|
433
|
-
automationSessions.set(workspace, {
|
|
434
|
-
...session,
|
|
435
|
-
idleTimer: null,
|
|
436
|
-
idleDeadlineAt: Date.now() + WINDOW_IDLE_TIMEOUT
|
|
437
|
-
});
|
|
438
|
-
}
|
|
439
|
-
async function maybeBindWorkspaceToExistingTab(workspace) {
|
|
440
|
-
if (!isNotebooklmWorkspace(workspace)) return null;
|
|
441
|
-
const tabs = await chrome.tabs.query({});
|
|
442
|
-
let bestTab = null;
|
|
443
|
-
let bestScore = -1;
|
|
444
|
-
for (const tab of tabs) {
|
|
445
|
-
const score = scoreWorkspaceTab(workspace, tab);
|
|
446
|
-
if (score > bestScore) {
|
|
447
|
-
bestScore = score;
|
|
448
|
-
bestTab = tab;
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
if (!bestTab?.id || bestScore < 0) return null;
|
|
452
|
-
setWorkspaceSession(workspace, {
|
|
453
|
-
windowId: bestTab.windowId,
|
|
454
|
-
owned: false,
|
|
455
|
-
preferredTabId: bestTab.id
|
|
456
|
-
});
|
|
457
|
-
console.log(`[opencli] Workspace ${workspace} bound to existing tab ${bestTab.id} in window ${bestTab.windowId}`);
|
|
458
|
-
resetWindowIdleTimer(workspace);
|
|
459
|
-
return bestTab.id;
|
|
460
|
-
}
|
|
461
|
-
/**
|
|
462
|
-
* Resolve target tab in the automation window.
|
|
463
|
-
* If explicit tabId is given, use that directly.
|
|
464
|
-
* Otherwise, find or create a tab in the dedicated automation window.
|
|
465
|
-
*/
|
|
430
|
+
return normalizeUrlForComparison(currentUrl) === normalizeUrlForComparison(targetUrl);
|
|
431
|
+
}
|
|
466
432
|
async function resolveTabId(tabId, workspace) {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
const newTab = await chrome.tabs.create({
|
|
501
|
-
windowId,
|
|
502
|
-
url: BLANK_PAGE,
|
|
503
|
-
active: true
|
|
504
|
-
});
|
|
505
|
-
if (!newTab.id) throw new Error("Failed to create tab in automation window");
|
|
506
|
-
return newTab.id;
|
|
433
|
+
if (tabId !== void 0) {
|
|
434
|
+
try {
|
|
435
|
+
const tab = await chrome.tabs.get(tabId);
|
|
436
|
+
const session = automationSessions.get(workspace);
|
|
437
|
+
const matchesSession = session ? tab.windowId === session.windowId : false;
|
|
438
|
+
if (isDebuggableUrl(tab.url) && matchesSession) return tabId;
|
|
439
|
+
if (session && !matchesSession) {
|
|
440
|
+
console.warn(`[opencli] Tab ${tabId} is not bound to workspace ${workspace}, re-resolving`);
|
|
441
|
+
} else if (!isDebuggableUrl(tab.url)) {
|
|
442
|
+
console.warn(`[opencli] Tab ${tabId} URL is not debuggable (${tab.url}), re-resolving`);
|
|
443
|
+
}
|
|
444
|
+
} catch {
|
|
445
|
+
console.warn(`[opencli] Tab ${tabId} no longer exists, re-resolving`);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
const windowId = await getAutomationWindow(workspace);
|
|
449
|
+
const tabs = await chrome.tabs.query({ windowId });
|
|
450
|
+
const debuggableTab = tabs.find((t) => t.id && isDebuggableUrl(t.url));
|
|
451
|
+
if (debuggableTab?.id) return debuggableTab.id;
|
|
452
|
+
const reuseTab = tabs.find((t) => t.id);
|
|
453
|
+
if (reuseTab?.id) {
|
|
454
|
+
await chrome.tabs.update(reuseTab.id, { url: BLANK_PAGE });
|
|
455
|
+
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
456
|
+
try {
|
|
457
|
+
const updated = await chrome.tabs.get(reuseTab.id);
|
|
458
|
+
if (isDebuggableUrl(updated.url)) return reuseTab.id;
|
|
459
|
+
console.warn(`[opencli] data: URI was intercepted (${updated.url}), creating fresh tab`);
|
|
460
|
+
} catch {
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const newTab = await chrome.tabs.create({ windowId, url: BLANK_PAGE, active: true });
|
|
464
|
+
if (!newTab.id) throw new Error("Failed to create tab in automation window");
|
|
465
|
+
return newTab.id;
|
|
507
466
|
}
|
|
508
467
|
async function listAutomationTabs(workspace) {
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
try {
|
|
518
|
-
return await chrome.tabs.query({ windowId: session.windowId });
|
|
519
|
-
} catch {
|
|
520
|
-
automationSessions.delete(workspace);
|
|
521
|
-
return [];
|
|
522
|
-
}
|
|
468
|
+
const session = automationSessions.get(workspace);
|
|
469
|
+
if (!session) return [];
|
|
470
|
+
try {
|
|
471
|
+
return await chrome.tabs.query({ windowId: session.windowId });
|
|
472
|
+
} catch {
|
|
473
|
+
automationSessions.delete(workspace);
|
|
474
|
+
return [];
|
|
475
|
+
}
|
|
523
476
|
}
|
|
524
477
|
async function listAutomationWebTabs(workspace) {
|
|
525
|
-
|
|
478
|
+
const tabs = await listAutomationTabs(workspace);
|
|
479
|
+
return tabs.filter((tab) => isDebuggableUrl(tab.url));
|
|
526
480
|
}
|
|
527
481
|
async function handleExec(cmd, workspace) {
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
return {
|
|
537
|
-
id: cmd.id,
|
|
538
|
-
ok: true,
|
|
539
|
-
data
|
|
540
|
-
};
|
|
541
|
-
} catch (err) {
|
|
542
|
-
return {
|
|
543
|
-
id: cmd.id,
|
|
544
|
-
ok: false,
|
|
545
|
-
error: err instanceof Error ? err.message : String(err)
|
|
546
|
-
};
|
|
547
|
-
}
|
|
482
|
+
if (!cmd.code) return { id: cmd.id, ok: false, error: "Missing code" };
|
|
483
|
+
const tabId = await resolveTabId(cmd.tabId, workspace);
|
|
484
|
+
try {
|
|
485
|
+
const data = await evaluateAsync(tabId, cmd.code);
|
|
486
|
+
return { id: cmd.id, ok: true, data };
|
|
487
|
+
} catch (err) {
|
|
488
|
+
return { id: cmd.id, ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
489
|
+
}
|
|
548
490
|
}
|
|
549
491
|
async function handleNavigate(cmd, workspace) {
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
id: cmd.id,
|
|
612
|
-
ok: true,
|
|
613
|
-
data: {
|
|
614
|
-
title: tab.title,
|
|
615
|
-
url: tab.url,
|
|
616
|
-
tabId,
|
|
617
|
-
timedOut
|
|
618
|
-
}
|
|
619
|
-
};
|
|
492
|
+
if (!cmd.url) return { id: cmd.id, ok: false, error: "Missing url" };
|
|
493
|
+
if (!isSafeNavigationUrl(cmd.url)) {
|
|
494
|
+
return { id: cmd.id, ok: false, error: "Blocked URL scheme -- only http:// and https:// are allowed" };
|
|
495
|
+
}
|
|
496
|
+
const tabId = await resolveTabId(cmd.tabId, workspace);
|
|
497
|
+
const beforeTab = await chrome.tabs.get(tabId);
|
|
498
|
+
const beforeNormalized = normalizeUrlForComparison(beforeTab.url);
|
|
499
|
+
const targetUrl = cmd.url;
|
|
500
|
+
if (beforeTab.status === "complete" && isTargetUrl(beforeTab.url, targetUrl)) {
|
|
501
|
+
return {
|
|
502
|
+
id: cmd.id,
|
|
503
|
+
ok: true,
|
|
504
|
+
data: { title: beforeTab.title, url: beforeTab.url, tabId, timedOut: false }
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
await detach(tabId);
|
|
508
|
+
await chrome.tabs.update(tabId, { url: targetUrl });
|
|
509
|
+
let timedOut = false;
|
|
510
|
+
await new Promise((resolve) => {
|
|
511
|
+
let settled = false;
|
|
512
|
+
let checkTimer = null;
|
|
513
|
+
let timeoutTimer = null;
|
|
514
|
+
const finish = () => {
|
|
515
|
+
if (settled) return;
|
|
516
|
+
settled = true;
|
|
517
|
+
chrome.tabs.onUpdated.removeListener(listener);
|
|
518
|
+
if (checkTimer) clearTimeout(checkTimer);
|
|
519
|
+
if (timeoutTimer) clearTimeout(timeoutTimer);
|
|
520
|
+
resolve();
|
|
521
|
+
};
|
|
522
|
+
const isNavigationDone = (url) => {
|
|
523
|
+
return isTargetUrl(url, targetUrl) || normalizeUrlForComparison(url) !== beforeNormalized;
|
|
524
|
+
};
|
|
525
|
+
const listener = (id, info, tab2) => {
|
|
526
|
+
if (id !== tabId) return;
|
|
527
|
+
if (info.status === "complete" && isNavigationDone(tab2.url ?? info.url)) {
|
|
528
|
+
finish();
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
chrome.tabs.onUpdated.addListener(listener);
|
|
532
|
+
checkTimer = setTimeout(async () => {
|
|
533
|
+
try {
|
|
534
|
+
const currentTab = await chrome.tabs.get(tabId);
|
|
535
|
+
if (currentTab.status === "complete" && isNavigationDone(currentTab.url)) {
|
|
536
|
+
finish();
|
|
537
|
+
}
|
|
538
|
+
} catch {
|
|
539
|
+
}
|
|
540
|
+
}, 100);
|
|
541
|
+
timeoutTimer = setTimeout(() => {
|
|
542
|
+
timedOut = true;
|
|
543
|
+
console.warn(`[opencli] Navigate to ${targetUrl} timed out after 15s`);
|
|
544
|
+
finish();
|
|
545
|
+
}, 15e3);
|
|
546
|
+
});
|
|
547
|
+
const tab = await chrome.tabs.get(tabId);
|
|
548
|
+
return {
|
|
549
|
+
id: cmd.id,
|
|
550
|
+
ok: true,
|
|
551
|
+
data: { title: tab.title, url: tab.url, tabId, timedOut }
|
|
552
|
+
};
|
|
620
553
|
}
|
|
621
554
|
async function handleTabs(cmd, workspace) {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
}
|
|
683
|
-
case "select": {
|
|
684
|
-
if (cmd.index === void 0 && cmd.tabId === void 0) return {
|
|
685
|
-
id: cmd.id,
|
|
686
|
-
ok: false,
|
|
687
|
-
error: "Missing index or tabId"
|
|
688
|
-
};
|
|
689
|
-
if (cmd.tabId !== void 0) {
|
|
690
|
-
const session = automationSessions.get(workspace);
|
|
691
|
-
let tab;
|
|
692
|
-
try {
|
|
693
|
-
tab = await chrome.tabs.get(cmd.tabId);
|
|
694
|
-
} catch {
|
|
695
|
-
return {
|
|
696
|
-
id: cmd.id,
|
|
697
|
-
ok: false,
|
|
698
|
-
error: `Tab ${cmd.tabId} no longer exists`
|
|
699
|
-
};
|
|
700
|
-
}
|
|
701
|
-
if (!session || tab.windowId !== session.windowId) return {
|
|
702
|
-
id: cmd.id,
|
|
703
|
-
ok: false,
|
|
704
|
-
error: `Tab ${cmd.tabId} is not in the automation window`
|
|
705
|
-
};
|
|
706
|
-
await chrome.tabs.update(cmd.tabId, { active: true });
|
|
707
|
-
return {
|
|
708
|
-
id: cmd.id,
|
|
709
|
-
ok: true,
|
|
710
|
-
data: { selected: cmd.tabId }
|
|
711
|
-
};
|
|
712
|
-
}
|
|
713
|
-
const target = (await listAutomationWebTabs(workspace))[cmd.index];
|
|
714
|
-
if (!target?.id) return {
|
|
715
|
-
id: cmd.id,
|
|
716
|
-
ok: false,
|
|
717
|
-
error: `Tab index ${cmd.index} not found`
|
|
718
|
-
};
|
|
719
|
-
await chrome.tabs.update(target.id, { active: true });
|
|
720
|
-
return {
|
|
721
|
-
id: cmd.id,
|
|
722
|
-
ok: true,
|
|
723
|
-
data: { selected: target.id }
|
|
724
|
-
};
|
|
725
|
-
}
|
|
726
|
-
default: return {
|
|
727
|
-
id: cmd.id,
|
|
728
|
-
ok: false,
|
|
729
|
-
error: `Unknown tabs op: ${cmd.op}`
|
|
730
|
-
};
|
|
731
|
-
}
|
|
555
|
+
switch (cmd.op) {
|
|
556
|
+
case "list": {
|
|
557
|
+
const tabs = await listAutomationWebTabs(workspace);
|
|
558
|
+
const data = tabs.map((t, i) => ({
|
|
559
|
+
index: i,
|
|
560
|
+
tabId: t.id,
|
|
561
|
+
url: t.url,
|
|
562
|
+
title: t.title,
|
|
563
|
+
active: t.active
|
|
564
|
+
}));
|
|
565
|
+
return { id: cmd.id, ok: true, data };
|
|
566
|
+
}
|
|
567
|
+
case "new": {
|
|
568
|
+
if (cmd.url && !isSafeNavigationUrl(cmd.url)) {
|
|
569
|
+
return { id: cmd.id, ok: false, error: "Blocked URL scheme -- only http:// and https:// are allowed" };
|
|
570
|
+
}
|
|
571
|
+
const windowId = await getAutomationWindow(workspace);
|
|
572
|
+
const tab = await chrome.tabs.create({ windowId, url: cmd.url ?? BLANK_PAGE, active: true });
|
|
573
|
+
return { id: cmd.id, ok: true, data: { tabId: tab.id, url: tab.url } };
|
|
574
|
+
}
|
|
575
|
+
case "close": {
|
|
576
|
+
if (cmd.index !== void 0) {
|
|
577
|
+
const tabs = await listAutomationWebTabs(workspace);
|
|
578
|
+
const target = tabs[cmd.index];
|
|
579
|
+
if (!target?.id) return { id: cmd.id, ok: false, error: `Tab index ${cmd.index} not found` };
|
|
580
|
+
await chrome.tabs.remove(target.id);
|
|
581
|
+
await detach(target.id);
|
|
582
|
+
return { id: cmd.id, ok: true, data: { closed: target.id } };
|
|
583
|
+
}
|
|
584
|
+
const tabId = await resolveTabId(cmd.tabId, workspace);
|
|
585
|
+
await chrome.tabs.remove(tabId);
|
|
586
|
+
await detach(tabId);
|
|
587
|
+
return { id: cmd.id, ok: true, data: { closed: tabId } };
|
|
588
|
+
}
|
|
589
|
+
case "select": {
|
|
590
|
+
if (cmd.index === void 0 && cmd.tabId === void 0)
|
|
591
|
+
return { id: cmd.id, ok: false, error: "Missing index or tabId" };
|
|
592
|
+
if (cmd.tabId !== void 0) {
|
|
593
|
+
const session = automationSessions.get(workspace);
|
|
594
|
+
let tab;
|
|
595
|
+
try {
|
|
596
|
+
tab = await chrome.tabs.get(cmd.tabId);
|
|
597
|
+
} catch {
|
|
598
|
+
return { id: cmd.id, ok: false, error: `Tab ${cmd.tabId} no longer exists` };
|
|
599
|
+
}
|
|
600
|
+
if (!session || tab.windowId !== session.windowId) {
|
|
601
|
+
return { id: cmd.id, ok: false, error: `Tab ${cmd.tabId} is not in the automation window` };
|
|
602
|
+
}
|
|
603
|
+
await chrome.tabs.update(cmd.tabId, { active: true });
|
|
604
|
+
return { id: cmd.id, ok: true, data: { selected: cmd.tabId } };
|
|
605
|
+
}
|
|
606
|
+
const tabs = await listAutomationWebTabs(workspace);
|
|
607
|
+
const target = tabs[cmd.index];
|
|
608
|
+
if (!target?.id) return { id: cmd.id, ok: false, error: `Tab index ${cmd.index} not found` };
|
|
609
|
+
await chrome.tabs.update(target.id, { active: true });
|
|
610
|
+
return { id: cmd.id, ok: true, data: { selected: target.id } };
|
|
611
|
+
}
|
|
612
|
+
default:
|
|
613
|
+
return { id: cmd.id, ok: false, error: `Unknown tabs op: ${cmd.op}` };
|
|
614
|
+
}
|
|
732
615
|
}
|
|
733
616
|
async function handleCookies(cmd) {
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
return {
|
|
752
|
-
id: cmd.id,
|
|
753
|
-
ok: true,
|
|
754
|
-
data
|
|
755
|
-
};
|
|
617
|
+
if (!cmd.domain && !cmd.url) {
|
|
618
|
+
return { id: cmd.id, ok: false, error: "Cookie scope required: provide domain or url to avoid dumping all cookies" };
|
|
619
|
+
}
|
|
620
|
+
const details = {};
|
|
621
|
+
if (cmd.domain) details.domain = cmd.domain;
|
|
622
|
+
if (cmd.url) details.url = cmd.url;
|
|
623
|
+
const cookies = await chrome.cookies.getAll(details);
|
|
624
|
+
const data = cookies.map((c) => ({
|
|
625
|
+
name: c.name,
|
|
626
|
+
value: c.value,
|
|
627
|
+
domain: c.domain,
|
|
628
|
+
path: c.path,
|
|
629
|
+
secure: c.secure,
|
|
630
|
+
httpOnly: c.httpOnly,
|
|
631
|
+
expirationDate: c.expirationDate
|
|
632
|
+
}));
|
|
633
|
+
return { id: cmd.id, ok: true, data };
|
|
756
634
|
}
|
|
757
635
|
async function handleScreenshot(cmd, workspace) {
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
};
|
|
770
|
-
} catch (err) {
|
|
771
|
-
return {
|
|
772
|
-
id: cmd.id,
|
|
773
|
-
ok: false,
|
|
774
|
-
error: err instanceof Error ? err.message : String(err)
|
|
775
|
-
};
|
|
776
|
-
}
|
|
636
|
+
const tabId = await resolveTabId(cmd.tabId, workspace);
|
|
637
|
+
try {
|
|
638
|
+
const data = await screenshot(tabId, {
|
|
639
|
+
format: cmd.format,
|
|
640
|
+
quality: cmd.quality,
|
|
641
|
+
fullPage: cmd.fullPage
|
|
642
|
+
});
|
|
643
|
+
return { id: cmd.id, ok: true, data };
|
|
644
|
+
} catch (err) {
|
|
645
|
+
return { id: cmd.id, ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
646
|
+
}
|
|
777
647
|
}
|
|
778
648
|
async function handleCloseWindow(cmd, workspace) {
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
ok: true,
|
|
790
|
-
data: { closed: true }
|
|
791
|
-
};
|
|
649
|
+
const session = automationSessions.get(workspace);
|
|
650
|
+
if (session) {
|
|
651
|
+
try {
|
|
652
|
+
await chrome.windows.remove(session.windowId);
|
|
653
|
+
} catch {
|
|
654
|
+
}
|
|
655
|
+
if (session.idleTimer) clearTimeout(session.idleTimer);
|
|
656
|
+
automationSessions.delete(workspace);
|
|
657
|
+
}
|
|
658
|
+
return { id: cmd.id, ok: true, data: { closed: true } };
|
|
792
659
|
}
|
|
793
660
|
async function handleSetFileInput(cmd, workspace) {
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
ok: true,
|
|
805
|
-
data: { count: cmd.files.length }
|
|
806
|
-
};
|
|
807
|
-
} catch (err) {
|
|
808
|
-
return {
|
|
809
|
-
id: cmd.id,
|
|
810
|
-
ok: false,
|
|
811
|
-
error: err instanceof Error ? err.message : String(err)
|
|
812
|
-
};
|
|
813
|
-
}
|
|
661
|
+
if (!cmd.files || !Array.isArray(cmd.files) || cmd.files.length === 0) {
|
|
662
|
+
return { id: cmd.id, ok: false, error: "Missing or empty files array" };
|
|
663
|
+
}
|
|
664
|
+
const tabId = await resolveTabId(cmd.tabId, workspace);
|
|
665
|
+
try {
|
|
666
|
+
await setFileInputFiles(tabId, cmd.files, cmd.selector);
|
|
667
|
+
return { id: cmd.id, ok: true, data: { count: cmd.files.length } };
|
|
668
|
+
} catch (err) {
|
|
669
|
+
return { id: cmd.id, ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
670
|
+
}
|
|
814
671
|
}
|
|
815
672
|
async function handleSessions(cmd) {
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
ok: true,
|
|
826
|
-
data
|
|
827
|
-
};
|
|
828
|
-
}
|
|
829
|
-
async function handleBindCurrent(cmd, workspace) {
|
|
830
|
-
const activeTabs = await chrome.tabs.query({
|
|
831
|
-
active: true,
|
|
832
|
-
lastFocusedWindow: true
|
|
833
|
-
});
|
|
834
|
-
const fallbackTabs = await chrome.tabs.query({ lastFocusedWindow: true });
|
|
835
|
-
const allTabs = await chrome.tabs.query({});
|
|
836
|
-
const boundTab = activeTabs.find((tab) => matchesBindCriteria(tab, cmd)) ?? fallbackTabs.find((tab) => matchesBindCriteria(tab, cmd)) ?? allTabs.find((tab) => matchesBindCriteria(tab, cmd));
|
|
837
|
-
if (!boundTab?.id) return {
|
|
838
|
-
id: cmd.id,
|
|
839
|
-
ok: false,
|
|
840
|
-
error: cmd.matchDomain || cmd.matchPathPrefix ? `No visible tab matching ${cmd.matchDomain ?? "domain"}${cmd.matchPathPrefix ? ` ${cmd.matchPathPrefix}` : ""}` : "No active debuggable tab found"
|
|
841
|
-
};
|
|
842
|
-
setWorkspaceSession(workspace, {
|
|
843
|
-
windowId: boundTab.windowId,
|
|
844
|
-
owned: false,
|
|
845
|
-
preferredTabId: boundTab.id
|
|
846
|
-
});
|
|
847
|
-
resetWindowIdleTimer(workspace);
|
|
848
|
-
console.log(`[opencli] Workspace ${workspace} explicitly bound to tab ${boundTab.id} (${boundTab.url})`);
|
|
849
|
-
return {
|
|
850
|
-
id: cmd.id,
|
|
851
|
-
ok: true,
|
|
852
|
-
data: {
|
|
853
|
-
tabId: boundTab.id,
|
|
854
|
-
windowId: boundTab.windowId,
|
|
855
|
-
url: boundTab.url,
|
|
856
|
-
title: boundTab.title,
|
|
857
|
-
workspace
|
|
858
|
-
}
|
|
859
|
-
};
|
|
860
|
-
}
|
|
861
|
-
//#endregion
|
|
673
|
+
const now = Date.now();
|
|
674
|
+
const data = await Promise.all([...automationSessions.entries()].map(async ([workspace, session]) => ({
|
|
675
|
+
workspace,
|
|
676
|
+
windowId: session.windowId,
|
|
677
|
+
tabCount: (await chrome.tabs.query({ windowId: session.windowId })).filter((tab) => isDebuggableUrl(tab.url)).length,
|
|
678
|
+
idleMsRemaining: Math.max(0, session.idleDeadlineAt - now)
|
|
679
|
+
})));
|
|
680
|
+
return { id: cmd.id, ok: true, data };
|
|
681
|
+
}
|