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;