navigator.clipboard 在非安全上下文下为 undefined, 改为优先使用 Clipboard API,不可用时回退到 execCommand。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+18
-2
@@ -1075,17 +1075,33 @@ function copyQuestionPrompt(qId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
navigator.clipboard.writeText(prompt).then(() => {
|
|
||||||
const btn = document.querySelector(`[data-copy="${qId}"]`);
|
const btn = document.querySelector(`[data-copy="${qId}"]`);
|
||||||
|
const showCopied = () => {
|
||||||
if (btn) {
|
if (btn) {
|
||||||
btn.textContent = '✅ 已复制';
|
btn.textContent = '✅ 已复制';
|
||||||
btn.classList.add('copied');
|
btn.classList.add('copied');
|
||||||
setTimeout(() => { btn.textContent = '📋 复制题目'; btn.classList.remove('copied'); }, 1500);
|
setTimeout(() => { btn.textContent = '📋 复制题目'; btn.classList.remove('copied'); }, 1500);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||||
|
navigator.clipboard.writeText(prompt).then(showCopied).catch(() => fallbackCopy(prompt, showCopied));
|
||||||
|
} else {
|
||||||
|
fallbackCopy(prompt, showCopied);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Util ───
|
// ─── Util ───
|
||||||
|
function fallbackCopy(text, onSuccess) {
|
||||||
|
const ta = document.createElement('textarea');
|
||||||
|
ta.value = text;
|
||||||
|
ta.style.cssText = 'position:fixed;left:-9999px;opacity:0';
|
||||||
|
document.body.appendChild(ta);
|
||||||
|
ta.select();
|
||||||
|
try { document.execCommand('copy'); onSuccess(); } catch (_) {}
|
||||||
|
document.body.removeChild(ta);
|
||||||
|
}
|
||||||
|
|
||||||
function escapeHtml(s) {
|
function escapeHtml(s) {
|
||||||
if (!s) return '';
|
if (!s) return '';
|
||||||
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||||
|
|||||||
Reference in New Issue
Block a user