| 37 | }; |
| 38 | |
| 39 | const fallbackCopy = (text) => { |
| 40 | try { |
| 41 | const textArea = document.createElement('textarea'); |
| 42 | textArea.value = text; |
| 43 | textArea.style.cssText = ` |
| 44 | position: fixed; |
| 45 | top: -9999px; |
| 46 | left: -9999px; |
| 47 | opacity: 0; |
| 48 | pointer-events: none; |
| 49 | z-index: -1; |
| 50 | `; |
| 51 | textArea.setAttribute('readonly', ''); |
| 52 | |
| 53 | document.body.appendChild(textArea); |
| 54 | textArea.select(); |
| 55 | textArea.setSelectionRange(0, text.length); |
| 56 | |
| 57 | const successful = document.execCommand('copy'); |
| 58 | document.body.removeChild(textArea); |
| 59 | |
| 60 | if (successful) { |
| 61 | Toast.success({ |
| 62 | content: t('消息已复制到剪贴板'), |
| 63 | duration: 2, |
| 64 | }); |
| 65 | } else { |
| 66 | throw new Error('execCommand copy failed'); |
| 67 | } |
| 68 | } catch (err) { |
| 69 | console.error('回退复制方案也失败:', err); |
| 70 | |
| 71 | let errorMessage = t(ERROR_MESSAGES.COPY_FAILED); |
| 72 | if (window.location.protocol === 'http:' && window.location.hostname !== 'localhost') { |
| 73 | errorMessage = t(ERROR_MESSAGES.COPY_HTTPS_REQUIRED); |
| 74 | } else if (!navigator.clipboard && !document.execCommand) { |
| 75 | errorMessage = t(ERROR_MESSAGES.BROWSER_NOT_SUPPORTED); |
| 76 | } |
| 77 | |
| 78 | Toast.error({ |
| 79 | content: errorMessage, |
| 80 | duration: 4, |
| 81 | }); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | copyToClipboard(textToCopy); |
| 86 | }, [t]); |