update telegram proxy
This commit is contained in:
		| @ -1,6 +1,39 @@ | ||||
| const ipc = window.electronAPI; | ||||
| let userInfo = {}; | ||||
| let trcConfig = {} | ||||
|  | ||||
| // 安全的 localStorage 访问工具 | ||||
| const safeLocalStorage = { | ||||
|   getItem: (key) => { | ||||
|     try { | ||||
|       return localStorage.getItem(key); | ||||
|     } catch (e) { | ||||
|       console.warn(`无法访问 localStorage.${key}:`, e.message); | ||||
|       return null; | ||||
|     } | ||||
|   }, | ||||
|   setItem: (key, value) => { | ||||
|     try { | ||||
|       localStorage.setItem(key, value); | ||||
|       return true; | ||||
|     } catch (e) { | ||||
|       console.warn(`无法设置 localStorage.${key}:`, e.message); | ||||
|       return false; | ||||
|     } | ||||
|   } | ||||
| }; | ||||
|  | ||||
| // 监听翻译配置更新事件 | ||||
| window.addEventListener("translateConfigChanged", function (event) { | ||||
|   console.log("📝 Telegram: 收到翻译配置更新事件", event.detail); | ||||
|   // 立即更新配置 | ||||
|   updateConfigInfo().then(() => { | ||||
|     console.log("✅ Telegram: 配置更新完成", { | ||||
|       sendTranslateStatus: trcConfig.sendTranslateStatus, | ||||
|       timestamp: new Date().toISOString() | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
| //========================用户基本信息获取开始============ | ||||
| const getCurrentUserId = (item) => { | ||||
|     if (!item){ | ||||
| @ -18,7 +51,9 @@ const getCurrentUserId = (item) => { | ||||
| } | ||||
| const onlineStatusCheck = ()=> { | ||||
|     setInterval(async () => { | ||||
|         const element = localStorage.dc1_auth_key; | ||||
|         // 安全地检查 localStorage | ||||
|         const element = safeLocalStorage.getItem('dc1_auth_key'); | ||||
|  | ||||
|         const main = document.getElementById('Main'); | ||||
|         const {avatarUrl,nickName,phoneNumber,userName} = await getUserInfo(); | ||||
|         if (element || main) { | ||||
| @ -54,7 +89,7 @@ const getUserInfo = () => { | ||||
|         // 统一获取用户ID的逻辑(避免重复) | ||||
|         const getUserId = () => { | ||||
|             try { | ||||
|                 const userAuthString = localStorage.getItem("user_auth"); | ||||
|                 const userAuthString = safeLocalStorage.getItem("user_auth"); | ||||
|                 if (!userAuthString) return null; | ||||
|                 return JSON.parse(userAuthString).id; | ||||
|             } catch (error) { | ||||
| @ -934,15 +969,58 @@ function calculateTgUnreadCount() { | ||||
|  * 初始化 DOM 监听器。 | ||||
|  */ | ||||
| const initTgObserver = () => { | ||||
|     const observeTarget = document.querySelector('.chat-list'); | ||||
|     console.log("tg Web: 尝试初始化 聊天框 监听器...");  | ||||
|     // 检查页面基本状态 | ||||
|     console.log("tg Web: 尝试初始化 聊天框 监听器..."); | ||||
|     console.log("页面状态:", { | ||||
|         url: window.location.href, | ||||
|         title: document.title, | ||||
|         readyState: document.readyState | ||||
|     }); | ||||
|  | ||||
|     // 检查多个可能的目标元素 | ||||
|     const targets = [ | ||||
|         '.chat-list', | ||||
|         '#LeftColumn-main', | ||||
|         '.chatlist-container', | ||||
|         '.chat-folders-container', | ||||
|         '.left-column', | ||||
|         '[data-testid="chat-list"]' | ||||
|     ]; | ||||
|  | ||||
|     let observeTarget = null; | ||||
|     let foundSelector = null; | ||||
|  | ||||
|     for (const selector of targets) { | ||||
|         observeTarget = document.querySelector(selector); | ||||
|         if (observeTarget) { | ||||
|             foundSelector = selector; | ||||
|             break; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     if (!observeTarget) { | ||||
|         // console.log("tg Web: 文档body尚未加载,1秒后重试初始化..."); | ||||
|         // 检查页面是否是 Telegram | ||||
|         if (!window.location.href.includes('telegram.org')) { | ||||
|             console.error("tg Web: 当前页面不是 Telegram,停止初始化"); | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         // 检查页面是否有基本的 Telegram 元素 | ||||
|         const hasBasicElements = document.querySelector('#root') || | ||||
|                                 document.querySelector('.app') || | ||||
|                                 document.querySelector('[class*="telegram"]'); | ||||
|  | ||||
|         if (!hasBasicElements) { | ||||
|             console.warn("tg Web: 页面可能未正常加载,检查网络连接"); | ||||
|         } | ||||
|  | ||||
|         console.log("tg Web: 等待页面元素加载,1秒后重试..."); | ||||
|         setTimeout(initTgObserver, 1000); | ||||
|         return; | ||||
|     } | ||||
|  | ||||
|     console.log(`tg Web: ✅ 找到目标元素 ${foundSelector},开始初始化监听器`); | ||||
|  | ||||
|     // MutationObserver 回调函数 | ||||
|     const observerCallback = (mutationsList, observer) => { | ||||
|         // console.log("MutationObserver 触发!"); // 调试时可打开,查看何时触发 | ||||
|  | ||||
| @ -21,6 +21,18 @@ window.addEventListener("languageChanged", function (event) { | ||||
|   window.updateLanguage(language); | ||||
| }); | ||||
|  | ||||
| // 监听翻译配置更新事件 | ||||
| window.addEventListener("translateConfigChanged", function (event) { | ||||
|   console.log("📝 WhatsApp: 收到翻译配置更新事件", event.detail); | ||||
|   // 立即更新配置 | ||||
|   updateConfigInfo().then(() => { | ||||
|     console.log("✅ WhatsApp: 配置更新完成", { | ||||
|       sendTranslateStatus: trcConfig.sendTranslateStatus, | ||||
|       timestamp: new Date().toISOString() | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
|  | ||||
| // 监听翻译配置更新事件 | ||||
| window.addEventListener("translateConfigChanged", function (event) { | ||||
|   console.log("WhatsApp: 收到翻译配置更新事件", event.detail); | ||||
|  | ||||
| @ -922,26 +922,54 @@ class WindowService { | ||||
|       } | ||||
|       // 尝试分段执行脚本以避免克隆错误 | ||||
|       try { | ||||
|         // 对于 Telegram,添加额外的初始化检查 | ||||
|         if (platform === "Telegram") { | ||||
|           // 等待 Telegram 页面完全加载 | ||||
|           await new Promise(resolve => setTimeout(resolve, 2000)); | ||||
|  | ||||
|           // 检查页面是否准备就绪 | ||||
|           const pageReady = await view.webContents.executeJavaScript(` | ||||
|             (function() { | ||||
|               return { | ||||
|                 readyState: document.readyState, | ||||
|                 hasMain: !!document.getElementById('Main'), | ||||
|                 hasLeftColumn: !!document.getElementById('LeftColumn-main'), | ||||
|                 url: window.location.href | ||||
|               }; | ||||
|             })(); | ||||
|           `); | ||||
|  | ||||
|           logger.info(`[${partitionId}] Telegram 页面状态:`, pageReady); | ||||
|  | ||||
|           if (pageReady.readyState !== 'complete') { | ||||
|             logger.warn(`[${partitionId}] Telegram 页面未完全加载,等待...`); | ||||
|             await new Promise(resolve => setTimeout(resolve, 3000)); | ||||
|           } | ||||
|         } | ||||
|  | ||||
|         // 将脚本包装在立即执行函数中,避免全局作用域污染 | ||||
|         const wrappedScript = ` | ||||
|           (function() { | ||||
|             try { | ||||
|               console.log('${platform}: 开始执行注入脚本...'); | ||||
|               ${scriptContent} | ||||
|               console.log('${platform}: 脚本执行完成'); | ||||
|             } catch (e) { | ||||
|               console.error('脚本执行内部错误:', e); | ||||
|               console.error('${platform}: 脚本执行内部错误:', e); | ||||
|               console.error('${platform}: 错误堆栈:', e.stack); | ||||
|             } | ||||
|           })(); | ||||
|         `; | ||||
|         await view.webContents.executeJavaScript(wrappedScript); | ||||
|         logger.info("execute jsCode successfully"); | ||||
|         logger.info(`[${partitionId}] ${platform} 脚本执行成功`); | ||||
|       } catch (executeError) { | ||||
|         // 如果包装执行失败,尝试直接执行 | ||||
|         logger.warn("包装脚本执行失败,尝试直接执行:", executeError.message); | ||||
|         logger.warn(`[${partitionId}] 包装脚本执行失败,尝试直接执行:`, executeError.message); | ||||
|         try { | ||||
|           await view.webContents.executeJavaScript(scriptContent); | ||||
|           logger.info("直接执行脚本成功"); | ||||
|           logger.info(`[${partitionId}] ${platform} 直接执行脚本成功`); | ||||
|         } catch (directError) { | ||||
|           logger.error("脚本执行完全失败:", directError.message); | ||||
|           logger.error(`[${partitionId}] ${platform} 脚本执行完全失败:`, directError.message); | ||||
|           // 不抛出错误,让页面继续加载 | ||||
|         } | ||||
|       } | ||||
| @ -1104,9 +1132,9 @@ class WindowService { | ||||
|       server = server.replace(/\s/g, ""); | ||||
|       logger.info("server:", server); | ||||
|  | ||||
|       // 对于WhatsApp平台,使用优化的代理配置 | ||||
|       if (platform === "WhatsApp") { | ||||
|         logger.info(`[${partitionId}] Proxy: WhatsApp平台,使用优化的代理配置`); | ||||
|       // 对于WhatsApp和Telegram平台,使用优化的代理配置 | ||||
|       if (platform === "WhatsApp" || platform === "Telegram") { | ||||
|         logger.info(`[${partitionId}] Proxy: ${platform}平台,使用优化的代理配置`); | ||||
|  | ||||
|         // 使用标准的fixed_servers模式,更稳定 | ||||
|         const proxyConfig = { | ||||
| @ -1132,7 +1160,7 @@ class WindowService { | ||||
|  | ||||
|           return; | ||||
|         } catch (error) { | ||||
|           logger.error(`[${partitionId}] Proxy: PAC脚本配置失败: ${error.message}`); | ||||
|           logger.error(`[${partitionId}] Proxy: ${platform}代理配置失败: ${error.message}`); | ||||
|           logger.info(`[${partitionId}] Proxy: 回退到标准代理配置`); | ||||
|         } | ||||
|       } | ||||
| @ -1896,12 +1924,23 @@ class WindowService { | ||||
|       const globalConfig = await app.sdb.selectOne("global_proxy_config"); | ||||
|  | ||||
|       if (globalConfig && globalConfig.proxyStatus === "true") { | ||||
|         // 即使IP或端口为空,也要强制设置代理 | ||||
|         const proxyIp = globalConfig.proxyIp || "invalid.proxy.ip"; | ||||
|         const proxyPort = globalConfig.proxyPort || "0"; | ||||
|         // 检查代理配置是否有效 | ||||
|         const hasValidConfig = globalConfig.proxyIp && globalConfig.proxyPort; | ||||
|  | ||||
|         logger.error(`[${partitionId}] 🔥 强制使用无效全局代理: ${proxyIp}:${proxyPort}`); | ||||
|         logger.error(`[${partitionId}] 🔥 这将导致网络请求失败,但符合严格模式要求`); | ||||
|         if (!hasValidConfig) { | ||||
|           logger.warn(`[${partitionId}] ⚠️  全局代理已开启但配置无效(IP或端口为空)`); | ||||
|           logger.warn(`[${partitionId}] 🔄 回退到系统代理,避免连接失败`); | ||||
|  | ||||
|           // 清除代理配置,使用系统代理 | ||||
|           await view.webContents.session.setProxy({ mode: 'system' }); | ||||
|           logger.info(`[${partitionId}] ✅ 已回退到系统代理`); | ||||
|           return; | ||||
|         } | ||||
|  | ||||
|         const proxyIp = globalConfig.proxyIp; | ||||
|         const proxyPort = globalConfig.proxyPort; | ||||
|  | ||||
|         logger.info(`[${partitionId}] 🌐 使用全局代理: ${proxyIp}:${proxyPort}`); | ||||
|  | ||||
|         // 构建代理配置 | ||||
|         const proxyConfig = { | ||||
| @ -1910,7 +1949,7 @@ class WindowService { | ||||
|             singleProxy: { | ||||
|               scheme: globalConfig.proxyType || 'http', | ||||
|               host: proxyIp, | ||||
|               port: parseInt(proxyPort) || 0 | ||||
|               port: parseInt(proxyPort) | ||||
|             } | ||||
|           } | ||||
|         }; | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 unknown
					unknown