fix: 重写 AgentWizard 复制逻辑 + 手动复制文本框 fallback

This commit is contained in:
attacker committed 2026-06-18 03:39:58 +00:00
1 parent 3dec5eec2a
commit cdb978fcfc
1 file changed
+13 -37
+13 -37
View File
@@ -17,12 +17,11 @@ interface AgentWizardProps {
onOpenChange: (open: boolean) => void;
}
/** 自动检测服务器地址:使用当前浏览器访问的地址(协议+主机+端口) */
/** 自动检测服务器地址:使用当前浏览器访问的主机名,端口固定 8080 */
function detectServerAddress(): string {
if (typeof window !== 'undefined' && window.location) {
// 直接用当前浏览器的 origin(如 http://10.10.10.150),
// Nginx 会将 /agent/ 请求代理到后端
return window.location.origin;
const host = window.location.hostname;
return host ? `http://${host}:8080` : 'http://127.0.0.1:8080';
}
return 'http://127.0.0.1:8080';
}
@@ -35,6 +34,7 @@ export function AgentWizard({ open, onOpenChange }: AgentWizardProps) {
const installCommand = `curl -fsSL ${serverUrl.replace(/^http:/, 'http:')}/agent/install.sh | bash -s -- --server ${serverUrl} --token ${token}`;
// 剪贴板 fallback:execCommand('copy') 兼容 HTTP 环境 // Logan.Li
const fallbackCopy = (text: string): boolean => {
const textArea = document.createElement('textarea');
textArea.value = text;
@@ -46,29 +46,6 @@ export function AgentWizard({ open, onOpenChange }: AgentWizardProps) {
textArea.select();
let success = false;
try {
<<<<<<< HEAD
// 优先使用现代 Clipboard API(需要 HTTPS 或 localhost)
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(installCommand);
} else {
// 降级方案:用隐藏 textarea + execCommand('copy'),兼容 HTTP 环境
const textarea = document.createElement('textarea');
textarea.value = installCommand;
textarea.style.position = 'fixed';
textarea.style.opacity = '0';
textarea.style.pointerEvents = 'none';
document.body.appendChild(textarea);
textarea.select();
const ok = document.execCommand('copy');
document.body.removeChild(textarea);
if (!ok) throw new Error('execCommand failed');
}
setCopied(true);
toast.success('安装命令已复制');
setTimeout(() => setCopied(false), 2000);
} catch {
toast.error('复制失败,请手动选中复制');
=======
success = document.execCommand('copy');
} catch {
// execCommand also failed
@@ -78,20 +55,21 @@ export function AgentWizard({ open, onOpenChange }: AgentWizardProps) {
};
const handleCopy = async () => {
const cmd = installCommand;
let success = false;
// 方式1: Clipboard API(HTTPS / localhost)
// 方式1: Clipboard API(HTTPS / localhost 可用)
try {
await navigator.clipboard.writeText(cmd);
success = true;
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(installCommand);
success = true;
}
} catch {
// Clipboard API 不可用
// Clipboard API 不可用,尝试 fallback
}
// 方式2: execCommand fallback(HTTP 也可用)
// 方式2: execCommand fallback(HTTP 环境也可用)
if (!success) {
success = fallbackCopy(cmd);
success = fallbackCopy(installCommand);
}
if (success) {
@@ -99,10 +77,8 @@ export function AgentWizard({ open, onOpenChange }: AgentWizardProps) {
toast.success('安装命令已复制到剪贴板,粘贴到目标主机执行');
setTimeout(() => setCopied(false), 3000);
} else {
toast.error('自动复制失败,请在下方代码框中手动全选复制 (Ctrl+A → Ctrl+C)');
// 切换到手选模式,展示可编辑文本框
toast.error('自动复制失败,请在下方手动全选复制 (Ctrl+A → Ctrl+C)');
setShowManualInput(true);
>>>>>>> ca18ead (feat: run.sh四模式(dev/docker/build/binary) + CLI flags + Redka内嵌Redis + Agent复制修复 + 服务器编辑IP字段 + 安装Agent按钮)
}
};