From feea35da4ccd1be0aab7bf12c8e0c5676762c5b1 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Sat, 19 Sep 2026 16:30:11 +0800 Subject: [PATCH] fix(webui): copy and export files on Safari LAN HTTP Clipboard APIs and instant blob downloads fail on insecure origins and WebKit. Route every export through the shared helpers. --- .../components/config/node-edit-dialog.tsx | 34 +--------------- .../src/components/debug/live-activity.tsx | 17 +++----- .../webui/src/components/pages/logs-page.tsx | 19 ++------- .../src/components/pages/settings-page.tsx | 29 ++++++-------- .../src/components/settings/system-panel.tsx | 13 +++--- .../src/components/settings/totp-panel.tsx | 19 +++++++++ .../settings/totp-recovery-codes.tsx | 40 ++++++++----------- .../webui/src/components/ui/json-tree.tsx | 20 ++-------- .../src/contexts/ActionFeedbackContext.tsx | 5 ++- 9 files changed, 70 insertions(+), 126 deletions(-) diff --git a/packages/webui/src/components/config/node-edit-dialog.tsx b/packages/webui/src/components/config/node-edit-dialog.tsx index 9e9b80b1..ac0ae225 100644 --- a/packages/webui/src/components/config/node-edit-dialog.tsx +++ b/packages/webui/src/components/config/node-edit-dialog.tsx @@ -27,7 +27,7 @@ import { DropdownSelect, type DropdownOption } from '@/components/ui/dropdown-se import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { ToggleSwitch } from '@/components/ui/toggle-switch'; -import { cn } from '@/lib/utils'; +import { cn, copyText } from '@/lib/utils'; import { accessTokenFeedback, type AccessTokenFeedback, @@ -413,27 +413,6 @@ interface TokenFieldProps { feedback?: AccessTokenFeedback; } -/** Insecure-context clipboard fallback. Returns true on success. - * Cast through a local alias so ts(6387) doesn't flag the call site — - * the deprecation tag is on the live signature and we know we're using - * the still-supported legacy form on purpose. */ -function legacyCopyToClipboard(value: string): boolean { - try { - const ta = document.createElement('textarea'); - ta.value = value; - ta.style.position = 'fixed'; - ta.style.opacity = '0'; - document.body.appendChild(ta); - ta.select(); - const exec = (document as unknown as { execCommand(cmd: string): boolean }).execCommand; - const ok = exec.call(document, 'copy'); - document.body.removeChild(ta); - return ok; - } catch { - return false; - } -} - /** * Password-style input for the access token. Hidden by default to keep * the value out of over-the-shoulder reads / screenshots; an eye toggle @@ -448,16 +427,7 @@ function TokenField({ label, value, onChange, onGenerate, placeholder, feedback const handleCopy = async () => { if (!value) return; - try { - await navigator.clipboard.writeText(value); - } catch { - // navigator.clipboard is undefined on insecure origins (LAN HTTP - // outside localhost). Fall back to a transient textarea + - // document.execCommand('copy'). The latter is deprecated but every - // current browser still honours it and there is no modern - // replacement for non-secure-context clipboard writes. - if (!legacyCopyToClipboard(value)) return; - } + if (!await copyText(value)) return; setCopied(true); if (copiedTimerRef.current != null) window.clearTimeout(copiedTimerRef.current); copiedTimerRef.current = window.setTimeout(() => { diff --git a/packages/webui/src/components/debug/live-activity.tsx b/packages/webui/src/components/debug/live-activity.tsx index 3bcf16c0..9fb09239 100644 --- a/packages/webui/src/components/debug/live-activity.tsx +++ b/packages/webui/src/components/debug/live-activity.tsx @@ -8,7 +8,7 @@ import { Activity, ChevronRight, Download, Pause, Play, RadioTower, Search, Tras import { JsonTree } from '@/components/ui/json-tree'; import { Segmented } from '@/components/debug/segmented'; import { useApi } from '@/lib/api'; -import { cn } from '@/lib/utils'; +import { cn, downloadBlob } from '@/lib/utils'; import type { DebugStreamMessage } from '@/types'; const STREAM_CAP = 300; @@ -133,17 +133,10 @@ export function LiveActivity() { }, [rows, kindFilter, query]); const exportJson = () => { - const blob = new Blob([JSON.stringify(visible.map((r) => ({ at: r.at, ...r.msg })), null, 2)], { type: 'application/json' }); - const url = URL.createObjectURL(blob); - const a = document.createElement('a'); - a.href = url; - a.download = `debug-activity-${visible.length}.json`; - document.body.appendChild(a); - a.click(); - a.remove(); - // Revoke after the download has had a chance to start (immediate revoke - // cancels it in some browsers). - setTimeout(() => URL.revokeObjectURL(url), 1000); + downloadBlob( + new Blob([JSON.stringify(visible.map((r) => ({ at: r.at, ...r.msg })), null, 2)], { type: 'application/json' }), + `debug-activity-${visible.length}.json`, + ); }; return ( diff --git a/packages/webui/src/components/pages/logs-page.tsx b/packages/webui/src/components/pages/logs-page.tsx index 77f62cb2..76e0734a 100644 --- a/packages/webui/src/components/pages/logs-page.tsx +++ b/packages/webui/src/components/pages/logs-page.tsx @@ -10,7 +10,7 @@ import { ScrollArea } from '@/components/ui/scroll-area'; import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'; import { ConfirmDialog } from '@/components/confirm-dialog'; import { SkeletonSwap } from '@/components/interior/skeleton-swap'; -import { cn } from '@/lib/utils'; +import { cn, downloadBlob } from '@/lib/utils'; import type { LogEntry, LogLevel, LogsPreset, UiHighlightRule } from '@/types'; import { useApi } from '@/lib/api'; import { @@ -370,13 +370,8 @@ export function LogsPage() { l.line || `${l.time} ${l.level.toUpperCase().padEnd(7)} [${l.scope}] ${l.message}`, )); const blob = new Blob([lines.join('\n') + '\n'], { type: 'text/plain;charset=utf-8' }); - const url = URL.createObjectURL(blob); - const a = document.createElement('a'); const ts = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19); - a.href = url; - a.download = `snowluma-logs-${ts}.log`; - a.click(); - URL.revokeObjectURL(url); + downloadBlob(blob, `snowluma-logs-${ts}.log`); }, [filtered]); const exportFullTrace = useCallback(async () => { @@ -392,13 +387,7 @@ export function LogsPage() { }, () => api.logs.exportTrace(), ); - const blob = new Blob([text], { type: 'text/plain;charset=utf-8' }); - const url = URL.createObjectURL(blob); - const a = document.createElement('a'); - a.href = url; - a.download = filename; - a.click(); - URL.revokeObjectURL(url); + downloadBlob(new Blob([text], { type: 'text/plain;charset=utf-8' }), filename); } catch (err) { console.error('exportTrace', err); } finally { @@ -421,7 +410,7 @@ export function LogsPage() { const levelsFiltered = enabled.size < LEVELS.length; return ( - + {/* ── Toolbar ─────────────────────────────────────────────── */}
diff --git a/packages/webui/src/components/pages/settings-page.tsx b/packages/webui/src/components/pages/settings-page.tsx index 45be265b..2e5cf90d 100644 --- a/packages/webui/src/components/pages/settings-page.tsx +++ b/packages/webui/src/components/pages/settings-page.tsx @@ -44,7 +44,7 @@ import { SkeletonSwap } from '@/components/interior/skeleton-swap'; import { SegmentedControl } from '@/components/interior/segmented-control'; import { useApi } from '@/lib/api'; import { useAppState } from '@/contexts/AppStateContext'; -import { cn } from '@/lib/utils'; +import { cn, downloadBlob, toColorInputValue } from '@/lib/utils'; import { settingsRoute, type SettingsTab } from '@/router'; import { NotificationsPanel } from '@/components/settings/notifications-panel'; import { GlobalConfigPanel } from '@/components/settings/global-config-panel'; @@ -138,7 +138,7 @@ function SettingsNav({ tab, onChange }: { tab: SettingsTab; onChange: (t: Settin aria-label="设置导航" className={cn( 'flex flex-wrap gap-1 rounded-xl border bg-card p-1.5', - 'lg:w-52 lg:shrink-0 lg:flex-col lg:flex-nowrap lg:self-start lg:sticky lg:top-4 lg:max-h-[calc(100vh-6rem)] lg:overflow-y-auto', + 'lg:w-52 lg:shrink-0 lg:flex-col lg:flex-nowrap lg:self-start lg:sticky lg:top-4 lg:max-h-[calc(100dvh-6rem)] lg:overflow-y-auto lg:overscroll-y-contain', )} > {TAB_GROUPS.map((group, gi) => ( @@ -476,12 +476,12 @@ function AppearancePanel() { > pickCustomAccent(e.target.value)} className="absolute inset-0 cursor-pointer opacity-0" aria-label="自定义强调色" /> - {a.accentMode === 'custom' && } + {a.accentMode === 'custom' && }
@@ -636,7 +636,7 @@ function BackgroundCard({