From f2fe001cc4e95c602cdabd5f35200f3574c5f098 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Fri, 6 Mar 2026 19:01:38 -0800 Subject: [PATCH] Fix message copy buttons not working over HTTP --- js/global_scope_js.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/js/global_scope_js.js b/js/global_scope_js.js index 205d9375..62b31d37 100644 --- a/js/global_scope_js.js +++ b/js/global_scope_js.js @@ -11,7 +11,11 @@ function copyToClipboard(element) { const rawText = messageElement.getAttribute("data-raw"); if (!rawText) return; - navigator.clipboard.writeText(rawText).then(function() { + const copyPromise = navigator.clipboard && window.isSecureContext + ? navigator.clipboard.writeText(rawText) + : fallbackCopyToClipboard(rawText); + + copyPromise.then(function() { const originalSvg = element.innerHTML; element.innerHTML = ""; setTimeout(() => { @@ -22,6 +26,27 @@ function copyToClipboard(element) { }); } +function fallbackCopyToClipboard(text) { + return new Promise((resolve, reject) => { + 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(); + try { + const successful = document.execCommand("copy"); + document.body.removeChild(textArea); + successful ? resolve() : reject(); + } catch (err) { + document.body.removeChild(textArea); + reject(err); + } + }); +} + function branchHere(element) { if (!element) return;