篡改猴插件代码重构。
parent
df2a4384eb
commit
c194656ee0
105
tampermonkey.js
105
tampermonkey.js
|
@ -1,7 +1,8 @@
|
||||||
/**
|
/**
|
||||||
* 篡改猴(Tampermonkey)| 油猴(Greasemonkey)浏览器脚本扩展
|
* 当前脚本为仿照的版本,并非篡改猴插件的源码,仅供学习参考。
|
||||||
*
|
*
|
||||||
* @author 由 WangLiang(王良)仿照的
|
* @name 篡改猴(Tampermonkey)| 油猴(Greasemonkey)浏览器脚本扩展
|
||||||
|
* @author 由 Wang Liang(王良)仿照的
|
||||||
* @authorHomePage https://wangliang1024.cn
|
* @authorHomePage https://wangliang1024.cn
|
||||||
* @description 篡改猴 (Tampermonkey) 是拥有 超过 1000 万用户 的最流行的浏览器扩展之一。 它适用于 Chrome、Microsoft Edge、Safari、Opera Next 和 Firefox。
|
* @description 篡改猴 (Tampermonkey) 是拥有 超过 1000 万用户 的最流行的浏览器扩展之一。 它适用于 Chrome、Microsoft Edge、Safari、Opera Next 和 Firefox。
|
||||||
* 有些人也会把篡改猴(Tampermonkey)称作油猴(Greasemonkey),尽管后者只是一款仅适用于 Firefox 浏览器的浏览器扩展程序。
|
* 有些人也会把篡改猴(Tampermonkey)称作油猴(Greasemonkey),尽管后者只是一款仅适用于 Firefox 浏览器的浏览器扩展程序。
|
||||||
|
@ -31,8 +32,15 @@
|
||||||
}*/
|
}*/
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// 创建插件API
|
||||||
|
const api = {};
|
||||||
|
|
||||||
|
|
||||||
|
//region DS自定义的API
|
||||||
|
|
||||||
// 创建插件样式
|
// 创建插件样式
|
||||||
function createPluginStyle (options) {
|
api.createPluginStyle = (options) => {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
// 创建一个新的<style>元素
|
// 创建一个新的<style>元素
|
||||||
|
@ -110,10 +118,10 @@
|
||||||
|
|
||||||
// 将<style>元素添加到<head>中
|
// 将<style>元素添加到<head>中
|
||||||
document.head.append(styleElement);
|
document.head.append(styleElement);
|
||||||
}
|
};
|
||||||
|
|
||||||
// 创建插件div
|
// 创建插件div
|
||||||
function createPluginDiv (options) {
|
api.createPluginDiv = (options) => {
|
||||||
options = {
|
options = {
|
||||||
...{ name: "油猴脚本" },
|
...{ name: "油猴脚本" },
|
||||||
...options
|
...options
|
||||||
|
@ -134,7 +142,7 @@
|
||||||
context.pluginElement.append(context.menusElement);
|
context.pluginElement.append(context.menusElement);
|
||||||
|
|
||||||
// 创建开关菜单
|
// 创建开关菜单
|
||||||
const enabled = window.__ds_global__.GM_getValue("ds_enabled", true)
|
const enabled = api.GM_getValue("ds_enabled", true)
|
||||||
const switchMenuElement = document.createElement('div');
|
const switchMenuElement = document.createElement('div');
|
||||||
const icon = (options.icon ? `<img alt="icon" src="${options.icon}"/>` : " ");
|
const icon = (options.icon ? `<img alt="icon" src="${options.icon}"/>` : " ");
|
||||||
switchMenuElement.id = PRE + "menu-0";
|
switchMenuElement.id = PRE + "menu-0";
|
||||||
|
@ -142,18 +150,18 @@
|
||||||
switchMenuElement.innerHTML = (enabled ? "✅" : "❌") + icon + options.name;
|
switchMenuElement.innerHTML = (enabled ? "✅" : "❌") + icon + options.name;
|
||||||
switchMenuElement.title = `点击${enabled ? "关闭" : "开启"}此脚本功能`;
|
switchMenuElement.title = `点击${enabled ? "关闭" : "开启"}此脚本功能`;
|
||||||
switchMenuElement.onclick = function () {
|
switchMenuElement.onclick = function () {
|
||||||
let enabled = window.__ds_global__.GM_getValue("ds_enabled", true)
|
let enabled = api.GM_getValue("ds_enabled", true)
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
hideMenus();
|
api.hideMenus();
|
||||||
enabled = false;
|
enabled = false;
|
||||||
} else {
|
} else {
|
||||||
showMenus();
|
api.showMenus();
|
||||||
enabled = true;
|
enabled = true;
|
||||||
}
|
}
|
||||||
switchMenuElement.innerHTML = (enabled ? "✅" : "❌") + icon + options.name;
|
switchMenuElement.innerHTML = (enabled ? "✅" : "❌") + icon + options.name;
|
||||||
switchMenuElement.title = `点击${enabled ? "关闭" : "开启"}此脚本功能`;
|
switchMenuElement.title = `点击${enabled ? "关闭" : "开启"}此脚本功能`;
|
||||||
window.__ds_global__.GM_setValue("ds_enabled", enabled)
|
api.GM_setValue("ds_enabled", enabled)
|
||||||
window.__ds_global__.GM_notification({
|
api.GM_notification({
|
||||||
text: `已${enabled ? "开启" : "关闭"} 「${options.name}」 功能\n(刷新网页后生效)`,
|
text: `已${enabled ? "开启" : "关闭"} 「${options.name}」 功能\n(刷新网页后生效)`,
|
||||||
timeout: 3500
|
timeout: 3500
|
||||||
});
|
});
|
||||||
|
@ -167,27 +175,28 @@
|
||||||
body.prepend(context.pluginElement);
|
body.prepend(context.pluginElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
function showMenus () {
|
api.showMenus = () => {
|
||||||
for (const menuCmdId in context.menus) {
|
for (const menuCmdId in context.menus) {
|
||||||
const menuElement = context.menus[menuCmdId].element;
|
const menuElement = context.menus[menuCmdId].element;
|
||||||
menuElement.style.display = "block";
|
menuElement.style.display = "block";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function hideMenus () {
|
// 隐藏菜单
|
||||||
|
api.hideMenus = () => {
|
||||||
for (const menuCmdId in context.menus) {
|
for (const menuCmdId in context.menus) {
|
||||||
const menuElement = context.menus[menuCmdId].element;
|
const menuElement = context.menus[menuCmdId].element;
|
||||||
menuElement.style.display = "none";
|
menuElement.style.display = "none";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.__ds_global__ = {
|
|
||||||
// 获取上下文
|
// 获取上下文
|
||||||
getContext: () => {
|
api.getContext = () => {
|
||||||
return context;
|
return context;
|
||||||
},
|
};
|
||||||
// 初始化
|
|
||||||
DS_init: (options) => {
|
// 初始化篡改猴操作界面
|
||||||
|
api.DS_init = (options) => {
|
||||||
try {
|
try {
|
||||||
if (context.initialized) return;
|
if (context.initialized) return;
|
||||||
// 合并默认参数
|
// 合并默认参数
|
||||||
|
@ -195,17 +204,23 @@
|
||||||
...context.defaultPluginOptions,
|
...context.defaultPluginOptions,
|
||||||
...options
|
...options
|
||||||
};
|
};
|
||||||
createPluginStyle(options);
|
api.createPluginStyle(options);
|
||||||
createPluginDiv(options);
|
api.createPluginDiv(options);
|
||||||
context.initialized = true;
|
context.initialized = true;
|
||||||
|
|
||||||
console.log("ds_tampermonkey: initialization completed(篡改猴插件初始化完成,篡改猴图标已显示在页面右侧,鼠标移到上面可展示功能列表!)")
|
console.log("ds_tampermonkey: initialization completed(篡改猴插件初始化完成,篡改猴图标已显示在页面右侧,鼠标移到上面可展示功能列表!)")
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("ds_tampermonkey: initialization failed(篡改猴插件初始化失败):", e);
|
console.error("ds_tampermonkey: initialization failed(篡改猴插件初始化失败):", e);
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
|
//endregion DS自定义的API
|
||||||
|
|
||||||
|
|
||||||
|
//region 篡改猴标准API,由DS自定义实现
|
||||||
|
|
||||||
// 注册菜单
|
// 注册菜单
|
||||||
GM_registerMenuCommand: (name, callback, options_or_accessKey) => {
|
api.GM_registerMenuCommand = (name, callback, options_or_accessKey) => {
|
||||||
const options = typeof options_or_accessKey === "string" ? { accessKey: options_or_accessKey } : options_or_accessKey;
|
const options = typeof options_or_accessKey === "string" ? { accessKey: options_or_accessKey } : options_or_accessKey;
|
||||||
|
|
||||||
// 生成菜单ID
|
// 生成菜单ID
|
||||||
|
@ -248,9 +263,10 @@
|
||||||
|
|
||||||
// 返回菜单ID
|
// 返回菜单ID
|
||||||
return menuCmdId;
|
return menuCmdId;
|
||||||
},
|
};
|
||||||
|
|
||||||
// 删除菜单
|
// 删除菜单
|
||||||
GM_unregisterMenuCommand: (menuCmdId) => {
|
api.GM_unregisterMenuCommand = (menuCmdId) => {
|
||||||
if (menuCmdId == null) {
|
if (menuCmdId == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -264,17 +280,19 @@
|
||||||
menuElement.remove();
|
menuElement.remove();
|
||||||
}
|
}
|
||||||
delete context.menus[menuCmdId];
|
delete context.menus[menuCmdId];
|
||||||
},
|
};
|
||||||
|
|
||||||
// 打开新标签
|
// 打开新标签
|
||||||
GM_openInTab: (url, options_or_loadInBackground) => {
|
api.GM_openInTab = (url, options_or_loadInBackground) => {
|
||||||
// const options = typeof options_or_loadInBackground === "boolean"
|
// const options = typeof options_or_loadInBackground === "boolean"
|
||||||
// ? { loadInBackground: options_or_loadInBackground }
|
// ? { loadInBackground: options_or_loadInBackground }
|
||||||
// : (options_or_loadInBackground || {});
|
// : (options_or_loadInBackground || {});
|
||||||
|
|
||||||
window.open(url)
|
window.open(url)
|
||||||
},
|
};
|
||||||
|
|
||||||
// 获取配置
|
// 获取配置
|
||||||
GM_getValue: (key, defaultValue) => {
|
api.GM_getValue = (key, defaultValue) => {
|
||||||
key = PRE + key;
|
key = PRE + key;
|
||||||
const valueStr = localStorage.getItem(key);
|
const valueStr = localStorage.getItem(key);
|
||||||
if (valueStr == null || valueStr === '') {
|
if (valueStr == null || valueStr === '') {
|
||||||
|
@ -285,19 +303,22 @@
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
}
|
}
|
||||||
return valueStr;
|
return valueStr;
|
||||||
},
|
};
|
||||||
|
|
||||||
// 设置配置
|
// 设置配置
|
||||||
GM_setValue: (key, value) => {
|
api.GM_setValue = (key, value) => {
|
||||||
key = PRE + key;
|
key = PRE + key;
|
||||||
localStorage.setItem(key, JSON.stringify({ v: value }));
|
localStorage.setItem(key, JSON.stringify({ v: value }));
|
||||||
},
|
};
|
||||||
|
|
||||||
// 删除设置
|
// 删除设置
|
||||||
GM_deleteValue: (key) => {
|
api.GM_deleteValue = (key) => {
|
||||||
key = PRE + key;
|
key = PRE + key;
|
||||||
localStorage.removeItem(key);
|
localStorage.removeItem(key);
|
||||||
},
|
};
|
||||||
|
|
||||||
// 通知
|
// 通知
|
||||||
GM_notification: (details_or_text, ondone_or_title, image, onclick) => {
|
api.GM_notification = (details_or_text, ondone_or_title, image, onclick) => {
|
||||||
// param1
|
// param1
|
||||||
let options = typeof details_or_text === "string" ? { text: details_or_text } : details_or_text;
|
let options = typeof details_or_text === "string" ? { text: details_or_text } : details_or_text;
|
||||||
if (typeof options !== "object") {
|
if (typeof options !== "object") {
|
||||||
|
@ -383,7 +404,19 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//endregion 篡改猴标准API,由DS自定义实现
|
||||||
|
|
||||||
|
|
||||||
|
// 设置脚本环境
|
||||||
|
window.__ds_global__ = api;
|
||||||
|
|
||||||
|
// 模块化支持
|
||||||
|
if (typeof module !== 'undefined') {
|
||||||
|
module.exports = api;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
console.log("ds_tampermonkey: completed")
|
||||||
})();
|
})();
|
||||||
console.log("ds_tampermonkey: completed")
|
console.log("ds_tampermonkey: loaded")
|
Loading…
Reference in New Issue