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