From 65066ebf690c370540da770e71409fa8750ccaa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Mon, 22 Apr 2024 10:55:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AF=A1=E6=94=B9=E7=8C=B4=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=8F=9C=E5=8D=95=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81title=E5=8F=82=E6=95=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- github/monkey.js | 8 ++++---- tampermonkey.js | 22 +++++++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/github/monkey.js b/github/monkey.js index 3c6e59b1..c8112e89 100644 --- a/github/monkey.js +++ b/github/monkey.js @@ -143,10 +143,10 @@ document.addEventListener("DOMContentLoaded", () => { if (menu_feedBack_ID) {GM_unregisterMenuCommand(menu_rawFast_ID); GM_unregisterMenuCommand(menu_rawDownLink_ID); GM_unregisterMenuCommand(menu_gitClone_ID); GM_unregisterMenuCommand(menu_feedBack_ID); menu_rawFast = GM_getValue('xiu2_menu_raw_fast');} // 避免在减少 raw 数组后,用户储存的数据大于数组而报错 if (menu_rawFast > raw_url.length - 1) menu_rawFast = 0 - menu_rawDownLink_ID = GM_registerMenuCommand(`${GM_getValue('menu_rawDownLink')?'✅':'❌'} 项目列表单文件快捷下载 (☁)`, function(){if (GM_getValue('menu_rawDownLink') === true) {GM_setValue('menu_rawDownLink', false); GM_notification({text: `已关闭 [项目列表单文件快捷下载 (☁)] 功能\n(刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});} else {GM_setValue('menu_rawDownLink', true); GM_notification({text: `已开启 [项目列表单文件快捷下载 (☁)] 功能\n(刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});}registerMenuCommand();}); - if (GM_getValue('menu_rawDownLink')) menu_rawFast_ID = GM_registerMenuCommand(`      ${['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣','🔟'][menu_rawFast]} [ ${raw_url[menu_rawFast][1]} ] 加速源 (☁) - 点击切换`, menu_toggle_raw_fast); - menu_gitClone_ID = GM_registerMenuCommand(`${GM_getValue('menu_gitClone')?'✅':'❌'} 添加 git clone 命令`, function(){if (GM_getValue('menu_gitClone') === true) {GM_setValue('menu_gitClone', false); GM_notification({text: `已关闭 [添加 git clone 命令] 功能`, timeout: 3500, onclick: function(){location.reload();}});} else {GM_setValue('menu_gitClone', true); GM_notification({text: `已开启 [添加 git clone 命令] 功能`, timeout: 3500, onclick: function(){location.reload();}});}registerMenuCommand();}); - menu_feedBack_ID = GM_registerMenuCommand('💬 反馈 & 建议 [Github]', function () {GM_openInTab('https://github.com/XIU2/UserScript', {active: true,insert: true,setParent: true});GM_openInTab('https://greasyfork.org/zh-CN/scripts/412245/feedback', {active: true,insert: true,setParent: true});}); + menu_rawDownLink_ID = GM_registerMenuCommand(`${GM_getValue('menu_rawDownLink')?'✅':'❌'} 项目列表单文件快捷下载 (☁)`, function(){if (GM_getValue('menu_rawDownLink') === true) {GM_setValue('menu_rawDownLink', false); GM_notification({text: `已关闭 [项目列表单文件快捷下载 (☁)] 功能\n(刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});} else {GM_setValue('menu_rawDownLink', true); GM_notification({text: `已开启 [项目列表单文件快捷下载 (☁)] 功能\n(刷新网页后生效)`, timeout: 3500, onclick: function(){location.reload();}});}registerMenuCommand();}, {title: "点击开关「项目列表单文件快捷下载」功能"}); + if (GM_getValue('menu_rawDownLink')) menu_rawFast_ID = GM_registerMenuCommand(`      ${['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣','🔟'][menu_rawFast]} [ ${raw_url[menu_rawFast][1]} ] 加速源 (☁) - 点击切换`, menu_toggle_raw_fast, {title: "点击切换「项目列表单文件快捷下载加速源」"}); + menu_gitClone_ID = GM_registerMenuCommand(`${GM_getValue('menu_gitClone')?'✅':'❌'} 添加 git clone 命令`, function(){if (GM_getValue('menu_gitClone') === true) {GM_setValue('menu_gitClone', false); GM_notification({text: `已关闭 [添加 git clone 命令] 功能`, timeout: 3500, onclick: function(){location.reload();}});} else {GM_setValue('menu_gitClone', true); GM_notification({text: `已开启 [添加 git clone 命令] 功能`, timeout: 3500, onclick: function(){location.reload();}});}registerMenuCommand();}, {title: "点击开关「添加 git clone 命令」功能"}); + menu_feedBack_ID = GM_registerMenuCommand('💬 反馈 & 建议 [Github]', function () {GM_openInTab('https://github.com/XIU2/UserScript', {active: true,insert: true,setParent: true});GM_openInTab('https://greasyfork.org/zh-CN/scripts/412245/feedback', {active: true,insert: true,setParent: true});}, {title: "点击前往反馈问题或提出建议"}); } // 切换加速源 diff --git a/tampermonkey.js b/tampermonkey.js index 15fd2630..dcb180c8 100644 --- a/tampermonkey.js +++ b/tampermonkey.js @@ -11,6 +11,7 @@ 'use strict'; (function () { const PRE = "DS-Tampermonkey:"; // 前缀 + const MENU_ID_PRE = PRE + "menu-"; const context = { initialized: false, // 是否已经初始化 @@ -196,16 +197,31 @@ const options = typeof options_or_accessKey === "string" ? { accessKey: options_or_accessKey } : options_or_accessKey; // 生成菜单ID - const menuCmdId = PRE + "menu-" + (++context.menuIndex); + let menuCmdId = options.id || ""; + if (options.id) { + if (options.id.indexOf(MENU_ID_PRE) === 0) { + menuCmdId = options.id; + } else { + menuCmdId = MENU_ID_PRE + options.id; + } + } else { + menuCmdId = MENU_ID_PRE + (options.id || ++context.menuIndex); + } // 创建菜单元素 const menuElement = document.createElement('div'); menuElement.id = menuCmdId; menuElement.className = "____ds-menu____"; menuElement.innerHTML = name; + if (options.title) { + menuElement.title = typeof options.title === "function" ? options.title() : options.title; + } if (callback) { menuElement.onclick = callback; } + if (options.accessKey) { + // TODO: 快捷键功能待开发,篡改猴官方文档:https://www.tampermonkey.net/documentation.php#api:GM_registerMenuCommand + } // 将菜单元素添加到菜单列表div中 context.menusElement.append(menuElement); @@ -223,6 +239,10 @@ }, // 删除菜单 GM_unregisterMenuCommand: (menuCmdId) => { + if (menuCmdId.indexOf(MENU_ID_PRE) !== 0) { + menuCmdId = MENU_ID_PRE + menuCmdId; + } + const menuElement = document.getElementById(menuCmdId) if (menuElement) { menuElement.remove();