跳到主要内容

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

浏览文件

browser-visibility.mjs

80 行 · 3.1 KB历史下载
1// Chromium must observe a minimized window to throttle its hidden pages.
2// Withdrawing an X window alone leaves document.visibilityState visible.
3export class BrowserVisibility {
4 constructor(cdp, targets, canHide = async () => true) {
5 this.cdp = cdp;
6 this.targets = targets;
7 this.canHide = canHide;
8 this.visible = true;
9 this.saved = new Map();
10 this.queue = Promise.resolve();
11 }
12
13 set(visible) {
14 if (typeof visible !== "boolean") throw new Error("Invalid browser visibility");
15 this.visible = visible;
16 const result = this.queue.catch(() => {}).then(() => this.apply());
17 this.queue = result;
18 return result;
19 }
20
21 activate(targetId) {
22 const result = this.queue.catch(() => {}).then(async () => {
23 await this.cdp("Target.activateTarget", { targetId });
24 if (this.visible || !await this.canHide()) {
25 const { windowId, bounds } = await this.cdp("Browser.getWindowForTarget", { targetId });
26 if (bounds.windowState === "minimized") await this.setBounds(windowId, { windowState: "normal" });
27 await this.setBounds(windowId, { windowState: "fullscreen" });
28 this.saved.delete(windowId);
29 } else {
30 await this.apply();
31 }
32 });
33 this.queue = result;
34 return result;
35 }
36
37 async setBounds(windowId, bounds) {
38 await this.cdp("Browser.setWindowBounds", { windowId, bounds });
39 // The CDP reply can precede the window manager's state transition.
40 // Fullscreen/minimized changes must observe the intermediate normal state.
41 for (let attempt = 0; attempt < 40; attempt++) {
42 const current = await this.cdp("Browser.getWindowBounds", { windowId });
43 if (current.bounds.windowState === bounds.windowState) return;
44 await new Promise(resolve => setTimeout(resolve, 25));
45 }
46 throw new Error("Browser window did not change state");
47 }
48
49 async apply() {
50 const visible = this.visible || !await this.canHide();
51 const windows = new Map();
52 for (const targetId of this.targets()) {
53 try {
54 const window = await this.cdp("Browser.getWindowForTarget", { targetId });
55 windows.set(window.windowId, window.bounds);
56 } catch (error) {
57 if (this.targets().includes(targetId)) throw error;
58 }
59 }
60 for (const windowId of this.saved.keys()) {
61 if (!windows.has(windowId)) this.saved.delete(windowId);
62 }
63 for (const [windowId, bounds] of windows) {
64 if (visible) {
65 const saved = this.saved.get(windowId);
66 if (!saved) continue;
67 await this.setBounds(windowId, { windowState: "normal" });
68 const restore = saved.windowState === "normal" ? saved : { windowState: saved.windowState };
69 await this.setBounds(windowId, restore);
70 this.saved.delete(windowId);
71 } else if (bounds.windowState !== "minimized") {
72 if (!this.saved.has(windowId)) this.saved.set(windowId, bounds);
73 if (bounds.windowState !== "normal") {
74 await this.setBounds(windowId, { windowState: "normal" });
75 }
76 await this.setBounds(windowId, { windowState: "minimized" });
77 }
78 }
79 }
80}