From b3075350ff009e089f8c859c4df3e501036a1162 Mon Sep 17 00:00:00 2001 From: wonder Date: Wed, 2 Sep 2026 21:49:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E5=9C=A8=E9=9D=9E=20HTTPS=20=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E4=B8=8B=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit navigator.clipboard 在非安全上下文下为 undefined, 改为优先使用 Clipboard API,不可用时回退到 execCommand。 Co-Authored-By: Claude Code --- index.html | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 40edfc1..e3b90c6 100644 --- a/index.html +++ b/index.html @@ -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, '"');