diff --git a/web/src/lib/clipboard.ts b/web/src/lib/clipboard.ts new file mode 100644 index 0000000..20d9ebf --- /dev/null +++ b/web/src/lib/clipboard.ts @@ -0,0 +1,30 @@ +// copyText 复制文本到剪贴板。 +// 优先用 Clipboard API(安全上下文 HTTPS/localhost);HTTP 环境回退到 execCommand('copy')。 +export async function copyText(text: string): Promise { + // 1. Clipboard API + if (navigator.clipboard?.writeText) { + try { + await navigator.clipboard.writeText(text) + return true + } catch { + /* 权限/焦点问题,回退 */ + } + } + // 2. execCommand 兜底(非安全上下文可用) + try { + const ta = document.createElement('textarea') + ta.value = text + ta.style.position = 'fixed' + ta.style.opacity = '0' + ta.style.pointerEvents = 'none' + document.body.appendChild(ta) + ta.focus() + ta.select() + ta.setSelectionRange(0, text.length) + const ok = document.execCommand('copy') + document.body.removeChild(ta) + return ok + } catch { + return false + } +} diff --git a/web/src/views/console/KeysView.vue b/web/src/views/console/KeysView.vue index f0230f3..57fef49 100644 --- a/web/src/views/console/KeysView.vue +++ b/web/src/views/console/KeysView.vue @@ -1,9 +1,10 @@