mirror of
https://github.com/yaklang/yaklang-chrome-extension.git
synced 2026-09-22 03:10:43 +08:00
fix Firefox proxy set
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, {useEffect, useState} from "react";
|
import React, {useEffect, useState, useRef} from "react";
|
||||||
import {Menu} from "antd";
|
import {Menu} from "antd";
|
||||||
import {
|
import {
|
||||||
DisconnectOutlined,
|
DisconnectOutlined,
|
||||||
@@ -55,10 +55,11 @@ interface CustomProxy {
|
|||||||
|
|
||||||
export const ProxySwitch: React.FC = () => {
|
export const ProxySwitch: React.FC = () => {
|
||||||
const [initialized, setInitialized] = useState<boolean>(false);
|
const [initialized, setInitialized] = useState<boolean>(false);
|
||||||
const [currentMode, setCurrentMode] = useState<string>("");
|
const [currentMode, setCurrentMode] = useState<string>("direct"); // 默认选中直接连接
|
||||||
const [customProxies, setCustomProxies] = useState<CustomProxy[]>([]);
|
const [customProxies, setCustomProxies] = useState<CustomProxy[]>([]);
|
||||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||||
|
const loadingTimeoutRef = useRef<number | null>(null);
|
||||||
|
|
||||||
// 监听存储变化
|
// 监听存储变化
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleMessage = (message: any) => {
|
const handleMessage = (message: any) => {
|
||||||
@@ -76,17 +77,37 @@ export const ProxySwitch: React.FC = () => {
|
|||||||
browser.runtime.onMessage.addListener(handleMessage);
|
browser.runtime.onMessage.addListener(handleMessage);
|
||||||
return () => {
|
return () => {
|
||||||
browser.runtime.onMessage.removeListener(handleMessage);
|
browser.runtime.onMessage.removeListener(handleMessage);
|
||||||
|
// 清除可能存在的超时计时器
|
||||||
|
if (loadingTimeoutRef.current) {
|
||||||
|
clearTimeout(loadingTimeoutRef.current);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
let mounted = true;
|
||||||
|
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
await loadProxyStatus();
|
try {
|
||||||
await loadCustomProxies();
|
await loadProxyStatus();
|
||||||
setInitialized(true);
|
if (mounted) {
|
||||||
|
await loadCustomProxies();
|
||||||
|
setInitialized(true);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("初始化失败:", error);
|
||||||
|
if (mounted) {
|
||||||
|
setInitialized(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
mounted = false;
|
||||||
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// 获取当前代理状态
|
// 获取当前代理状态
|
||||||
@@ -100,6 +121,7 @@ export const ProxySwitch: React.FC = () => {
|
|||||||
|
|
||||||
if (response && response.success) {
|
if (response && response.success) {
|
||||||
const activeMode = response.data.mode;
|
const activeMode = response.data.mode;
|
||||||
|
console.log("获取到当前代理模式:", activeMode);
|
||||||
setCurrentMode(activeMode);
|
setCurrentMode(activeMode);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -107,8 +129,10 @@ export const ProxySwitch: React.FC = () => {
|
|||||||
// 如果后台脚本没有返回,则从存储中获取当前代理
|
// 如果后台脚本没有返回,则从存储中获取当前代理
|
||||||
const currentProxy = await getCurrentProxy();
|
const currentProxy = await getCurrentProxy();
|
||||||
if (currentProxy) {
|
if (currentProxy) {
|
||||||
|
console.log("从存储获取到当前代理:", currentProxy.id);
|
||||||
setCurrentMode(currentProxy.id);
|
setCurrentMode(currentProxy.id);
|
||||||
} else {
|
} else {
|
||||||
|
console.log("未找到当前代理,使用默认值 direct");
|
||||||
setCurrentMode("direct");
|
setCurrentMode("direct");
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -139,6 +163,7 @@ export const ProxySwitch: React.FC = () => {
|
|||||||
);
|
);
|
||||||
setCustomProxies(proxies);
|
setCustomProxies(proxies);
|
||||||
console.log("proxy_switch proxies", proxies);
|
console.log("proxy_switch proxies", proxies);
|
||||||
|
|
||||||
// 查找并设置已启用的代理
|
// 查找并设置已启用的代理
|
||||||
const enabledProxy = configs.find((proxy: ProxyConfig) => proxy.enabled);
|
const enabledProxy = configs.find((proxy: ProxyConfig) => proxy.enabled);
|
||||||
if (enabledProxy) {
|
if (enabledProxy) {
|
||||||
@@ -184,14 +209,24 @@ export const ProxySwitch: React.FC = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("proxy_switch 处理代理模式变更", mode);
|
console.log("proxy_switch 处理代理模式变更", mode);
|
||||||
|
|
||||||
// 如果当前已经是选中的模式,不做任何操作
|
// 如果当前已经是选中的模式,不做任何操作
|
||||||
if (mode === currentMode) return;
|
if (mode === currentMode) return;
|
||||||
|
|
||||||
|
// 清除之前可能存在的加载超时
|
||||||
|
if (loadingTimeoutRef.current) {
|
||||||
|
clearTimeout(loadingTimeoutRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 立即更新UI状态,避免闪烁
|
// 先更新UI,让用户感知到变化
|
||||||
setCurrentMode(mode);
|
setCurrentMode(mode);
|
||||||
// 显示加载状态但不阻塞UI
|
setIsLoading(true);
|
||||||
setTimeout(() => setIsLoading(true), 0);
|
|
||||||
|
// 设置超时保护,确保加载状态最终会被清除
|
||||||
|
loadingTimeoutRef.current = window.setTimeout(() => {
|
||||||
|
setIsLoading(false);
|
||||||
|
}, 5000); // 5秒超时保护
|
||||||
|
|
||||||
// 发送切换代理请求
|
// 发送切换代理请求
|
||||||
const response = await browser.runtime.sendMessage({
|
const response = await browser.runtime.sendMessage({
|
||||||
@@ -200,21 +235,25 @@ export const ProxySwitch: React.FC = () => {
|
|||||||
source: "proxy_switch",
|
source: "proxy_switch",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 请求完成后,清除超时保护
|
||||||
|
if (loadingTimeoutRef.current) {
|
||||||
|
clearTimeout(loadingTimeoutRef.current);
|
||||||
|
loadingTimeoutRef.current = null;
|
||||||
|
}
|
||||||
|
|
||||||
if (!response || !response.success) {
|
if (!response || !response.success) {
|
||||||
// 如果失败,恢复原状态
|
// 如果失败,恢复原状态
|
||||||
console.error("Failed to switch proxy mode");
|
console.error("Failed to switch proxy mode");
|
||||||
await loadProxyStatus(); // 重新加载正确的状态
|
await loadProxyStatus(); // 重新加载正确的状态
|
||||||
} else {
|
} else {
|
||||||
setTimeout(() => {
|
console.log("代理模式切换成功:", mode);
|
||||||
setCurrentMode((preMode) => {
|
setCurrentMode(mode);
|
||||||
return mode === preMode ? preMode : mode;
|
|
||||||
});
|
|
||||||
}, 20);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error switching to proxy mode ${mode}:`, error);
|
console.error(`Error switching to proxy mode ${mode}:`, error);
|
||||||
await loadProxyStatus(); // 出错时重新加载正确的状态
|
await loadProxyStatus(); // 出错时重新加载正确的状态
|
||||||
} finally {
|
} finally {
|
||||||
|
// 无论如何,最终要关闭加载状态
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -296,16 +335,25 @@ export const ProxySwitch: React.FC = () => {
|
|||||||
return items;
|
return items;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// 只在加载完成后渲染内容
|
||||||
|
if (!initialized) {
|
||||||
|
return <div className="proxy-switch-container loading">加载中...</div>;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="proxy-switch-container">
|
<div className="proxy-switch-container">
|
||||||
<Menu
|
<Menu
|
||||||
className="proxy-menu"
|
className="proxy-menu"
|
||||||
selectedKeys={[currentMode]}
|
selectedKeys={[currentMode]}
|
||||||
|
defaultSelectedKeys={[currentMode]}
|
||||||
items={buildMenuItems()}
|
items={buildMenuItems()}
|
||||||
onClick={({key}) => handleModeChange(key)}
|
onClick={({key}) => handleModeChange(key)}
|
||||||
key={`proxy-menu-${currentMode}`}
|
|
||||||
/>
|
/>
|
||||||
{isLoading && <div className="loading-overlay">切换中...</div>}
|
{isLoading && (
|
||||||
|
<div className="loading-overlay">
|
||||||
|
切换中...
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user