From caa20dfb7658ab0b815e68f537bdbdf727212e70 Mon Sep 17 00:00:00 2001 From: jingrow Date: Mon, 29 Dec 2025 23:09:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=8D=E5=88=B6=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dashboard/src/components/ClickToCopyField.vue | 53 +++++++++++++++---- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/dashboard/src/components/ClickToCopyField.vue b/dashboard/src/components/ClickToCopyField.vue index c4c12cf..e635412 100644 --- a/dashboard/src/components/ClickToCopyField.vue +++ b/dashboard/src/components/ClickToCopyField.vue @@ -44,15 +44,50 @@ export default { }; }, methods: { - copyTextContentToClipboard() { - const clipboard = window.navigator.clipboard; - clipboard.writeText(this.textContent).then(() => { - this.copied = true; - setTimeout(() => { - this.copied = false; - }, 4000); - toast.success(this.$t('Copied to clipboard!')); - }); + async copyTextContentToClipboard() { + try { + // 优先使用现代的 Clipboard API + if (navigator.clipboard && navigator.clipboard.writeText) { + await navigator.clipboard.writeText(this.textContent); + this.showCopySuccess(); + } else { + // 降级方案:使用传统的 execCommand 方法 + this.fallbackCopyTextToClipboard(this.textContent); + } + } catch (error) { + // 如果 Clipboard API 失败,尝试降级方案 + this.fallbackCopyTextToClipboard(this.textContent); + } + }, + fallbackCopyTextToClipboard(text) { + const textArea = document.createElement('textarea'); + textArea.value = text; + textArea.style.position = 'fixed'; + textArea.style.left = '-999999px'; + textArea.style.top = '-999999px'; + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + + try { + const successful = document.execCommand('copy'); + if (successful) { + this.showCopySuccess(); + } else { + toast.error(this.$t('Copy failed, please copy manually')); + } + } catch (err) { + toast.error(this.$t('Copy failed, please copy manually')); + } finally { + document.body.removeChild(textArea); + } + }, + showCopySuccess() { + this.copied = true; + setTimeout(() => { + this.copied = false; + }, 4000); + toast.success(this.$t('Copied to clipboard!')); } } };