From ae267a2ff6a5f6f3466eb27baf2400fe56ef666e Mon Sep 17 00:00:00 2001 From: kicer Date: Wed, 2 Sep 2026 19:55:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(clipboard):=20=E9=99=8D=E7=BA=A7=E5=A4=8D?= =?UTF-8?q?=E5=88=B6=E6=94=B9=20readonly+=E4=B8=8Dfocus,=E9=81=BF=E5=85=8D?= =?UTF-8?q?Android=E5=BC=B9=E8=BD=AF=E9=94=AE=E7=9B=98=E9=97=AA=E7=83=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit navigator.clipboard.writeText 在 Android 偶发 reject(Document is not focused),\n落入 textarea 降级。原降级用 ta.focus() 会唤起软键盘,键盘弹起/收回导致\n视口高度抖动=底部闪烁。改为 readonly textarea + 只 select 不 focus,\n能 execCommand copy 但不弹键盘。 --- app/server/public/js/core.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/server/public/js/core.js b/app/server/public/js/core.js index 50e1469..79a88d9 100644 --- a/app/server/public/js/core.js +++ b/app/server/public/js/core.js @@ -67,7 +67,8 @@ function fmtTime(ts) { // 跨天带日期,跨年再带年份 return (d.getFullYear() === now.getFullYear() ? md : d.getFullYear() + '/' + md) + ' ' + hm; } -// 剪贴板:WebView/局域网 http 非安全上下文没有 navigator.clipboard,降级 execCommand +// 剪贴板:优先 navigator.clipboard(https/localhost),失败时降级 execCommand。 +// 降级用 readonly textarea + 只 select 不 focus:不选焦点,手机上不弹软键盘。 async function copyText(text) { try { if (navigator.clipboard && window.isSecureContext) { await navigator.clipboard.writeText(text); return true; } @@ -75,8 +76,12 @@ async function copyText(text) { try { const ta = document.createElement('textarea'); ta.value = text; - ta.style.position = 'fixed'; ta.style.opacity = '0'; - document.body.appendChild(ta); ta.focus(); ta.select(); + ta.setAttribute('readonly', ''); + ta.style.position = 'fixed'; ta.style.top = '0'; ta.style.left = '-9999px'; + ta.style.opacity = '0'; + document.body.appendChild(ta); + ta.select(); + ta.setSelectionRange(0, ta.value.length); const ok = document.execCommand('copy'); document.body.removeChild(ta); return ok;