// ==UserScript== // @name 腾讯元宝自动切换至deepseek // @namespace http://tampermonkey.net/ // @version 1.1 // @description 腾讯元宝进入页面和新建对话时自动切换模型为deepseek // @license MIT // @author Diyun // @match https://yuanbao.tencent.com/* // @icon https://cdn-bot.hunyuan.tencent.com/logo.png // @grant none // @run-at document-end // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 状态管理对象 const state = { isSwitching: false, // 切换操作进行中标记 hasBound: false // 事件绑定标记 }; // 智能元素检测函数(增加超时机制) const waitForElement = (selector, isXPath = false, timeout = 5000) => { return new Promise((resolve, reject) => { const start = Date.now(); const check = () => { let element; if (isXPath) { const result = document.evaluate( selector, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ); element = result.singleNodeValue; } else { element = document.querySelector(selector); } if (element) { resolve(element); } else if (Date.now() - start >= timeout) { reject(new Error('元素查找超时')); } else { setTimeout(check, 100); } }; check(); }); }; // 模型切换主逻辑(增加互斥锁) const autoSwitchModel = async () => { if (state.isSwitching) return; state.isSwitching = true; try { // 第一阶段:等待并点击模型切换按钮 const switchBtn = await waitForElement('button[dt-button-id="model_switch"]:not([disabled])'); switchBtn.click(); console.debug('[智能切换] 已展开模型列表'); // 第二阶段:等待并选择目标模型 const targetItem = await waitForElement( '//div[contains(@class,"drop-down-item")][.//div[text()="DeepSeek"]][.//div[contains(.,"深度思考")]]', true ); targetItem.click(); console.log('[智能切换] 模型切换成功'); } catch (err) { console.error('[智能切换] 操作失败:', err); } finally { state.isSwitching = false; } }; // 智能事件绑定(带防抖机制) const smartBind = () => { // 事件委托处理新建对话按钮 document.body.addEventListener('click', async (e) => { const target = e.target.closest('.yb-common-nav__hd__new-chat.J_UseGuideNewChat0'); if (!target) return; console.log('[智能切换] 检测到新建对话操作'); setTimeout(() => { autoSwitchModel().catch(console.error); }, 1000); // 根据实际网络状况调整延迟 }, { once: false, passive: true }); }; // 页面初始化逻辑 const init = () => { // 主功能初始化 setTimeout(() => { autoSwitchModel().catch(console.error); smartBind(); }, 1500); // 轻量级DOM监听(仅监听按钮区域) const observer = new MutationObserver((mutations) => { for (const mutation of mutations) { if (mutation.addedNodes.length) { const hasRelevantNode = [...mutation.addedNodes].some(node => node.nodeType === 1 && node.closest('.yb-common-nav__hd__new-chat.J_UseGuideNewChat0') ); if (hasRelevantNode) smartBind(); } } }); observer.observe(document.body, { childList: true, subtree: true, attributes: false, characterData: false }); }; // 启动脚本 window.addEventListener('load', init, { once: true }); })();