From 0a68632c8aa542b4c8ae9ab03dbf5ca9221e736c Mon Sep 17 00:00:00 2001 From: yml2213 Date: Tue, 23 Jun 2026 21:12:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=BF=9C=E7=A8=8Bhttp=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=97=B6=E5=A4=8D=E5=88=B6Cookie=E5=A4=B1=E8=B4=A5=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0execCommand=20fallback=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E9=9D=9E=E5=AE=89=E5=85=A8=E4=B8=8A=E4=B8=8B=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/frontend/src/pages/CookiePage.tsx | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/web/frontend/src/pages/CookiePage.tsx b/web/frontend/src/pages/CookiePage.tsx index c49a788..0becb5e 100644 --- a/web/frontend/src/pages/CookiePage.tsx +++ b/web/frontend/src/pages/CookiePage.tsx @@ -8,6 +8,30 @@ import { getErrorMessage } from '../utils/error'; const { Text } = Typography; +/** 复制文本到剪贴板,兼容非安全上下文(http://非localhost) */ +function copyToClipboard(text: string): Promise { + if (navigator.clipboard?.writeText) { + return navigator.clipboard.writeText(text); + } + // fallback:利用 textarea + execCommand,兼容 http 远程访问 + return new Promise((resolve, reject) => { + const ta = document.createElement('textarea'); + ta.value = text; + ta.style.position = 'fixed'; + ta.style.left = '-9999px'; + document.body.appendChild(ta); + ta.select(); + try { + const ok = document.execCommand('copy'); + document.body.removeChild(ta); + ok ? resolve() : reject(new Error('execCommand copy failed')); + } catch (e) { + document.body.removeChild(ta); + reject(e); + } + }); +} + export default function CookiePage() { const { token } = theme.useToken(); const [cookies, setCookies] = useState([]); @@ -55,7 +79,7 @@ export default function CookiePage() { message.warning('Cookie 为空'); return; } - navigator.clipboard.writeText(cookie).then(() => { + copyToClipboard(cookie).then(() => { message.success(`已复制 ${username} 的 Cookie`); }).catch(() => { message.error('复制失败'); @@ -71,7 +95,7 @@ export default function CookiePage() { const text = selected .map((c) => `${c.account_username}: ${c.cookie || '(空)'}`) .join('\n'); - navigator.clipboard.writeText(text).then(() => { + copyToClipboard(text).then(() => { message.success(`已复制 ${selected.length} 条 Cookie`); }).catch(() => { message.error('复制失败');