跳到主要内容

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

浏览文件

clipboard.js

211 行 · 10.0 KB历史下载
1// Read/write the host system clipboard only after an explicit user action.
2import UI from './app/ui.js?cage=3';
3import { getKeycode, getKeysym } from './core/input/util.js';
4
5const bar = document.createElement('div');
6bar.id = 'cage-clipboard-bar';
7bar.innerHTML = `<strong>Cage 浏览器</strong><button id="cage-copy">复制到本机</button><button id="cage-paste">粘贴到 Cage</button><button id="cage-transfer">文本传递</button><span role="status" id="cage-clipboard-status">远程画面内 ⌘→Ctrl · 剪贴板仅传递文本</span>`;
8document.body.append(bar);
9const panel = document.createElement('div');
10panel.id = 'cage-clipboard-panel';
11panel.hidden = true;
12panel.innerHTML = `<label for="cage-transfer-text">待传递文本(可在这里使用本机复制、粘贴)</label><textarea id="cage-transfer-text" rows="5"></textarea><button id="cage-send">粘贴到 Cage</button><button id="cage-close">关闭</button>`;
13document.body.append(panel);
14const style = document.createElement('style');
15style.textContent = `#cage-clipboard-bar{position:fixed;z-index:10000;top:0;left:0;right:0;height:42px;display:flex;align-items:center;gap:10px;padding:0 14px;background:#20252b;color:#fff;font:13px system-ui}#cage-clipboard-bar button,#cage-clipboard-panel button{padding:5px 10px;border:1px solid #777;border-radius:5px;cursor:pointer}#cage-clipboard-status{color:#c7d2df}body{padding-top:42px;box-sizing:border-box}#cage-clipboard-panel{position:fixed;z-index:10001;top:50px;right:12px;width:min(440px,90vw);padding:16px;background:#fff;color:#222;font:14px system-ui;border:1px solid #aaa;border-radius:8px}#cage-transfer-text{display:block;box-sizing:border-box;width:100%;margin:10px 0;resize:vertical}`;
16document.head.append(style);
17// Browsers reliably dispatch native paste to an editable element. A canvas
18// alone is not a cross-browser clipboard target.
19const pasteTarget = document.createElement('textarea');
20pasteTarget.id = 'cage-native-paste';
21pasteTarget.setAttribute('aria-label', 'Cage 原生粘贴接收区');
22pasteTarget.style.cssText = 'position:fixed;left:-10000px;top:0;width:1px;height:1px';
23document.body.append(pasteTarget);
24const status = document.getElementById('cage-clipboard-status');
25const field = document.getElementById('cage-transfer-text');
26let bound;
27let copyRequested = false;
28let pasteSequence = 0;
29let pendingPastes = 0;
30let copyTimer;
31let lastRemoteText = '';
32const say = text => { status.textContent = text; };
33function remoteFocused() {
34 return document.getElementById('noVNC_container')?.contains(document.activeElement);
35}
36function shortcut(key) {
37 const rfb = UI.rfb;
38 rfb.sendKey(0xffe3, 'ControlLeft', true);
39 rfb.sendKey(key.charCodeAt(0), 'Key' + key.toUpperCase(), true);
40 rfb.sendKey(key.charCodeAt(0), 'Key' + key.toUpperCase(), false);
41 rfb.sendKey(0xffe3, 'ControlLeft', false);
42}
43function bind() {
44 if (!UI.rfb || bound === UI.rfb) return;
45 bound = UI.rfb;
46 bound.addEventListener('clipboard', async event => {
47 lastRemoteText = event.detail.text;
48 if (!copyRequested) return;
49 copyRequested = false;
50 clearTimeout(copyTimer);
51 try {
52 await navigator.clipboard.writeText(lastRemoteText);
53 say('已复制到本机');
54 } catch {
55 field.value = lastRemoteText;
56 panel.hidden = false;
57 field.focus();
58 field.select();
59 say('浏览器未允许剪贴板写入,请在文本框中复制');
60 }
61 });
62}
63new MutationObserver(bind).observe(document.documentElement, {attributes:true, attributeFilter:['class']});
64bind();
65function connected() {
66 bind();
67 if (!UI.connected || !UI.rfb) { say('请先连接 Cage 浏览器'); return false; }
68 return true;
69}
70function copy(key = 'c') {
71 if (!connected()) return;
72 copyRequested = true;
73 shortcut(key);
74 say('正在复制 Cage 中选中的文本…');
75 clearTimeout(copyTimer);
76 copyTimer = setTimeout(() => {
77 copyRequested = false;
78 // Do not copy an older value when the remote selection did not change.
79 field.value = lastRemoteText;
80 panel.hidden = false;
81 say('如未选中文本,请先选择;也可在文本框中复制现有 Cage 剪贴板');
82 }, 1800);
83}
84function paste(text) {
85 if (!connected()) return;
86 console.debug("Cage paste accepted", ++pasteSequence);
87 pendingPastes++;
88 const done = UI.rfb.cagePasteText(text);
89 UI.rfb.focus();
90 panel.hidden = true;
91 say('正在粘贴…');
92 done.then(() => { pendingPastes--; say(pendingPastes ? '正在粘贴…' : '已粘贴到 Cage'); }, () => { pendingPastes--; say('剪贴板未就绪,请重新连接后重试'); });
93}
94async function readAndPaste() {
95 if (!connected()) return;
96 try { paste(await navigator.clipboard.readText()); }
97 catch {
98 field.value = '';
99 panel.hidden = false;
100 field.focus();
101 say('请在文本框中粘贴,再点击“粘贴到 Cage”');
102 }
103}
104document.getElementById('cage-copy').onclick = () => copy();
105document.getElementById('cage-paste').onclick = readAndPaste;
106document.getElementById('cage-transfer').onclick = () => {
107 field.value = lastRemoteText;
108 panel.hidden = !panel.hidden;
109 if (!panel.hidden) field.focus();
110};
111document.getElementById('cage-send').onclick = () => paste(field.value);
112document.getElementById('cage-close').onclick = () => { panel.hidden = true; UI.rfb?.focus(); };
113// Capture shortcuts only over the remote screen. Local toolbar fields keep
114// the host browser's shortcuts. Use noVNC's key conversion for letters,
115// digits, punctuation and named keys rather than maintaining another map.
116const handledKeys = new Set();
117const remoteModifiers = new Map();
118const physicalModifiers = new Set();
119window.addEventListener('blur', () => {
120 for (const [code, {rfb, keysym}] of remoteModifiers) rfb.sendKey(keysym, code, false);
121 remoteModifiers.clear();
122 physicalModifiers.clear();
123 handledKeys.clear();
124});
125const modifierKeys = new Set(['Meta', 'Control', 'Shift', 'Alt', 'CapsLock']);
126function commandShortcut(event) {
127 const code = getKeycode(event);
128 let keysym = getKeysym(event);
129 // Some macOS Command chords expose a control character instead of the
130 // letter. Recover only that malformed case from its physical letter key.
131 if ((keysym === null || keysym < 0x20) && /^Key[A-Z]$/.test(code)) {
132 keysym = (event.shiftKey ? code[3] : code[3].toLowerCase()).codePointAt(0);
133 }
134 if (keysym === null || !connected()) return false;
135 // Some macOS input paths report modifier flags without separate modifier
136 // keydown events. Assert the chord explicitly, then release only modifiers
137 // that are not physically held according to the events we observed.
138 const held = prefix => [...physicalModifiers].some(code => code.startsWith(prefix));
139 const control = physicalModifiers.has('ControlRight') ? [0xffe4, 'ControlRight'] : [0xffe3, 'ControlLeft'];
140 const shift = physicalModifiers.has('ShiftRight') ? [0xffe2, 'ShiftRight'] : [0xffe1, 'ShiftLeft'];
141 UI.rfb.sendKey(...control, true);
142 if (event.shiftKey) UI.rfb.sendKey(...shift, true);
143 UI.rfb.sendKey(keysym, code, true);
144 UI.rfb.sendKey(keysym, code, false);
145 if (event.shiftKey && !held('Shift')) UI.rfb.sendKey(...shift, false);
146 if (!event.ctrlKey || !held('Control')) UI.rfb.sendKey(...control, false);
147 return true;
148}
149document.addEventListener('keydown', event => {
150 if (!remoteFocused()) return;
151 const key = event.key.toLowerCase();
152 const code = getKeycode(event);
153 if (['Shift', 'Control', 'Alt'].includes(event.key)) physicalModifiers.add(code);
154 // noVNC normally synthesizes immediate key-up while Command is held,
155 // even for Shift. Own these modifier pairs so Cmd+Shift+Z keeps Shift.
156 if (event.metaKey && ['Shift', 'Control', 'Alt'].includes(event.key) && connected()) {
157 const keysym = getKeysym(event);
158 UI.rfb.sendKey(keysym, code, true);
159 remoteModifiers.set(code, {rfb:UI.rfb, keysym});
160 event.preventDefault();
161 event.stopImmediatePropagation();
162 return;
163 }
164 const plainClipboard = !event.altKey && !event.shiftKey && ['c','x'].includes(key);
165 const pasteClipboard = !event.altKey && key === 'v';
166 if ((event.metaKey || event.ctrlKey) && (plainClipboard || pasteClipboard)) {
167 event.stopImmediatePropagation();
168 handledKeys.add(code);
169 if (pasteClipboard) {
170 pasteTarget.value = '';
171 pasteTarget.focus({preventScroll:true});
172 say('等待本机粘贴事件…');
173 return; // Keep the native paste event and its clipboard payload.
174 }
175 event.preventDefault();
176 copy(key);
177 return;
178 }
179 if (event.metaKey && !modifierKeys.has(event.key) && commandShortcut(event)) {
180 event.preventDefault();
181 event.stopImmediatePropagation();
182 handledKeys.add(code);
183 return;
184 }
185 // Command itself must never reach Linux as Super. Shift/Alt/Ctrl still
186 // reach noVNC normally, regardless of the order modifiers are pressed.
187 if (event.key === 'Meta') event.stopImmediatePropagation();
188}, true);
189document.addEventListener('keyup', event => {
190 const code = getKeycode(event);
191 physicalModifiers.delete(code);
192 const modifier = remoteModifiers.get(code);
193 if (modifier) {
194 modifier.rfb.sendKey(modifier.keysym, code, false);
195 remoteModifiers.delete(code);
196 event.stopImmediatePropagation();
197 return;
198 }
199 // Match by physical key even if Command was released first, or focus
200 // moved to the native paste receiver after keydown.
201 if (handledKeys.delete(getKeycode(event)) || (remoteFocused() && event.key === 'Meta')) {
202 event.stopImmediatePropagation();
203 }
204}, true);
205document.addEventListener('paste', event => {
206 if ((!remoteFocused() && event.target !== pasteTarget) || !connected()) return;
207 event.preventDefault();
208 event.stopImmediatePropagation();
209 if (!event.clipboardData.types.includes('text/plain')) { say('当前仅支持文本粘贴'); return; }
210 paste(event.clipboardData.getData('text/plain'));
211}, true);