Skip to content

Commit 2a3a565

Browse files
centdixclaude
andcommitted
fix: terminal clipboard copy failing in non-secure contexts
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 2f19e25 commit 2a3a565

1 file changed

Lines changed: 26 additions & 3 deletions

File tree

frontend/src/lib/Terminal.svelte

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@
1717
let ws: WebSocket;
1818
let resizeObs: ResizeObserver;
1919
20+
function copyToClipboard(text: string): void {
21+
if (navigator.clipboard?.writeText) {
22+
navigator.clipboard.writeText(text).catch(() => {});
23+
return;
24+
}
25+
// Fallback for non-secure contexts (HTTP on non-localhost)
26+
const ta = document.createElement("textarea");
27+
ta.value = text;
28+
ta.style.position = "fixed";
29+
ta.style.opacity = "0";
30+
document.body.appendChild(ta);
31+
ta.select();
32+
document.execCommand("copy");
33+
document.body.removeChild(ta);
34+
}
35+
2036
export function sendSelectPane(pane: number) {
2137
if (ws?.readyState === WebSocket.OPEN) {
2238
ws.send(JSON.stringify({ type: "selectPane", pane }));
@@ -57,8 +73,7 @@
5773
if (idx !== -1) {
5874
const b64 = data.slice(idx + 1);
5975
try {
60-
const text = atob(b64);
61-
navigator.clipboard.writeText(text);
76+
copyToClipboard(atob(b64));
6277
} catch {}
6378
}
6479
return true;
@@ -68,7 +83,7 @@
6883
term.onSelectionChange(() => {
6984
const sel = term.getSelection();
7085
if (sel) {
71-
navigator.clipboard.writeText(sel);
86+
copyToClipboard(sel);
7287
}
7388
});
7489
@@ -77,6 +92,14 @@
7792
term.attachCustomKeyEventHandler((e: KeyboardEvent) => {
7893
if (e.type !== "keydown") return true;
7994
const mod = e.metaKey || e.ctrlKey;
95+
if (mod && (e.key === "c" || e.key === "C")) {
96+
if (term.hasSelection()) {
97+
copyToClipboard(term.getSelection());
98+
term.clearSelection();
99+
return false;
100+
}
101+
return true;
102+
}
80103
if (mod && (e.key === "ArrowUp" || e.key === "ArrowDown")) return false;
81104
if (mod && (e.key === "k" || e.key === "K")) return false;
82105
if (mod && (e.key === "m" || e.key === "M")) return false;

0 commit comments

Comments
 (0)