| |
| |
| |
| |
| |
| |
| export async function copyToClipboard(text: string): Promise<boolean> { |
| |
| if (navigator.clipboard && window.isSecureContext) { |
| try { |
| await navigator.clipboard.writeText(text); |
| return true; |
| } catch (err) { |
| console.error('Clipboard API 复制失败:', err); |
| } |
| } |
|
|
| |
| try { |
| const textArea = document.createElement('textarea'); |
| textArea.value = text; |
|
|
| |
| textArea.style.position = 'fixed'; |
| textArea.style.left = '-9999px'; |
| textArea.style.top = '0'; |
| document.body.appendChild(textArea); |
|
|
| textArea.focus(); |
| textArea.select(); |
|
|
| const successful = document.execCommand('copy'); |
| document.body.removeChild(textArea); |
|
|
| return successful; |
| } catch (err) { |
| console.error('execCommand 复制失败:', err); |
| return false; |
| } |
| } |
|
|