(text)
| 756 | // ============================================ |
| 757 | |
| 758 | async function copyToClipboard(text) { |
| 759 | if (navigator.clipboard && navigator.clipboard.writeText) { |
| 760 | try { |
| 761 | await navigator.clipboard.writeText(text); |
| 762 | toast.success('已复制到剪贴板'); |
| 763 | return true; |
| 764 | } catch (err) { |
| 765 | // 降级到 execCommand |
| 766 | } |
| 767 | } |
| 768 | try { |
| 769 | const ta = document.createElement('textarea'); |
| 770 | ta.value = text; |
| 771 | ta.style.cssText = 'position:fixed;top:0;left:0;opacity:0;pointer-events:none;'; |
| 772 | document.body.appendChild(ta); |
| 773 | ta.focus(); |
| 774 | ta.select(); |
| 775 | const ok = document.execCommand('copy'); |
| 776 | document.body.removeChild(ta); |
| 777 | if (ok) { |
| 778 | toast.success('已复制到剪贴板'); |
| 779 | return true; |
| 780 | } |
| 781 | throw new Error('execCommand failed'); |
| 782 | } catch (err) { |
| 783 | toast.error('复制失败'); |
| 784 | return false; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // ============================================ |
| 789 | // 本地存储助手 |
no test coverage detected