mirror of
https://github.com/yaklang/yaklang-chrome-extension.git
synced 2026-09-26 13:11:53 +08:00
fix popup ui
This commit is contained in:
+28
-22
@@ -1,6 +1,6 @@
|
||||
import { browser } from 'wxt/browser';
|
||||
import type { ProxyConfig } from '../types/proxy';
|
||||
import { getAllProxyConfigs, getProxyConfig, enableProxyConfig, disableAllProxies } from './storage';
|
||||
import { getAllProxyConfigs, getProxyConfig, enableProxyConfig, disableAllProxies, getCurrentProxy, setCurrentProxy } from './storage';
|
||||
|
||||
/**
|
||||
* 获取当前激活的代理模式
|
||||
@@ -8,10 +8,19 @@ import { getAllProxyConfigs, getProxyConfig, enableProxyConfig, disableAllProxie
|
||||
*/
|
||||
export async function getCurrentProxyMode(): Promise<string> {
|
||||
try {
|
||||
// 首先尝试从当前代理存储中获取
|
||||
const currentProxyConfig = await getCurrentProxy();
|
||||
if (currentProxyConfig) {
|
||||
return currentProxyConfig.id;
|
||||
}
|
||||
|
||||
// 如果没有当前代理记录,则从配置列表查找已启用的代理
|
||||
const configs = await getAllProxyConfigs();
|
||||
const enabledProxy = configs.find(config => config.enabled);
|
||||
|
||||
if (enabledProxy) {
|
||||
// 如果找到已启用的代理,更新当前代理存储
|
||||
await setCurrentProxy(enabledProxy);
|
||||
return enabledProxy.id;
|
||||
}
|
||||
|
||||
@@ -30,23 +39,6 @@ export async function getCurrentProxyMode(): Promise<string> {
|
||||
*/
|
||||
export async function switchProxyMode(mode: string): Promise<boolean> {
|
||||
try {
|
||||
if (mode === 'direct') {
|
||||
// 清除所有代理设置
|
||||
await disableAllProxies();
|
||||
await browser.proxy.settings.clear({});
|
||||
return true;
|
||||
}
|
||||
|
||||
if (mode === 'system') {
|
||||
// 使用系统代理
|
||||
await disableAllProxies();
|
||||
await browser.proxy.settings.set({
|
||||
value: { mode: 'system' },
|
||||
scope: 'regular'
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
// 使用自定义代理
|
||||
const config = await getProxyConfig(mode);
|
||||
if (!config) {
|
||||
@@ -59,14 +51,16 @@ export async function switchProxyMode(mode: string): Promise<boolean> {
|
||||
|
||||
// 设置代理
|
||||
if (config.proxyType === 'direct') {
|
||||
// 直接连接模式
|
||||
await browser.proxy.settings.clear({});
|
||||
} else if (config.proxyType === 'system') {
|
||||
// 系统代理模式
|
||||
await browser.proxy.settings.set({
|
||||
value: { mode: 'system' },
|
||||
scope: 'regular'
|
||||
});
|
||||
} else {
|
||||
// 构建代理配置
|
||||
} else if (config.proxyType === 'fixed_servers') {
|
||||
// 固定服务器代理
|
||||
const proxyConfig = {
|
||||
mode: 'fixed_servers',
|
||||
rules: {}
|
||||
@@ -74,7 +68,7 @@ export async function switchProxyMode(mode: string): Promise<boolean> {
|
||||
|
||||
// 添加代理规则
|
||||
const proxyRule = {
|
||||
scheme: config.proxyType,
|
||||
scheme: config.scheme,
|
||||
host: config.host || '',
|
||||
port: config.port || 80
|
||||
};
|
||||
@@ -88,7 +82,7 @@ export async function switchProxyMode(mode: string): Promise<boolean> {
|
||||
// 设置代理规则
|
||||
proxyConfig.rules = {
|
||||
singleProxy: proxyRule,
|
||||
bypassList: ['localhost', '127.0.0.1']
|
||||
bypassList: config.bypassList || ['localhost', '127.0.0.1']
|
||||
};
|
||||
|
||||
// 应用代理设置
|
||||
@@ -96,6 +90,18 @@ export async function switchProxyMode(mode: string): Promise<boolean> {
|
||||
value: proxyConfig,
|
||||
scope: 'regular'
|
||||
});
|
||||
} else if (config.proxyType === 'pac_script') {
|
||||
// PAC脚本代理
|
||||
const pacConfig = {
|
||||
mode: 'pac_script',
|
||||
pacScript: config.pacScript
|
||||
};
|
||||
|
||||
// 应用PAC脚本设置
|
||||
await browser.proxy.settings.set({
|
||||
value: pacConfig,
|
||||
scope: 'regular'
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user