From 2849d0a2a1ede3b89a61ef153fe385a6f015feab Mon Sep 17 00:00:00 2001 From: wozeparrot Date: Sat, 8 Jun 2024 23:51:47 +0000 Subject: [PATCH] fix copying to clipboard on a non secure context (#4890) --- examples/tinychat/index.html | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/tinychat/index.html b/examples/tinychat/index.html index 8cd2519078..a361dd01cb 100644 --- a/examples/tinychat/index.html +++ b/examples/tinychat/index.html @@ -97,7 +97,15 @@ button.className = 'clipboard-button'; button.innerHTML = ''; button.onclick = () => { - navigator.clipboard.writeText(codeBlock.textContent); + // navigator.clipboard.writeText(codeBlock.textContent); + const range = document.createRange(); + range.setStartBefore(codeBlock); + range.setEndAfter(codeBlock); + window.getSelection()?.removeAllRanges(); + window.getSelection()?.addRange(range); + document.execCommand('copy'); + window.getSelection()?.removeAllRanges(); + button.innerHTML = ''; setTimeout(() => button.innerHTML = '', 1000); };