diff --git a/src/utils/copy.ts b/src/utils/copy.ts index 1b1c26d0..d181cd65 100644 --- a/src/utils/copy.ts +++ b/src/utils/copy.ts @@ -6,6 +6,19 @@ document.addEventListener('copy', (e) => { e.preventDefault() }) -export function copyToClip(text: string) { - return navigator.clipboard.writeText(text) +export async function copyToClip(text: string) { + // https://stackoverflow.com/questions/51805395/navigator-clipboard-is-undefined + if (navigator.clipboard && window.isSecureContext) { + await navigator.clipboard.writeText(text) + } + else { + const input: HTMLTextAreaElement = document.createElement('textarea') + input.setAttribute('readonly', 'readonly') + input.value = text + document.body.appendChild(input) + input.select() + if (document.execCommand('copy')) + document.execCommand('copy') + document.body.removeChild(input) + } }