跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

control.js

98 行 · 4.9 KB历史下载
1// This extension page is opened as a hidden CDP target by the guest controller.
2// The debugger permission maps CDP target IDs to Chrome tab IDs. We never
3// attach a debugger through the extension or accept messages from webpages.
4const targetsByTab = new Map();
5globalThis.manageCageExtensions = async function (input) {
6 const items = (await chrome.management.getAll()).filter(item => item.id !== chrome.runtime.id && item.type === 'extension');
7 if (input.mode === 'list') return { items: items.map(({ id, name, description, version, enabled, mayDisable, optionsUrl, permissions, hostPermissions, installType }) =>
8 ({ id, name, description, version, enabled, mayDisable, optionsUrl, permissions, hostPermissions, installType })) };
9 const entry = items.find(item => item.id === input.id);
10 if (!entry) throw new Error('扩展不存在');
11 if (input.mode === 'options') return { url: entry.optionsUrl || '' };
12 if (!entry.mayDisable) throw new Error('此扩展由浏览器管理');
13 if (!['enable', 'disable'].includes(input.mode)) throw new Error('扩展操作无效');
14 await chrome.management.setEnabled(input.id, input.mode === 'enable');
15 return { ok: true };
16};
17let managedSplit;
18const faviconVersions = new Map();
19chrome.tabs.onUpdated.addListener((tabId, changes) => {
20 if (!("favIconUrl" in changes || "url" in changes || changes.status === "complete")) return;
21 faviconVersions.set(tabId, (faviconVersions.get(tabId) || 0) + 1);
22 void chrome.debugger.getTargets().then(targets => {
23 const target = targets.find(item => item.tabId === tabId);
24 if (target) globalThis.cageFaviconChanged?.(target.id);
25 }).catch(() => {});
26});
27chrome.tabs.onRemoved.addListener(tabId => faviconVersions.delete(tabId));
28
29// Read Chromium's decoded favicon cache. The extension never requests a site's
30// icon URL, so its permissions cannot bypass the webpage's image policy.
31globalThis.readCageFavicon = async function (targetId) {
32 try {
33 const targets = await chrome.debugger.getTargets();
34 const tabId = targets.find(target => target.id === targetId)?.tabId;
35 if (tabId == null) return null;
36 const tab = await chrome.tabs.get(tabId);
37 const version = faviconVersions.get(tabId);
38 if (!/^https?:/.test(tab.url || "") || !tab.favIconUrl) return { url: tab.url, favicon: "" };
39 const source = new URL(chrome.runtime.getURL("/_favicon/"));
40 source.searchParams.set("pageUrl", tab.url);
41 source.searchParams.set("size", "32");
42 source.searchParams.set("version", String(version || 0));
43 const image = new Image();
44 image.src = source.href;
45 await image.decode();
46 const current = await chrome.tabs.get(tabId);
47 if (current.url !== tab.url || current.favIconUrl !== tab.favIconUrl || faviconVersions.get(tabId) !== version) return null;
48 const canvas = document.createElement("canvas");
49 canvas.width = canvas.height = 32;
50 canvas.getContext("2d").drawImage(image, 0, 0, 32, 32);
51 const favicon = canvas.toDataURL("image/png");
52 return { url: tab.url, favicon: favicon.length <= 8192 ? favicon : "" };
53 } catch {
54 return null;
55 }
56};
57
58chrome.tabs.onActivated.addListener(({ tabId }) => {
59 const target = targetsByTab.get(tabId);
60 if (target) globalThis.cagePaneActivated?.(target);
61});
62
63chrome.tabs.onUpdated.addListener((id, changes) => {
64 const target = targetsByTab.get(id);
65 if (target && "splitViewId" in changes && changes.splitViewId !== managedSplit) {
66 targetsByTab.clear();
67 managedSplit = null;
68 globalThis.cagePaneSeparated?.(target);
69 }
70});
71
72globalThis.setCageSplit = async function (left, right) {
73 if (!chrome.tabs.createSplit) throw new Error("Chromium 155 or later is required");
74 const targets = await chrome.debugger.getTargets();
75 const findTab = id => targets.find(t => t.id === id)?.tabId;
76 const pair = left ? await Promise.all([left, right].map(id => chrome.tabs.get(findTab(id)))) : [];
77 if (pair.length && pair[0].incognito !== pair[1].incognito) throw new Error("Separate browser profiles");
78 targetsByTab.clear();
79 const affected = new Set(pair.map(tab => tab.splitViewId));
80 if (managedSplit != null) affected.add(managedSplit);
81 if (!pair.length && managedSplit == null) {
82 // Chromium can restore the foreground split before Cage reconnects.
83 const [active] = await chrome.tabs.query({ active: true, lastFocusedWindow: true });
84 if (active) affected.add(active.splitViewId);
85 }
86 const open = await chrome.tabs.query({});
87 for (const id of affected)
88 if (id >= 0 && open.some(tab => tab.splitViewId === id)) await chrome.tabs.unsplit(id);
89 managedSplit = null;
90 if (!pair.length) return;
91 const [a, b] = pair;
92 const index = a.index + (b.windowId === a.windowId && b.index < a.index ? 0 : 1);
93 if (a.windowId !== b.windowId || b.index !== index)
94 await chrome.tabs.move(b.id, { windowId: a.windowId, index });
95 managedSplit = await chrome.tabs.createSplit([a.id, b.id]);
96 targetsByTab.set(a.id, left);
97 targetsByTab.set(b.id, right);
98};