/** * 复制文本。 * * `navigator.clipboard` 只在**安全上下文**(https 或 localhost)里有,内网生产入口是 * 纯 http,那里它是 undefined——直接调会抛错,用户看到的却是"浏览器不允许自动复制"。 * 所以这里退回 `document.execCommand("copy")`:老,但在 http 下确实能用。 */ export async function copyText(text: string): Promise { if (window.isSecureContext && navigator.clipboard?.writeText) { try { await navigator.clipboard.writeText(text); return true; } catch { // 权限被拒也走兜底,别直接判定失败 } } const area = document.createElement("textarea"); area.value = text; area.setAttribute("readonly", ""); area.style.position = "fixed"; area.style.top = "-1000px"; area.style.opacity = "0"; document.body.appendChild(area); area.select(); let ok = false; try { ok = document.execCommand("copy"); } catch { ok = false; } document.body.removeChild(area); return ok; }