|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function copyToClipboard(text: string): Promise<void> { |
|
if (navigator.clipboard) { |
|
try { |
|
await navigator.clipboard.writeText(text); |
|
return; |
|
|
|
} catch (_) { |
|
|
|
} |
|
} |
|
|
|
|
|
try { |
|
const textArea = document.createElement("textarea"); |
|
textArea.value = text; |
|
|
|
|
|
textArea.style.top = "0"; |
|
textArea.style.left = "0"; |
|
textArea.style.position = "fixed"; |
|
|
|
document.body.appendChild(textArea); |
|
textArea.focus(); |
|
textArea.select(); |
|
|
|
const successful = document.execCommand("copy"); |
|
document.body.removeChild(textArea); |
|
|
|
if (!successful) { |
|
throw new Error("Failed to copy text using fallback method."); |
|
} |
|
} catch (err) { |
|
return Promise.reject(err); |
|
} |
|
} |
|
|