cdp-toolkit 3.5.0 → 3.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/CHANGELOG.md +12 -0
- package/CONTRACT.md +1 -0
- package/README.md +36 -15
- package/dist/bidi/mux-daemon.js +18 -5
- package/dist/cli.js +495 -55
- package/dist/index.js +329 -16
- package/dist/mcp.js +514 -40
- package/package.json +4 -3
- package/src/cdp/driver.ts +1 -1
- package/src/driver.ts +5 -1
- package/src/focusPolicy.ts +16 -0
- package/src/index.ts +8 -1
- package/src/manifest.ts +124 -0
- package/src/origins.ts +4 -2
- package/src/policy.ts +4 -0
- package/src/shared-tools.ts +11 -6
- package/src/toolDocs.ts +32 -0
- package/src/toolGroups.ts +2 -1
- package/src/tools/screencast.ts +3 -1
- package/src/tools/tab-groups.ts +435 -0
- package/src/version.ts +1 -1
package/dist/index.js
CHANGED
|
@@ -590,6 +590,15 @@ var init_leases = __esm(() => {
|
|
|
590
590
|
LEASE_FILE_RE = new RegExp(`^lease-(${BACKEND_KEY_SOURCE})-(.+)\\.json$`);
|
|
591
591
|
});
|
|
592
592
|
|
|
593
|
+
// src/focusPolicy.ts
|
|
594
|
+
function focusDenied() {
|
|
595
|
+
return TRUTHY.has((process.env.CDP_DENY_FOCUS ?? "").trim().toLowerCase());
|
|
596
|
+
}
|
|
597
|
+
var TRUTHY;
|
|
598
|
+
var init_focusPolicy = __esm(() => {
|
|
599
|
+
TRUTHY = new Set(["1", "true", "yes", "on"]);
|
|
600
|
+
});
|
|
601
|
+
|
|
593
602
|
// src/origins.ts
|
|
594
603
|
import { mkdir as mkdir2, readFile as readFile2, readdir as readdir2, unlink as unlink2, writeFile as writeFile2 } from "node:fs/promises";
|
|
595
604
|
import { join as join2 } from "node:path";
|
|
@@ -689,7 +698,7 @@ async function originIndex(backend, liveIds) {
|
|
|
689
698
|
return index;
|
|
690
699
|
}
|
|
691
700
|
async function newTrackedPage(driver, backend, opts = {}) {
|
|
692
|
-
const page = await driver.newPage(opts.url, { focus: opts.focus === true });
|
|
701
|
+
const page = await driver.newPage(opts.url, { focus: opts.focus === true && !focusDenied() });
|
|
693
702
|
await recordOrigin(backend, page.id, { label: opts.label });
|
|
694
703
|
return page;
|
|
695
704
|
}
|
|
@@ -716,6 +725,7 @@ async function resolveLiveLabel(backend, label, liveIds) {
|
|
|
716
725
|
}
|
|
717
726
|
var ORIGIN_FILE_RE;
|
|
718
727
|
var init_origins = __esm(() => {
|
|
728
|
+
init_focusPolicy();
|
|
719
729
|
init_leases();
|
|
720
730
|
ORIGIN_FILE_RE = new RegExp(`^origin-(${BACKEND_KEY_SOURCE})-(.+)\\.json$`);
|
|
721
731
|
});
|
|
@@ -1005,7 +1015,7 @@ var init_session = __esm(() => {
|
|
|
1005
1015
|
|
|
1006
1016
|
// src/cdp/hiddenTabs.ts
|
|
1007
1017
|
function hiddenTabsRequested() {
|
|
1008
|
-
return
|
|
1018
|
+
return TRUTHY2.has((process.env.CDP_HIDDEN_TABS ?? "").trim().toLowerCase());
|
|
1009
1019
|
}
|
|
1010
1020
|
function hiddenTabsMax() {
|
|
1011
1021
|
const raw = Number(process.env.CDP_HIDDEN_TABS_MAX);
|
|
@@ -1144,10 +1154,10 @@ async function disposeHiddenTabs() {
|
|
|
1144
1154
|
owners.clear();
|
|
1145
1155
|
await Promise.all(all.map((o) => o.dispose()));
|
|
1146
1156
|
}
|
|
1147
|
-
var DEFAULT_HIDDEN_TABS_MAX = 20, HIDDEN_TAB_VIEWPORT, FALLBACK_DEVICE_SCALE_FACTOR = 2, OWNER_COMMAND_TIMEOUT_MS = 5000,
|
|
1157
|
+
var DEFAULT_HIDDEN_TABS_MAX = 20, HIDDEN_TAB_VIEWPORT, FALLBACK_DEVICE_SCALE_FACTOR = 2, OWNER_COMMAND_TIMEOUT_MS = 5000, TRUTHY2, HiddenTabCapError, owners;
|
|
1148
1158
|
var init_hiddenTabs = __esm(() => {
|
|
1149
1159
|
HIDDEN_TAB_VIEWPORT = { width: 1280, height: 800 };
|
|
1150
|
-
|
|
1160
|
+
TRUTHY2 = new Set(["1", "true", "yes", "on"]);
|
|
1151
1161
|
HiddenTabCapError = class HiddenTabCapError extends Error {
|
|
1152
1162
|
max;
|
|
1153
1163
|
openIds;
|
|
@@ -1891,7 +1901,10 @@ var init_driver = __esm(() => {
|
|
|
1891
1901
|
focus_emulation: ["emulate.focus"],
|
|
1892
1902
|
click_focus_gated: ["emulate.focus"],
|
|
1893
1903
|
wait_for_download: ["browser.downloads"],
|
|
1894
|
-
grant_permissions: ["browser.permissions"]
|
|
1904
|
+
grant_permissions: ["browser.permissions"],
|
|
1905
|
+
list_tab_groups: ["browser.tabGroups"],
|
|
1906
|
+
group_tabs: ["browser.tabGroups"],
|
|
1907
|
+
ungroup_tabs: ["browser.tabGroups"]
|
|
1895
1908
|
};
|
|
1896
1909
|
});
|
|
1897
1910
|
|
|
@@ -2726,6 +2739,7 @@ var CDP_CAPABILITIES = new Set([
|
|
|
2726
2739
|
"emulate.focus",
|
|
2727
2740
|
"browser.downloads",
|
|
2728
2741
|
"browser.permissions",
|
|
2742
|
+
"browser.tabGroups",
|
|
2729
2743
|
"worker.targets",
|
|
2730
2744
|
"frame.targets"
|
|
2731
2745
|
]);
|
|
@@ -3638,6 +3652,7 @@ async function reapStaleAgentTabs(driver, backend) {
|
|
|
3638
3652
|
}
|
|
3639
3653
|
|
|
3640
3654
|
// src/shared-tools.ts
|
|
3655
|
+
init_focusPolicy();
|
|
3641
3656
|
init_driver();
|
|
3642
3657
|
import { mkdir as mkdir5, writeFile as writeFile4 } from "node:fs/promises";
|
|
3643
3658
|
import { dirname as dirname2, join as join5 } from "node:path";
|
|
@@ -4321,7 +4336,10 @@ async function listPagesOne(driver, args = {}) {
|
|
|
4321
4336
|
async function newPage(driver, args = {}) {
|
|
4322
4337
|
const label = typeof args.label === "string" && args.label.length ? args.label : defaultLabel();
|
|
4323
4338
|
const p = await newTrackedPage(driver, backendOf(driver), { url: args.url, label, focus: args.focus === true });
|
|
4324
|
-
const hidden =
|
|
4339
|
+
const hidden = {
|
|
4340
|
+
...p.hidden === true ? { hidden: true } : {},
|
|
4341
|
+
...args.focus === true && focusDenied() ? { focusDenied: true } : {}
|
|
4342
|
+
};
|
|
4325
4343
|
const explicit = args.claim === true;
|
|
4326
4344
|
if (!explicit && !requireLease())
|
|
4327
4345
|
return { targetId: p.id, url: p.url, ...hidden };
|
|
@@ -4352,9 +4370,9 @@ async function selectPage(driver, args) {
|
|
|
4352
4370
|
if (args.target === undefined || args.target === "")
|
|
4353
4371
|
throw new SharedToolError("select_page requires an explicit target");
|
|
4354
4372
|
const p = await resolvePage(driver, args.target);
|
|
4355
|
-
if (args.focus !== true) {
|
|
4373
|
+
if (args.focus !== true || focusDenied()) {
|
|
4356
4374
|
await writeSelectedFile(p.id);
|
|
4357
|
-
return { selected: p.id, focused: false };
|
|
4375
|
+
return { selected: p.id, focused: false, ...args.focus === true ? { focusDenied: true } : {} };
|
|
4358
4376
|
}
|
|
4359
4377
|
const shown = await driver.activatePage(p.id);
|
|
4360
4378
|
if (shown.revealedFrom === undefined || shown.id === p.id) {
|
|
@@ -4789,7 +4807,7 @@ async function pressKey(driver, args) {
|
|
|
4789
4807
|
pressed: args.key,
|
|
4790
4808
|
modifiers,
|
|
4791
4809
|
delivered: false,
|
|
4792
|
-
note: "the page saw no keydown: a background tab gets no trusted keyboard input. A click on the tab's page (click tool) usually gives it focus so keys land; or open the tab with new_page focus:true. A key handled inside a cross-origin iframe also reads as not delivered."
|
|
4810
|
+
note: "the page saw no keydown: a background tab gets no trusted keyboard input. A click on the tab's page (click tool) usually gives it focus so keys land; or open the tab with new_page focus:true (which raises the browser over the user's work; refused under CDP_DENY_FOCUS). A key handled inside a cross-origin iframe also reads as not delivered."
|
|
4793
4811
|
};
|
|
4794
4812
|
}
|
|
4795
4813
|
return { pressed: args.key, modifiers };
|
|
@@ -5791,6 +5809,7 @@ async function performanceAnalyzeInsight(args = {}) {
|
|
|
5791
5809
|
}
|
|
5792
5810
|
|
|
5793
5811
|
// src/tools/screencast.ts
|
|
5812
|
+
init_focusPolicy();
|
|
5794
5813
|
init_client();
|
|
5795
5814
|
import { spawn } from "node:child_process";
|
|
5796
5815
|
import { mkdir as mkdir8, rm as rm2, stat as stat2, writeFile as writeFile7 } from "node:fs/promises";
|
|
@@ -6091,7 +6110,7 @@ async function startScreenRecording(args = {}) {
|
|
|
6091
6110
|
});
|
|
6092
6111
|
try {
|
|
6093
6112
|
await conn.send("Page.enable");
|
|
6094
|
-
if (args.bringToFront)
|
|
6113
|
+
if (args.bringToFront && !focusDenied())
|
|
6095
6114
|
await conn.send("Page.bringToFront").catch(() => {});
|
|
6096
6115
|
const params = { format };
|
|
6097
6116
|
if (format === "jpeg" && args.quality !== undefined)
|
|
@@ -6737,11 +6756,302 @@ async function grantPermissions(args = {}) {
|
|
|
6737
6756
|
return { granted: [...permissions], origin, ...reset ? { reset: true } : {}, target };
|
|
6738
6757
|
}
|
|
6739
6758
|
|
|
6740
|
-
// src/tools/
|
|
6759
|
+
// src/tools/tab-groups.ts
|
|
6741
6760
|
init_client();
|
|
6742
|
-
import {
|
|
6743
|
-
import {
|
|
6761
|
+
import { createHash } from "node:crypto";
|
|
6762
|
+
import { mkdir as mkdir12, readFile as readFile9, writeFile as writeFile9 } from "node:fs/promises";
|
|
6763
|
+
import { join as join11 } from "node:path";
|
|
6744
6764
|
var ARTIFACT_DIR7 = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
6765
|
+
var HELPER_DIR = join11(ARTIFACT_DIR7, "tab-groups-helper");
|
|
6766
|
+
var TAB_GROUP_COLORS = ["grey", "blue", "red", "yellow", "green", "pink", "purple", "cyan", "orange"];
|
|
6767
|
+
var HELPER_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqesYyi7LmBxzWP7VFJGlH2khinCz0uU2z3KNDvZ3Kb8gTLPpthgRWp27pu5/Wk2nvQJdcrunf6u5vqomH2xhvXIqjgXtth2o1GrE6oJMWRUqEoC7I4YUXdSieRpxAxtG37KiSFOyFOU59Som3gGOM2j1qlQIsID0QwRCeRs095SLoTtkA6YwZxtOETgvZAhbbpkmYwkXjTzgY85sS1JfMYj67cmfyz/Nt6yfORSJYEmWAfGCjQnvtOn9TjCazZvmoASUhsKZNZVWckAg+Md+ZKBPbl14/UUwLmWHQ7SDcJozHZtFeLFXySwvmBm3NhP1rK5vd/v+modsQY3M3DbqLQIDAQAB";
|
|
6768
|
+
var HELPER_MANIFEST = {
|
|
6769
|
+
manifest_version: 3,
|
|
6770
|
+
name: "cdp-toolkit tab groups",
|
|
6771
|
+
description: "Helper installed by cdp-toolkit's tab-group tools (list_tab_groups, group_tabs, ungroup_tabs). No UI, no state.",
|
|
6772
|
+
version: "1.0.0",
|
|
6773
|
+
key: HELPER_KEY,
|
|
6774
|
+
permissions: ["tabs", "tabGroups", "debugger"],
|
|
6775
|
+
background: { service_worker: "sw.js" }
|
|
6776
|
+
};
|
|
6777
|
+
function extensionIdFromKey(keyBase64) {
|
|
6778
|
+
const hex = createHash("sha256").update(Buffer.from(keyBase64, "base64")).digest("hex").slice(0, 32);
|
|
6779
|
+
return [...hex].map((c) => String.fromCharCode(97 + parseInt(c, 16))).join("");
|
|
6780
|
+
}
|
|
6781
|
+
var HELPER_EXTENSION_ID = extensionIdFromKey(HELPER_KEY);
|
|
6782
|
+
var HELPER_SW_BODY = String.raw`
|
|
6783
|
+
const NONE = -1;
|
|
6784
|
+
|
|
6785
|
+
async function targetIdsByTab() {
|
|
6786
|
+
const m = new Map();
|
|
6787
|
+
try {
|
|
6788
|
+
for (const t of await chrome.debugger.getTargets()) {
|
|
6789
|
+
if (t.type === "page" && typeof t.tabId === "number") m.set(t.tabId, t.id);
|
|
6790
|
+
}
|
|
6791
|
+
} catch (e) {
|
|
6792
|
+
// No mapping is a degraded answer (rows lack targetId), not a failure of the listing.
|
|
6793
|
+
}
|
|
6794
|
+
return m;
|
|
6795
|
+
}
|
|
6796
|
+
|
|
6797
|
+
function row(t, m) {
|
|
6798
|
+
return { tabId: t.id, targetId: m.get(t.id), index: t.index, url: t.url || t.pendingUrl || "", title: t.title || "" };
|
|
6799
|
+
}
|
|
6800
|
+
|
|
6801
|
+
const byPlace = (a, b) => a.windowId - b.windowId || a.index - b.index;
|
|
6802
|
+
|
|
6803
|
+
async function list(args) {
|
|
6804
|
+
const q = args && args.windowId !== undefined ? { windowId: args.windowId } : {};
|
|
6805
|
+
const [tabs, groups, m] = await Promise.all([chrome.tabs.query(q), chrome.tabGroups.query(q), targetIdsByTab()]);
|
|
6806
|
+
const wins = new Map();
|
|
6807
|
+
for (const t of tabs.slice().sort(byPlace)) {
|
|
6808
|
+
let w = wins.get(t.windowId);
|
|
6809
|
+
if (!w) wins.set(t.windowId, (w = { windowId: t.windowId, tabCount: 0, ungrouped: 0, groups: [] }));
|
|
6810
|
+
w.tabCount++;
|
|
6811
|
+
if (t.groupId === NONE || t.groupId === undefined) {
|
|
6812
|
+
w.ungrouped++;
|
|
6813
|
+
if (args && args.includeUngrouped) (w.ungroupedTabs || (w.ungroupedTabs = [])).push(row(t, m));
|
|
6814
|
+
continue;
|
|
6815
|
+
}
|
|
6816
|
+
let g = w.groups.find((x) => x.groupId === t.groupId);
|
|
6817
|
+
if (!g) {
|
|
6818
|
+
const info = groups.find((x) => x.id === t.groupId) || {};
|
|
6819
|
+
g = { groupId: t.groupId, title: info.title || "", color: info.color, collapsed: !!info.collapsed, tabs: [] };
|
|
6820
|
+
w.groups.push(g);
|
|
6821
|
+
}
|
|
6822
|
+
g.tabs.push(row(t, m));
|
|
6823
|
+
}
|
|
6824
|
+
const windows = [...wins.values()];
|
|
6825
|
+
return {
|
|
6826
|
+
windows,
|
|
6827
|
+
groupCount: windows.reduce((n, w) => n + w.groups.length, 0),
|
|
6828
|
+
tabCount: tabs.length,
|
|
6829
|
+
};
|
|
6830
|
+
}
|
|
6831
|
+
|
|
6832
|
+
async function selectTabs(args, m) {
|
|
6833
|
+
if (Array.isArray(args.tabIds)) {
|
|
6834
|
+
const out = [];
|
|
6835
|
+
for (const id of args.tabIds) {
|
|
6836
|
+
try { out.push(await chrome.tabs.get(id)); }
|
|
6837
|
+
catch (e) { throw new Error("no tab with tabId " + id + " (tab ids come from list_tab_groups)"); }
|
|
6838
|
+
}
|
|
6839
|
+
return out;
|
|
6840
|
+
}
|
|
6841
|
+
if (Array.isArray(args.targetIds)) {
|
|
6842
|
+
const byTarget = new Map([...m].map(([tabId, targetId]) => [targetId, tabId]));
|
|
6843
|
+
const out = [];
|
|
6844
|
+
for (const tid of args.targetIds) {
|
|
6845
|
+
const id = byTarget.get(tid);
|
|
6846
|
+
if (id === undefined) throw new Error("no tab with targetId " + tid + " (a page's CDP target id, as list_pages shows it)");
|
|
6847
|
+
out.push(await chrome.tabs.get(id));
|
|
6848
|
+
}
|
|
6849
|
+
return out;
|
|
6850
|
+
}
|
|
6851
|
+
const all = (await chrome.tabs.query({})).sort(byPlace);
|
|
6852
|
+
if (typeof args.urlMatch === "string") {
|
|
6853
|
+
const hit = all.filter((t) => (t.url || "").includes(args.urlMatch) || (t.pendingUrl || "").includes(args.urlMatch));
|
|
6854
|
+
if (!hit.length) throw new Error("urlMatch " + JSON.stringify(args.urlMatch) + " matched no tab");
|
|
6855
|
+
return hit;
|
|
6856
|
+
}
|
|
6857
|
+
if (typeof args.groupId === "number") {
|
|
6858
|
+
await chrome.tabGroups.get(args.groupId).catch(() => { throw new Error("no tab group with groupId " + args.groupId); });
|
|
6859
|
+
return all.filter((t) => t.groupId === args.groupId);
|
|
6860
|
+
}
|
|
6861
|
+
throw new Error("no tab selector given");
|
|
6862
|
+
}
|
|
6863
|
+
|
|
6864
|
+
async function group(args) {
|
|
6865
|
+
const m = await targetIdsByTab();
|
|
6866
|
+
const tabs = await selectTabs(args, m);
|
|
6867
|
+
let win;
|
|
6868
|
+
if (args.groupId !== undefined) {
|
|
6869
|
+
win = (await chrome.tabGroups.get(args.groupId).catch(() => { throw new Error("no tab group with groupId " + args.groupId); })).windowId;
|
|
6870
|
+
} else if (args.windowId !== undefined) {
|
|
6871
|
+
win = args.windowId;
|
|
6872
|
+
} else {
|
|
6873
|
+
const ws = [...new Set(tabs.map((t) => t.windowId))];
|
|
6874
|
+
if (ws.length > 1) {
|
|
6875
|
+
throw new Error(
|
|
6876
|
+
"the selected tabs are in " + ws.length + " windows (" + ws.join(", ") + "); pass windowId to name the window the group goes in " +
|
|
6877
|
+
"(tabs from other windows are moved there). Without it Chrome would move them all into the last-focused window.",
|
|
6878
|
+
);
|
|
6879
|
+
}
|
|
6880
|
+
win = ws[0];
|
|
6881
|
+
}
|
|
6882
|
+
const moved = tabs.filter((t) => t.windowId !== win).map((t) => ({ tabId: t.id, fromWindowId: t.windowId }));
|
|
6883
|
+
const opts = { tabIds: tabs.map((t) => t.id) };
|
|
6884
|
+
if (args.groupId !== undefined) opts.groupId = args.groupId;
|
|
6885
|
+
else opts.createProperties = { windowId: win };
|
|
6886
|
+
const gid = await chrome.tabs.group(opts);
|
|
6887
|
+
const upd = {};
|
|
6888
|
+
if (args.title !== undefined) upd.title = args.title;
|
|
6889
|
+
if (args.color !== undefined) upd.color = args.color;
|
|
6890
|
+
if (args.collapsed !== undefined) upd.collapsed = args.collapsed;
|
|
6891
|
+
const g = Object.keys(upd).length ? await chrome.tabGroups.update(gid, upd) : await chrome.tabGroups.get(gid);
|
|
6892
|
+
const members = g.windowId > 0 ? (await chrome.tabs.query({ windowId: g.windowId })).filter((t) => t.groupId === gid).sort(byPlace) : [];
|
|
6893
|
+
if (members.length === 0) {
|
|
6894
|
+
// Vivaldi answers chrome.tabs.group with an id but keeps its own tab stacks: the "group" has
|
|
6895
|
+
// windowId -1 and no tabs. Fail loudly rather than report a group that does not exist.
|
|
6896
|
+
throw new Error("the browser accepted chrome.tabs.group but created no visible group (windowId " + g.windowId + ", 0 tabs); this browser does not support Chrome tab groups (Vivaldi uses its own tab stacks)");
|
|
6897
|
+
}
|
|
6898
|
+
return { groupId: gid, windowId: g.windowId, title: g.title || "", color: g.color, collapsed: !!g.collapsed, tabs: members.map((t) => row(t, m)), moved };
|
|
6899
|
+
}
|
|
6900
|
+
|
|
6901
|
+
async function ungroup(args) {
|
|
6902
|
+
const m = await targetIdsByTab();
|
|
6903
|
+
const tabs = await selectTabs(args, m);
|
|
6904
|
+
const grouped = tabs.filter((t) => t.groupId !== NONE && t.groupId !== undefined);
|
|
6905
|
+
if (grouped.length) await chrome.tabs.ungroup(grouped.map((t) => t.id));
|
|
6906
|
+
return {
|
|
6907
|
+
ungrouped: grouped.map((t) => Object.assign(row(t, m), { fromGroupId: t.groupId })),
|
|
6908
|
+
alreadyUngrouped: tabs.filter((t) => !grouped.includes(t)).map((t) => t.id),
|
|
6909
|
+
};
|
|
6910
|
+
}
|
|
6911
|
+
|
|
6912
|
+
self.cdpToolkitTabGroups = { version: VERSION, list, group, ungroup };
|
|
6913
|
+
`;
|
|
6914
|
+
var HELPER_VERSION = createHash("sha256").update(HELPER_SW_BODY).digest("hex").slice(0, 12);
|
|
6915
|
+
var HELPER_SW_SOURCE = `"use strict";
|
|
6916
|
+
const VERSION = ${JSON.stringify(HELPER_VERSION)};
|
|
6917
|
+
${HELPER_SW_BODY}`;
|
|
6918
|
+
var isInt = (v) => typeof v === "number" && Number.isInteger(v);
|
|
6919
|
+
function checkInt(tool, name, v) {
|
|
6920
|
+
if (v !== undefined && !isInt(v))
|
|
6921
|
+
throw new Error(`${tool}: '${name}' must be an integer (got ${JSON.stringify(v)})`);
|
|
6922
|
+
}
|
|
6923
|
+
function checkSelector(tool, a, withGroupId) {
|
|
6924
|
+
const given = [
|
|
6925
|
+
a.tabIds !== undefined && "tabIds",
|
|
6926
|
+
a.targetIds !== undefined && "targetIds",
|
|
6927
|
+
typeof a.urlMatch === "string" && a.urlMatch !== "" && "urlMatch",
|
|
6928
|
+
withGroupId && a.groupId !== undefined && "groupId"
|
|
6929
|
+
].filter(Boolean);
|
|
6930
|
+
const names = withGroupId ? "tabIds, targetIds, urlMatch or groupId" : "tabIds, targetIds or urlMatch";
|
|
6931
|
+
if (given.length !== 1)
|
|
6932
|
+
throw new Error(`${tool}: give exactly one of ${names} (got ${given.length ? given.join(" + ") : "none"})`);
|
|
6933
|
+
if (a.tabIds !== undefined) {
|
|
6934
|
+
if (!Array.isArray(a.tabIds) || a.tabIds.length === 0 || !a.tabIds.every(isInt)) {
|
|
6935
|
+
throw new Error(`${tool}: 'tabIds' must be a non-empty array of integer chrome tab ids (got ${JSON.stringify(a.tabIds)})`);
|
|
6936
|
+
}
|
|
6937
|
+
}
|
|
6938
|
+
if (a.targetIds !== undefined) {
|
|
6939
|
+
if (!Array.isArray(a.targetIds) || a.targetIds.length === 0 || !a.targetIds.every((t) => typeof t === "string" && t !== "")) {
|
|
6940
|
+
throw new Error(`${tool}: 'targetIds' must be a non-empty array of CDP target id strings (got ${JSON.stringify(a.targetIds)})`);
|
|
6941
|
+
}
|
|
6942
|
+
}
|
|
6943
|
+
}
|
|
6944
|
+
function validateListTabGroupsArgs(a) {
|
|
6945
|
+
checkInt("list_tab_groups", "windowId", a.windowId);
|
|
6946
|
+
if (a.includeUngrouped !== undefined && typeof a.includeUngrouped !== "boolean") {
|
|
6947
|
+
throw new Error("list_tab_groups: 'includeUngrouped' must be a boolean");
|
|
6948
|
+
}
|
|
6949
|
+
}
|
|
6950
|
+
function validateGroupTabsArgs(a) {
|
|
6951
|
+
const tool = "group_tabs";
|
|
6952
|
+
checkSelector(tool, a, false);
|
|
6953
|
+
checkInt(tool, "groupId", a.groupId);
|
|
6954
|
+
checkInt(tool, "windowId", a.windowId);
|
|
6955
|
+
if (a.groupId !== undefined && a.windowId !== undefined) {
|
|
6956
|
+
throw new Error(`${tool}: give groupId (add to that group, in its window) or windowId (new group in that window), not both`);
|
|
6957
|
+
}
|
|
6958
|
+
if (a.title !== undefined && typeof a.title !== "string")
|
|
6959
|
+
throw new Error(`${tool}: 'title' must be a string`);
|
|
6960
|
+
if (a.collapsed !== undefined && typeof a.collapsed !== "boolean")
|
|
6961
|
+
throw new Error(`${tool}: 'collapsed' must be a boolean`);
|
|
6962
|
+
if (a.color !== undefined && !TAB_GROUP_COLORS.includes(a.color)) {
|
|
6963
|
+
throw new Error(`${tool}: 'color' must be one of ${TAB_GROUP_COLORS.join(", ")} (got ${JSON.stringify(a.color)})`);
|
|
6964
|
+
}
|
|
6965
|
+
}
|
|
6966
|
+
function validateUngroupTabsArgs(a) {
|
|
6967
|
+
checkSelector("ungroup_tabs", a, true);
|
|
6968
|
+
checkInt("ungroup_tabs", "groupId", a.groupId);
|
|
6969
|
+
}
|
|
6970
|
+
var SW_URL_PREFIX = `chrome-extension://${HELPER_EXTENSION_ID}/`;
|
|
6971
|
+
var WORKER_WAIT_MS = 5000;
|
|
6972
|
+
async function helperWorker() {
|
|
6973
|
+
return (await listTargets()).find((t) => t.type === "service_worker" && t.url.startsWith(SW_URL_PREFIX));
|
|
6974
|
+
}
|
|
6975
|
+
async function writeHelper() {
|
|
6976
|
+
await mkdir12(HELPER_DIR, { recursive: true });
|
|
6977
|
+
const files = {
|
|
6978
|
+
"manifest.json": JSON.stringify(HELPER_MANIFEST, null, 2),
|
|
6979
|
+
"sw.js": HELPER_SW_SOURCE
|
|
6980
|
+
};
|
|
6981
|
+
for (const [name, body] of Object.entries(files)) {
|
|
6982
|
+
const path = join11(HELPER_DIR, name);
|
|
6983
|
+
const current = await readFile9(path, "utf8").catch(() => {
|
|
6984
|
+
return;
|
|
6985
|
+
});
|
|
6986
|
+
if (current !== body)
|
|
6987
|
+
await writeFile9(path, body);
|
|
6988
|
+
}
|
|
6989
|
+
}
|
|
6990
|
+
async function evalIn(workerId, expression) {
|
|
6991
|
+
return withPage(workerId, async (conn) => {
|
|
6992
|
+
const r = await conn.send("Runtime.evaluate", { expression, awaitPromise: true, returnByValue: true });
|
|
6993
|
+
if (r.exceptionDetails) {
|
|
6994
|
+
const d = r.exceptionDetails.exception?.description ?? r.exceptionDetails.text ?? "error";
|
|
6995
|
+
throw new Error(d.split(`
|
|
6996
|
+
`)[0].replace(/^Error: /, ""));
|
|
6997
|
+
}
|
|
6998
|
+
return r.result?.value;
|
|
6999
|
+
});
|
|
7000
|
+
}
|
|
7001
|
+
async function currentHelperWorker() {
|
|
7002
|
+
const w = await helperWorker();
|
|
7003
|
+
if (!w)
|
|
7004
|
+
return;
|
|
7005
|
+
const v = await evalIn(w.id, "self.cdpToolkitTabGroups && self.cdpToolkitTabGroups.version").catch(() => {
|
|
7006
|
+
return;
|
|
7007
|
+
});
|
|
7008
|
+
return v === HELPER_VERSION ? w.id : undefined;
|
|
7009
|
+
}
|
|
7010
|
+
async function ensureHelper() {
|
|
7011
|
+
const existing = await currentHelperWorker();
|
|
7012
|
+
if (existing)
|
|
7013
|
+
return existing;
|
|
7014
|
+
await writeHelper();
|
|
7015
|
+
const conn = await openBrowser();
|
|
7016
|
+
try {
|
|
7017
|
+
await conn.send("Extensions.loadUnpacked", { path: HELPER_DIR });
|
|
7018
|
+
} catch (e) {
|
|
7019
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
7020
|
+
throw new CdpError(`tab-group tools could not install their helper extension (Extensions.loadUnpacked: ${msg}). ` + `They need a Chromium that allows loadUnpacked over CDP, with ${HELPER_DIR} on the browser's own machine.`);
|
|
7021
|
+
} finally {
|
|
7022
|
+
conn.close();
|
|
7023
|
+
}
|
|
7024
|
+
const deadline = Date.now() + WORKER_WAIT_MS;
|
|
7025
|
+
while (Date.now() < deadline) {
|
|
7026
|
+
const id = await currentHelperWorker();
|
|
7027
|
+
if (id)
|
|
7028
|
+
return id;
|
|
7029
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
7030
|
+
}
|
|
7031
|
+
throw new CdpError(`the tab-groups helper extension installed but its service worker did not start within ${WORKER_WAIT_MS} ms`);
|
|
7032
|
+
}
|
|
7033
|
+
async function callHelper(op, args) {
|
|
7034
|
+
const id = await ensureHelper();
|
|
7035
|
+
return await evalIn(id, `self.cdpToolkitTabGroups.${op}(${JSON.stringify(args)})`);
|
|
7036
|
+
}
|
|
7037
|
+
async function listTabGroups(args = {}) {
|
|
7038
|
+
validateListTabGroupsArgs(args);
|
|
7039
|
+
return callHelper("list", { windowId: args.windowId, includeUngrouped: args.includeUngrouped });
|
|
7040
|
+
}
|
|
7041
|
+
async function groupTabs(args = {}) {
|
|
7042
|
+
validateGroupTabsArgs(args);
|
|
7043
|
+
return callHelper("group", args);
|
|
7044
|
+
}
|
|
7045
|
+
async function ungroupTabs(args = {}) {
|
|
7046
|
+
validateUngroupTabsArgs(args);
|
|
7047
|
+
return callHelper("ungroup", args);
|
|
7048
|
+
}
|
|
7049
|
+
|
|
7050
|
+
// src/tools/extract.ts
|
|
7051
|
+
init_client();
|
|
7052
|
+
import { mkdir as mkdir13, writeFile as writeFile10 } from "node:fs/promises";
|
|
7053
|
+
import { dirname as dirname3, join as join12 } from "node:path";
|
|
7054
|
+
var ARTIFACT_DIR8 = process.env.CDP_ARTIFACT_DIR ?? "/tmp/cdp-toolkit";
|
|
6745
7055
|
var DEFAULT_BASE_URL = "http://127.0.0.1:8090/v1";
|
|
6746
7056
|
var DEFAULT_MODEL = "schematron";
|
|
6747
7057
|
var DEFAULT_TIMEOUT_MS2 = 90000;
|
|
@@ -7141,10 +7451,10 @@ async function extractPage(driver, args) {
|
|
|
7141
7451
|
};
|
|
7142
7452
|
if (args.savePath === undefined || args.savePath === "")
|
|
7143
7453
|
return result;
|
|
7144
|
-
const path = args.savePath.startsWith("/") ? args.savePath :
|
|
7145
|
-
await
|
|
7454
|
+
const path = args.savePath.startsWith("/") ? args.savePath : join12(ARTIFACT_DIR8, args.savePath);
|
|
7455
|
+
await mkdir13(dirname3(path), { recursive: true });
|
|
7146
7456
|
const json = JSON.stringify(result, null, 2);
|
|
7147
|
-
await
|
|
7457
|
+
await writeFile10(path, json, "utf8");
|
|
7148
7458
|
const saved = {
|
|
7149
7459
|
path,
|
|
7150
7460
|
bytes: Buffer.byteLength(json, "utf8"),
|
|
@@ -7207,6 +7517,9 @@ var TOOLS = {
|
|
|
7207
7517
|
click_focus_gated: clickFocusGated,
|
|
7208
7518
|
wait_for_download: waitForDownload,
|
|
7209
7519
|
grant_permissions: grantPermissions,
|
|
7520
|
+
list_tab_groups: listTabGroups,
|
|
7521
|
+
group_tabs: groupTabs,
|
|
7522
|
+
ungroup_tabs: ungroupTabs,
|
|
7210
7523
|
extract_page: onCdp(extractPage),
|
|
7211
7524
|
claim_page: onCdp(LEASE_TOOLS.claim_page),
|
|
7212
7525
|
release_page: onCdp(LEASE_TOOLS.release_page),
|