console.log("Content script starting..."); // 代理操作类型常量 const ProxyActionType = { SET_PROXY_CONFIG: 'SET_PROXY_CONFIG', CLEAR_PROXY_CONFIG: 'CLEAR_PROXY_CONFIG', GET_PROXY_STATUS: 'GET_PROXY_STATUS', GET_PROXY_CONFIGS: 'GET_PROXY_CONFIGS', UPDATE_PROXY_CONFIG: 'UPDATE_PROXY_CONFIG' }; // 在文件顶部添加常量声明 const YAK_ICON_URL = chrome.runtime.getURL('/images/yak.svg'); // 添加一个通用的消息发送函数 async function sendMessageWithRetry(message, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { return await chrome.runtime.sendMessage(message); } catch (error) { console.warn(`Attempt ${i + 1} failed:`, error); if (i === maxRetries - 1) { throw error; } // 等待一小段时间后重试 await new Promise(resolve => setTimeout(resolve, 500)); } } } // 获取当前代理状态 async function getCurrentProxy() { try { const response = await sendMessageWithRetry({ action: ProxyActionType.GET_PROXY_STATUS }); if (!response || !response.success) { console.log('No valid response from background script'); return { enable: false, proxy: '', currentMode: 'direct' }; } const status = response.data; console.log('Proxy status from background:', status); // 返回当前模式 return { enable: status.enabled, proxy: status.mode === 'system' ? 'system' : '', currentMode: status.mode || 'direct' // 使用 mode 来判断当前激活的代理 }; } catch (error) { console.error('Error getting proxy status:', error); return { enable: false, proxy: '', currentMode: 'direct' }; } } // 获取所有代理配置 async function getProxyConfigs() { try { const response = await sendMessageWithRetry({ action: ProxyActionType.GET_PROXY_CONFIGS }); if (!response) { console.log('No response from background script'); return []; } if (!response.success) { console.error('Error in response:', response.error); return []; } return response.data || []; } catch (error) { console.error('Error getting proxy configs:', error); return []; } } // 切换代理 async function switchProxy(config) { try { console.log('Switching proxy:', config); // 根据配置类型构建正确的配置对象 let proxyConfig; if (config.proxyType === 'system') { proxyConfig = { id: 'system', name: '[系统代理]', proxyType: 'system', enabled: true }; } else if (config.proxyType === 'direct') { proxyConfig = { id: 'direct', name: '[直接连接]', proxyType: 'direct', enabled: false }; } else { proxyConfig = { ...config, enabled: true }; } await sendMessageWithRetry({ action: ProxyActionType.SET_PROXY_CONFIG, config: proxyConfig }); await PanelManager.updatePanel(); } catch (error) { console.error('Error switching proxy:', error); } } // 清除代理 async function clearProxy() { try { const response = await sendMessageWithRetry({ action: ProxyActionType.SET_PROXY_CONFIG, config: { id: 'direct', name: '[直接连接]', proxyType: 'direct', enabled: false } }); console.log('Clear proxy response:', response); // 使用 PanelManager 的 updatePanel 方法 await PanelManager.updatePanel(); } catch (error) { console.error('Error clearing proxy:', error); } } // 修改 PanelManager const PanelManager = { panel: null, messageListener: null, _updating: false, _updateQueue: Promise.resolve(), _currentState: null, // 用于跟踪当前状态 _lastUpdate: null, // 添加最后更新时间戳 _lastState: null, _pollingInterval: null, init() { // 确保 document.body 存在 if (!document.body) { console.log('Body not ready, waiting...'); this.waitForBody(); return; } if (this.panel) { console.log('Panel already exists, updating...'); this.updatePanel(); return; } console.log('Creating new panel...'); this.createPanel(); }, // 添加等待 body 的方法 waitForBody() { if (document.body) { this.init(); return; } console.log('Setting up MutationObserver for body'); const observer = new MutationObserver((mutations, obs) => { if (document.body) { console.log('Body found via observer'); obs.disconnect(); this.init(); } }); observer.observe(document.documentElement, { childList: true, subtree: true }); }, createPanel() { if (!document.body) { console.log('Body not available during panel creation'); return; } // 创建容器 const container = document.createElement('div'); container.id = 'yakit-proxy-panel'; // 创建 shadow DOM const shadow = container.attachShadow({ mode: 'open' }); // 添加样式 const style = document.createElement('style'); style.textContent = ` .floating-panel { position: fixed; top: 20px; right: 0; width: 180px; background: white; border-radius: 8px 0 0 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); z-index: 2147483647; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; transition: transform 0.3s ease; } .floating-panel.collapsed { transform: translateX(100%); } .panel-header { padding: 4px; border-bottom: 1px solid #eee; border-radius: 8px 0 0 0; background: #f8f9fa; display: flex; justify-content: space-between; align-items: center; } .collapse-trigger { position: absolute; left: -20px; top: 0; width: 20px; height: 100%; background: #f8f9fa; border-radius: 8px 0 0 8px; cursor: pointer; display: flex; align-items: center; justify-content: center; box-shadow: -2px 0 5px rgba(0,0,0,0.1); } .collapse-trigger:hover { background: #e9ecef; } .collapse-trigger::after { content: '◀'; transition: transform 0.3s ease; } .floating-panel.collapsed .collapse-trigger::after { transform: rotate(180deg); } .panel-content { padding: 4px; } .proxy-item { position: relative; display: flex; align-items: center; padding: 4px 10px; cursor: pointer; transition: all 0.2s; color: #666; border-left: 3px solid transparent; overflow: hidden; } .proxy-item > span { position: relative; z-index: 1; } .watermark-icon { position: absolute; right: 0; width: 100%; height: 100%; opacity: 0.3; pointer-events: none; display: none; object-fit: contain; object-position: right center; padding: 4px; } .proxy-item.active .watermark-icon { display: block; } .proxy-item.active { color: #ff6b00 !important; background: #fff7e6; border-left: 3px solid #ff6b00; } .proxy-item.active span:first-child { color: #ff6b00 !important; } .proxy-item span:first-child { margin-right: 8px; font-size: 16px; color: #666; transition: color 0.2s; } .proxy-item:hover { background: #f5f5f5; } .proxy-item:hover span:first-child { color: #ff6b00; } .add-proxy { display: flex; align-items: center; padding: 4px 10px; color: #1890ff; cursor: pointer; border-top: 1px solid #eee; transition: all 0.2s; } .add-proxy:hover { background: #f5f5f5; } .settings { padding: 4px 10px; color: #666; cursor: pointer; border-top: 1px solid #eee; transition: all 0.2s; } .settings:hover { background: #f5f5f5; } .panel-header .header-content { display: flex; align-items: center; gap: 8px; padding: 0 4px; } .yak-icon { width: 24px; height: 24px; object-fit: contain; } .divider { height: 1px; background-color: #eee; margin: 4px 0; } `; // 创建面板内容 const panel = document.createElement('div'); panel.className = 'floating-panel'; panel.innerHTML = `