优化油猴插件

scripts
王良 2024-04-24 09:57:07 +08:00
parent 83d3ba7371
commit 8661b6acf2
2 changed files with 61 additions and 29 deletions

View File

@ -28,7 +28,7 @@ document.addEventListener("DOMContentLoaded", () => {
} }
if (!((window.__ds_global__ || {}).GM_getValue || (() => true))("ds_enabled", true)) { if (!((window.__ds_global__ || {}).GM_getValue || (() => true))("ds_enabled", true)) {
console.log("ds_github_monkey_2.5.20: disabled") console.log("ds_github_monkey_2.5.20: tampermonkey disabled")
return return
} }

View File

@ -1,7 +1,7 @@
/** /**
* 当前脚本为仿照的版本并非篡改猴插件的源码仅供学习参考 * 篡改猴Tampermonkey| 油猴Greasemonkey浏览器脚本扩展
* *
* @name 篡改猴Tampermonkey| 油猴Greasemonkey浏览器脚本扩展 * @remark 当前脚本为仿照的版本并非篡改猴插件的源码仅供学习参考
* @author Wang Liang王良仿照的 * @author Wang Liang王良仿照的
* @authorHomePage https://wangliang1024.cn * @authorHomePage https://wangliang1024.cn
* @description 篡改猴 (Tampermonkey) 是拥有 超过 1000 万用户 的最流行的浏览器扩展之一 它适用于 ChromeMicrosoft EdgeSafariOpera Next Firefox * @description 篡改猴 (Tampermonkey) 是拥有 超过 1000 万用户 的最流行的浏览器扩展之一 它适用于 ChromeMicrosoft EdgeSafariOpera Next Firefox
@ -20,16 +20,20 @@
const context = { const context = {
initialized: false, // 是否已经初始化 initialized: false, // 是否已经初始化
defaultPluginOptions: {}, // 默认插件选项 defaultPluginOptions: {}, // 默认插件选项
pluginElement: null, // 插件div styleElement: null, // 插件样式元素
menusElement: null, // 菜单列表div pluginElement: null, // 插件div元素
menusElement: null, // 菜单列表div元素
userMenusElement: null, // 用户菜单列表div元素
menus: {}, // 菜单集合 menus: {}, // 菜单集合
menuIndex: 0, // 菜单索引用于生成menuCmdId menuIndex: 0, // 菜单索引用于生成menuCmdId
lastNotification: null // 最后一次通知 lastNotification: null // 最后一次通知
/*{ /*
obj: null, // 通知对象 {
obj: null, // 通知对象类型Notification
options: null, // 通知选项 options: null, // 通知选项
timeout: null // 通知定时器 timeout: null // 通知定时器
}*/ }
*/
}; };
@ -48,6 +52,7 @@
// 创建一个新的<style>元素 // 创建一个新的<style>元素
const styleElement = document.createElement('style'); const styleElement = document.createElement('style');
styleElement.id = PRE + "plugin-style";
// 设置<style>元素的type属性 // 设置<style>元素的type属性
styleElement.type = 'text/css'; styleElement.type = 'text/css';
@ -121,6 +126,9 @@
// 将<style>元素添加到<head>中 // 将<style>元素添加到<head>中
document.head.append(styleElement); document.head.append(styleElement);
// 将<style>元素保存在上下文中
context.styleElement = styleElement;
}; };
// 创建插件div // 创建插件div
@ -132,11 +140,13 @@
// 创建插件div // 创建插件div
context.pluginElement = document.createElement('div'); context.pluginElement = document.createElement('div');
context.pluginElement.id = PRE + "plugin";
context.pluginElement.title = "油猴脚本" + (options.name ? "" + options.name : ""); context.pluginElement.title = "油猴脚本" + (options.name ? "" + options.name : "");
context.pluginElement.className = "____ds-icon____"; context.pluginElement.className = "____ds-icon____";
// 创建菜单列表div // 创建菜单列表div
context.menusElement = document.createElement('div'); context.menusElement = document.createElement('div');
context.menusElement.id = PRE + "menus";
context.menusElement.className = "____ds-menus____"; context.menusElement.className = "____ds-menus____";
if (options.width > 0) { if (options.width > 0) {
context.menusElement.style['min-width'] = options.width + "px"; context.menusElement.style['min-width'] = options.width + "px";
@ -155,10 +165,10 @@
switchMenuElement.onclick = function () { switchMenuElement.onclick = function () {
let enabled = api.GM_getValue("ds_enabled", true) let enabled = api.GM_getValue("ds_enabled", true)
if (enabled) { if (enabled) {
api.hideMenus(); api.hideUserMenus();
enabled = false; enabled = false;
} else { } else {
api.showMenus(); api.showUserMenus();
enabled = true; enabled = true;
} }
switchMenuElement.innerHTML = (enabled ? "✅" : "❌") + icon + options.name; switchMenuElement.innerHTML = (enabled ? "✅" : "❌") + icon + options.name;
@ -172,25 +182,30 @@
// 将开关菜单添加到菜单列表div中 // 将开关菜单添加到菜单列表div中
context.menusElement.append(switchMenuElement); context.menusElement.append(switchMenuElement);
// 创建用户菜单列表div
context.userMenusElement = document.createElement('div');
context.userMenusElement.id = PRE + "user-menus";
context.userMenusElement.className = "____ds-user-menus____";
// 将用户菜单div添加到菜单div中
context.menusElement.append(context.userMenusElement);
// 获取body元素 // 获取body元素
const body = document.getElementsByTagName('body')[0]; const body = document.getElementsByTagName('body')[0];
// 将插件div添加到body中 // 将插件div添加到body中
body.prepend(context.pluginElement); body.prepend(context.pluginElement);
} }
// 显示菜单列表 // 显示用户菜单列表
api.showMenus = () => { api.showUserMenus = () => {
for (const menuCmdId in context.menus) { if (context.userMenusElement) {
const menuElement = context.menus[menuCmdId].element; context.userMenusElement.style.display = "block";
menuElement.style.display = "block";
} }
} }
// 隐藏菜单列表 // 隐藏用户菜单列表
api.hideMenus = () => { api.hideUserMenus = () => {
for (const menuCmdId in context.menus) { if (context.userMenusElement) {
const menuElement = context.menus[menuCmdId].element; context.userMenusElement.style.display = "none";
menuElement.style.display = "none";
} }
} }
@ -225,10 +240,18 @@
// 生成菜单ID // 生成菜单ID
let menuCmdId; let menuCmdId;
if (options.id) { if (options.id) {
if (options.id.indexOf(MENU_ID_PRE) === 0) { if (typeof options.id !== "string") {
menuCmdId = options.id; options.id = options.id.toString();
} else { }
menuCmdId = MENU_ID_PRE + options.id;
menuCmdId = (options.id.indexOf(MENU_ID_PRE) === 0 ? '' : MENU_ID_PRE) + options.id;
// 如果是数字ID为了避免与自增ID索引冲突将数字ID赋值给自增ID索引
if (options.id.match("^\\d+$")) {
const numberId = parseInt(options.id);
if (numberId > context.menuIndex) {
context.menuIndex = numberId;
}
} }
} else { } else {
menuCmdId = MENU_ID_PRE + (++context.menuIndex); menuCmdId = MENU_ID_PRE + (++context.menuIndex);
@ -250,7 +273,7 @@
} }
// 将菜单元素添加到菜单列表div中 // 将菜单元素添加到菜单列表div中
context.menusElement.append(menuElement); context.userMenusElement.append(menuElement);
// 将菜单添加到菜单集合中 // 将菜单添加到菜单集合中
context.menus[menuCmdId] = { context.menus[menuCmdId] = {
@ -270,15 +293,24 @@
return; return;
} }
if (typeof menuCmdId !== "string") {
menuCmdId = menuCmdId.toString();
}
if (menuCmdId.indexOf(MENU_ID_PRE) !== 0) { if (menuCmdId.indexOf(MENU_ID_PRE) !== 0) {
menuCmdId = MENU_ID_PRE + menuCmdId; menuCmdId = MENU_ID_PRE + menuCmdId;
} }
const menuElement = document.getElementById(menuCmdId) const menu = context.menus[menuCmdId];
if (menuElement) { if (menu) {
menuElement.remove(); menu.element.remove();
delete context.menus[menuCmdId];
} else {
const menuElement = document.getElementById(menuCmdId)
if (menuElement) {
menuElement.remove();
}
} }
delete context.menus[menuCmdId];
}; };
// 打开新标签 // 打开新标签