clipboard.ts 515 B

1234567891011121314151617
  1. export async function copyToClipboard(text: string): Promise<boolean> {
  2. try {
  3. if (navigator.clipboard?.writeText) {
  4. await navigator.clipboard.writeText(text);
  5. return true;
  6. }
  7. } catch {}
  8. const textarea = document.createElement('textarea');
  9. textarea.value = text;
  10. textarea.style.position = 'fixed';
  11. textarea.style.opacity = '0';
  12. document.body.appendChild(textarea);
  13. textarea.select();
  14. const ok = document.execCommand('copy');
  15. document.body.removeChild(textarea);
  16. return ok;
  17. }