navigator.clipboard 在非安全上下文下为 undefined, 改为优先使用 Clipboard API,不可用时回退到 execCommand。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+19
-3
@@ -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, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||
|
||||
Reference in New Issue
Block a user