From 48f0d08be65c246cdb0e66a13f83eee7c461120e Mon Sep 17 00:00:00 2001 From: hope Date: Wed, 4 Mar 2026 17:20:05 +0800 Subject: [PATCH] fix(web): add fallback for clipboard API in insecure contexts (#34092) - Clipboard API requires secure context (HTTPS) to work - Windows HTTP environments fail silently - Add textarea element fallback for non-secure contexts --- ui/src/ui/chat/copy-as-markdown.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ui/src/ui/chat/copy-as-markdown.ts b/ui/src/ui/chat/copy-as-markdown.ts index 12aeb6999e6..d402bdeccb7 100644 --- a/ui/src/ui/chat/copy-as-markdown.ts +++ b/ui/src/ui/chat/copy-as-markdown.ts @@ -17,11 +17,28 @@ async function copyTextToClipboard(text: string): Promise { return false; } + // Try Clipboard API first (works in secure contexts/HTTPS) try { await navigator.clipboard.writeText(text); return true; } catch { - return false; + // Fallback for non-secure contexts (HTTP on Windows/localhost) + try { + // Use textarea element fallback for insecure contexts + const textarea = document.createElement("textarea"); + textarea.value = text; + textarea.style.position = "fixed"; + textarea.style.left = "-9999px"; + textarea.style.top = "-9999px"; + document.body.appendChild(textarea); + textarea.focus(); + textarea.select(); + const success = document.execCommand("copy"); + document.body.removeChild(textarea); + return success; + } catch { + return false; + } } }