fix: 修复复制按钮在非 HTTPS 环境下报错的问题
Deploy Examination / deploy (push) Successful in 43s

navigator.clipboard 在非安全上下文下为 undefined,
改为优先使用 Clipboard API,不可用时回退到 execCommand。

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
2026-09-02 21:49:29 +08:00
parent df63692a18
commit b3075350ff
+19 -3
View File
@@ -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) {
btn.textContent = '✅ 已复制';
btn.classList.add('copied');
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 ───
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) {
if (!s) return '';
return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');