From ba899782fe8478afbb5d2fb1660fcf2638fcd245 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 18 May 2026 14:47:33 +0800 Subject: [PATCH] feat(webui): mask the access token and add copy + visibility buttons The adapter edit dialog now ships a dedicated TokenField for the access-token slot instead of reusing the plain text Field. It: - defaults to a password-style input so the value stays out of casual screen reads / screenshots, - exposes an Eye / EyeOff button that flips the input type rather than masking the string, so paste / autocomplete still behave like a native password field, - exposes a Copy button that pushes the current value to navigator.clipboard, flashing a check icon for ~1.4 s on success, and disables itself when the value is empty, - falls back to a transient textarea + document.execCommand('copy') for non-secure-context environments (LAN HTTP outside localhost), routed through a local alias so the deprecation warning on the live signature doesn't fire at the call site. --- .../components/config/node-edit-dialog.tsx | 105 +++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/packages/webui/src/components/config/node-edit-dialog.tsx b/packages/webui/src/components/config/node-edit-dialog.tsx index ac6b0b63..39ff38de 100644 --- a/packages/webui/src/components/config/node-edit-dialog.tsx +++ b/packages/webui/src/components/config/node-edit-dialog.tsx @@ -3,7 +3,8 @@ // throw away changes). Validation is minimal — blank-name and dup-name // disable the save button; everything else is best-effort coercion. -import { useState } from 'react'; +import { useRef, useState } from 'react'; +import { Check, Copy, Eye, EyeOff } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Dialog, @@ -80,7 +81,7 @@ export function NodeEditDialog(props: NodeEditDialogProps - void; + placeholder?: string; +} + +/** 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 + * unmasks it and a copy button shoves the current value to the system + * clipboard with a short "已复制" confirmation flash. + */ +function TokenField({ label, value, onChange, placeholder }: TokenFieldProps) { + const [visible, setVisible] = useState(false); + const [copied, setCopied] = useState(false); + const copiedTimerRef = useRef(null); + + 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; + } + setCopied(true); + if (copiedTimerRef.current != null) window.clearTimeout(copiedTimerRef.current); + copiedTimerRef.current = window.setTimeout(() => { + setCopied(false); + copiedTimerRef.current = null; + }, 1400); + }; + + return ( +
+ +
+ onChange(e.target.value)} + placeholder={placeholder} + autoComplete="off" + spellCheck={false} + className="flex-1 font-mono" + /> + + +
+
+ ); +} + interface SegmentedOption { value: T; label: string;