fix(clipboard): 降级复制改 readonly+不focus,避免Android弹软键盘闪烁
navigator.clipboard.writeText 在 Android 偶发 reject(Document is not focused),\n落入 textarea 降级。原降级用 ta.focus() 会唤起软键盘,键盘弹起/收回导致\n视口高度抖动=底部闪烁。改为 readonly textarea + 只 select 不 focus,\n能 execCommand copy 但不弹键盘。
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user