/** * 复制文本。 * * **必须留兜底**:这个站点是走 http://192.168.x.x:18051 访问的,不是安全上下文, * 浏览器在这种情况下根本不提供 `navigator.clipboard`(不是拒绝,是 undefined)。 * 只写新 API 的话,局域网里点「复制」永远没反应。 */ export async function copyText(value: string): Promise { if (!value) return false if (window.isSecureContext && navigator.clipboard) { try { await navigator.clipboard.writeText(value) return true } catch { // 落到下面的兜底 } } try { const area = document.createElement('textarea') area.value = value // 定位到视口外而不是 display:none —— 隐藏元素选不中,复制会失败。 area.style.position = 'fixed' area.style.top = '-1000px' area.style.opacity = '0' document.body.appendChild(area) area.select() const ok = document.execCommand('copy') document.body.removeChild(area) return ok } catch { return false } }