From dec4d255de62cded5d450644aec9394a7b0f8d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=99=E6=BC=A0=E4=B9=8B=E5=AD=90?= <7850715+maboloshi@users.noreply.github.com> Date: Sat, 1 Feb 2025 16:40:00 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84`=E5=8A=9F=E8=83=BD=E5=BC=80?= =?UTF-8?q?=E5=85=B3`=E9=80=BB=E8=BE=91=201.=20=E5=85=A8=E5=B1=80=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1`FeatureSet`=E5=82=A8=E5=AD=98=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=BC=80=E5=85=B3=E5=B1=9E=E6=80=A7=202.=20=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1`CONFIG`=E7=9A=84`DESC=5FSELECTORS`=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E5=AD=98=E5=82=A8`=E9=A1=B5=E9=9D=A2=E7=AE=80?= =?UTF-8?q?=E4=BB=8Bcss=E7=AD=9B=E9=80=89=E5=99=A8=E8=A7=84=E5=88=99`=203.?= =?UTF-8?q?=20=E9=87=8D=E6=9E=84`registerMenuCommand=20=E5=87=BD=E6=95=B0`?= =?UTF-8?q?,=20=E5=A4=A7=E5=B9=85=E6=8F=90=E5=8D=87=E5=8F=AF=E7=BB=B4?= =?UTF-8?q?=E6=8A=A4=E6=80=A7=E5=92=8C=E6=89=A9=E5=B1=95=E6=80=A7=204.=20?= =?UTF-8?q?=E8=B0=83=E6=95=B4`=E7=9B=91=E5=90=AC=20Turbo=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E4=BA=8B=E4=BB=B6`=E9=80=BB=E8=BE=91=E5=92=8C?= =?UTF-8?q?=E5=90=AF=E7=94=A8`=E7=AE=80=E4=BB=8B=E7=BF=BB=E8=AF=91`?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.user.js | 99 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 31 deletions(-) diff --git a/main.user.js b/main.user.js index 725c01040..f63893ce5 100644 --- a/main.user.js +++ b/main.user.js @@ -30,6 +30,13 @@ const lang = 'zh-CN'; // 设置默认语言 const FeatureSet = { enable_RegExp: GM_getValue("enable_RegExp", true), + enable_transDesc: GM_getValue("enable_transDesc", true), + }; + const CONFIG = { + DESC_SELECTORS: { + repository: ".f4.my-3", + gist: ".gist-content [itemprop='about']" + }, }; let page = false, @@ -39,8 +46,7 @@ ignoreSelectors = [], tranSelectors = [], regexpRules = []; - - let enable_XunfeiTranslation = GM_getValue("enable_XunfeiTranslation", true); // 初始化讯飞听见翻译为开启状态 + function updateConfig(page) { if (cachedPage !== page && page) { cachedPage = page; @@ -451,29 +457,61 @@ * registerMenuCommand 函数:注册菜单。 */ function registerMenuCommand() { - const toggleRegExp = () => { - enable_RegExp = !enable_RegExp; - GM_setValue("enable_RegExp", enable_RegExp); - GM_notification(`已${enable_RegExp ? '开启' : '关闭'}正则功能`); - if (enable_RegExp) { - location.reload(); - } - GM_unregisterMenuCommand(id); - id = GM_registerMenuCommand(`${enable_RegExp ? '关闭' : '开启'}正则功能`, toggleRegExp); + const createMenuCommand = (config) => { + const { label, key, callback } = config; + let menuId; + + const getMenuLabel = (label, isEnabled) => + `${isEnabled ? "禁用" : "启用"} ${label}`; + + const toggle = () => { + const newFeatureState = !FeatureSet[key]; + GM_setValue(key, newFeatureState); + FeatureSet[key] = newFeatureState; + GM_notification(`${label}已${newFeatureState ? '启用' : '禁用'}`); + + // 调用回调函数 + if (callback) callback(newFeatureState); + + // 更新菜单命令的标签 + GM_unregisterMenuCommand(menuId); + menuId = GM_registerMenuCommand( + getMenuLabel(label, newFeatureState), + toggle + ); + }; + + // 初始注册菜单命令 + menuId = GM_registerMenuCommand( + getMenuLabel(label, FeatureSet[key]), + toggle + ); }; - const toggleXunfeiTranslation = () => { - enable_XunfeiTranslation = !enable_XunfeiTranslation; - GM_setValue("enable_XunfeiTranslation", enable_XunfeiTranslation); - GM_notification(`已${enable_XunfeiTranslation ? '开启' : '关闭'}讯飞听见翻译`); - // 这里可以添加一些额外的逻辑,比如根据开关状态更新页面等 - GM_unregisterMenuCommand(xunfeiId); - xunfeiId = GM_registerMenuCommand(`${enable_XunfeiTranslation ? '关闭' : '开启'}讯飞听见翻译`, toggleXunfeiTranslation); - }; - let id = GM_registerMenuCommand(`${enable_RegExp ? '关闭' : '开启'}正则功能`, toggleRegExp); - // 注册新的菜单命令,用于控制讯飞听见翻译的开关 - let xunfeiId = GM_registerMenuCommand(`${enable_XunfeiTranslation ? '关闭' : '开启'}讯飞听见翻译`, toggleXunfeiTranslation); - } + const menuConfigs = [ + { + label: "正则功能", + key: "enable_RegExp", + callback: (newFeatureState) => { + if (newFeatureState) traverseNode(document.body); + } + }, + { + label: "描述翻译", + key: "enable_transDesc", + callback: (newFeatureState) => { + if (newFeatureState && CONFIG.DESC_SELECTORS[page]) { + transDesc(CONFIG.DESC_SELECTORS[page]); + } else { + document.getElementById('translate-me')?.remove(); + } + } + } + ]; + + // 注册所有菜单项 + menuConfigs.forEach(config => createMenuCommand(config)); + }; /** * init 函数:初始化翻译功能。 @@ -503,14 +541,13 @@ // 监听 Turbo 完成事件 document.addEventListener('turbo:load', () => { - if (page) { - transTitle(); // 翻译页面标题 - transBySelector(); - if (page === "repository") { //仓库简介翻译 - transDesc(".f4.my-3"); - } else if (page === "gist") { // Gist 简介翻译 - transDesc(".gist-content [itemprop='about']"); - } + if (!page) return; + + transTitle(); // 翻译页面标题 + transBySelector(); + + if (FeatureSet.enable_transDesc && CONFIG.DESC_SELECTORS[page]) { + transDesc(CONFIG.DESC_SELECTORS[page]); } });