优化油猴脚本。
parent
2fde1a5810
commit
7fc5a1fed0
|
@ -5,10 +5,10 @@
|
||||||
// @name:en Github Enhancement - High Speed Download
|
// @name:en Github Enhancement - High Speed Download
|
||||||
// @version 2.5.20
|
// @version 2.5.20
|
||||||
// @author X.I.U
|
// @author X.I.U
|
||||||
// @description 高速下载 Git Clone/SSH、Release、Raw、Code(ZIP) 等文件 (公益加速)、项目列表单文件快捷下载 (☁)、添加 git clone 命令
|
// @description 高速下载 Git Clone/SSH、Release、Raw、Code(ZIP) 等文件 (公益加速)、项目列表单文件快捷下载 (☁)、添加 git clone 命令
|
||||||
// @description:zh-CN 高速下载 Git Clone/SSH、Release、Raw、Code(ZIP) 等文件 (公益加速)、项目列表单文件快捷下载 (☁)
|
// @description:zh-CN 高速下载 Git Clone/SSH、Release、Raw、Code(ZIP) 等文件 (公益加速)、项目列表单文件快捷下载 (☁)
|
||||||
// @description:zh-TW 高速下載 Git Clone/SSH、Release、Raw、Code(ZIP) 等文件 (公益加速)、項目列表單文件快捷下載 (☁)
|
// @description:zh-TW 高速下載 Git Clone/SSH、Release、Raw、Code(ZIP) 等文件 (公益加速)、項目列表單文件快捷下載 (☁)
|
||||||
// @description:en High-speed download of Git Clone/SSH, Release, Raw, Code(ZIP) and other files (Based on public welfare), project list file quick download (☁)
|
// @description:en High-speed download of Git Clone/SSH, Release, Raw, Code(ZIP) and other files (Based on public welfare), project list file quick download (☁)
|
||||||
// @match *://github.com/*
|
// @match *://github.com/*
|
||||||
// @match *://hub.incept.pw/*
|
// @match *://hub.incept.pw/*
|
||||||
// @match *://hub.nuaa.cf/*
|
// @match *://hub.nuaa.cf/*
|
||||||
|
@ -59,15 +59,19 @@
|
||||||
['https://download.scholar.rr.nu', '美国', '[美国 纽约] - 该公益加速源由 [FastGit 群组成员] 提供'],
|
['https://download.scholar.rr.nu', '美国', '[美国 纽约] - 该公益加速源由 [FastGit 群组成员] 提供'],
|
||||||
//['https://download.njuu.cf', '美国', '[美国 纽约] - 该公益加速源由 [FastGit 群组成员] 提供'], // 域名挂了
|
//['https://download.njuu.cf', '美国', '[美国 纽约] - 该公益加速源由 [FastGit 群组成员] 提供'], // 域名挂了
|
||||||
['https://download.yzuu.cf', '美国', '[美国 纽约] - 该公益加速源由 [FastGit 群组成员] 提供']
|
['https://download.yzuu.cf', '美国', '[美国 纽约] - 该公益加速源由 [FastGit 群组成员] 提供']
|
||||||
], download_url = [
|
];
|
||||||
|
|
||||||
|
const download_url = [
|
||||||
//['https://download.fastgit.org', '德国', '[德国] - 该公益加速源由 [FastGit] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 4 个来负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~', 'https://archive.fastgit.org'], // 证书过期
|
//['https://download.fastgit.org', '德国', '[德国] - 该公益加速源由 [FastGit] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 4 个来负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~', 'https://archive.fastgit.org'], // 证书过期
|
||||||
['https://mirror.ghproxy.com/https://github.com', '韩国', '[日本、韩国、德国等](CDN 不固定) - 该公益加速源由 [ghproxy] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~'],
|
['https://mirror.ghproxy.com/https://github.com', '韩国', '[日本、韩国、德国等](CDN 不固定) - 该公益加速源由 [ghproxy] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~'],
|
||||||
['https://ghproxy.net/https://github.com', '日本', '[日本 大阪] - 该公益加速源由 [ghproxy] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~'],
|
['https://ghproxy.net/https://github.com', '日本', '[日本 大阪] - 该公益加速源由 [ghproxy] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~'],
|
||||||
['https://kkgithub.com', '香港', '[中国香港、日本、新加坡等] - 该公益加速源由 [help.kkgithub.com] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 4 个来负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~'],
|
//['https://kkgithub.com', '香港', '[中国香港、日本、新加坡等] - 该公益加速源由 [help.kkgithub.com] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 4 个来负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~'],
|
||||||
//['https://download.incept.pw', '香港', '[中国香港] - 该公益加速源由 [FastGit 群组成员] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 4 个来负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~'] // ERR_SSL_PROTOCOL_ERROR
|
//['https://download.incept.pw', '香港', '[中国香港] - 该公益加速源由 [FastGit 群组成员] 提供 提示:希望大家尽量多使用前面的美国节点(每次随机 4 个来负载均衡), 避免流量都集中到亚洲公益节点,减少成本压力,公益才能更持久~'] // ERR_SSL_PROTOCOL_ERROR
|
||||||
], clone_url = [
|
];
|
||||||
|
|
||||||
|
const clone_url = [
|
||||||
['https://gitclone.com', '国内', '[中国 国内] - 该公益加速源由 [GitClone] 提供 - 缓存:有 - 首次比较慢,缓存后较快'],
|
['https://gitclone.com', '国内', '[中国 国内] - 该公益加速源由 [GitClone] 提供 - 缓存:有 - 首次比较慢,缓存后较快'],
|
||||||
['https://kkgithub.com', '香港', '[中国香港、日本、新加坡等] - 该公益加速源由 [help.kkgithub.com] 提供 - 缓存:无(或时间很短)'],
|
//['https://kkgithub.com', '香港', '[中国香港、日本、新加坡等] - 该公益加速源由 [help.kkgithub.com] 提供 - 缓存:无(或时间很短)'],
|
||||||
['https://hub.incept.pw', '香港', '[中国香港、美国] - 该公益加速源由 [FastGit 群组成员] 提供'],
|
['https://hub.incept.pw', '香港', '[中国香港、美国] - 该公益加速源由 [FastGit 群组成员] 提供'],
|
||||||
['https://mirror.ghproxy.com/https://github.com', '韩国', '[日本、韩国、德国等](CDN 不固定) - 该公益加速源由 [ghproxy] 提供 - 缓存:无(或时间很短)'],
|
['https://mirror.ghproxy.com/https://github.com', '韩国', '[日本、韩国、德国等](CDN 不固定) - 该公益加速源由 [ghproxy] 提供 - 缓存:无(或时间很短)'],
|
||||||
//['https://gh-proxy.com/https://github.com', '韩国', '[韩国] - 该公益加速源由 [ghproxy] 提供 - 缓存:无(或时间很短)'],
|
//['https://gh-proxy.com/https://github.com', '韩国', '[韩国] - 该公益加速源由 [ghproxy] 提供 - 缓存:无(或时间很短)'],
|
||||||
|
@ -88,11 +92,15 @@
|
||||||
//['https://hub.yzuu.cf', '美国', '[美国 纽约] - 该公益加速源由 [FastGit 群组成员] 提供'], // 暂无必要
|
//['https://hub.yzuu.cf', '美国', '[美国 纽约] - 该公益加速源由 [FastGit 群组成员] 提供'], // 暂无必要
|
||||||
//['https://hub.0z.gs', '美国', '[美国 Cloudflare CDN]'], // 域名无解析
|
//['https://hub.0z.gs', '美国', '[美国 Cloudflare CDN]'], // 域名无解析
|
||||||
//['https://hub.shutcm.cf', '美国', '[美国 Cloudflare CDN]'] // 连接超时
|
//['https://hub.shutcm.cf', '美国', '[美国 Cloudflare CDN]'] // 连接超时
|
||||||
], clone_ssh_url = [
|
];
|
||||||
|
|
||||||
|
const clone_ssh_url = [
|
||||||
['ssh://git@ssh.github.com:443/', 'Github 原生', '[日本、新加坡等] - Github 官方提供的 443 端口的 SSH(依然是 SSH 协议),适用于限制访问 22 端口的网络环境'],
|
['ssh://git@ssh.github.com:443/', 'Github 原生', '[日本、新加坡等] - Github 官方提供的 443 端口的 SSH(依然是 SSH 协议),适用于限制访问 22 端口的网络环境'],
|
||||||
['git@ssh.fastgit.org:', '香港', '[中国 香港] - 该公益加速源由 [FastGit] 提供']
|
['git@ssh.fastgit.org:', '香港', '[中国 香港] - 该公益加速源由 [FastGit] 提供']
|
||||||
//['git@git.zhlh6.cn:', '美国', '[美国 洛杉矶]'] // 挂了
|
//['git@git.zhlh6.cn:', '美国', '[美国 洛杉矶]'] // 挂了
|
||||||
], raw_url = [
|
];
|
||||||
|
|
||||||
|
const raw_url = [
|
||||||
['https://raw.githubusercontent.com', 'Github 原生', '[日本 东京]'],
|
['https://raw.githubusercontent.com', 'Github 原生', '[日本 东京]'],
|
||||||
//['https://raw.kkgithub.com', '香港', '[中国香港、日本、新加坡等] - 该公益加速源由 [help.kkgithub.com] 提供 - 缓存:无(或时间很短)'],
|
//['https://raw.kkgithub.com', '香港', '[中国香港、日本、新加坡等] - 该公益加速源由 [help.kkgithub.com] 提供 - 缓存:无(或时间很短)'],
|
||||||
['https://mirror.ghproxy.com/https://raw.githubusercontent.com', '韩国', '[日本、韩国、德国等](CDN 不固定) - 该公益加速源由 [ghproxy] 提供 - 缓存:无(或时间很短)'],
|
['https://mirror.ghproxy.com/https://raw.githubusercontent.com', '韩国', '[日本、韩国、德国等](CDN 不固定) - 该公益加速源由 [ghproxy] 提供 - 缓存:无(或时间很短)'],
|
||||||
|
@ -119,7 +127,9 @@
|
||||||
//['https://cdn.54188.cf/gh', '美国', '[美国 Cloudflare CDN] - 该公益加速源由 [PencilNavigator] 提供 - 缓存:有'], // 暂无必要
|
//['https://cdn.54188.cf/gh', '美国', '[美国 Cloudflare CDN] - 该公益加速源由 [PencilNavigator] 提供 - 缓存:有'], // 暂无必要
|
||||||
//['https://raw.fastgit.org', '德国', '[德国] - 该公益加速源由 [FastGit] 提供 - 缓存:无(或时间很短)'], // 挂了
|
//['https://raw.fastgit.org', '德国', '[德国] - 该公益加速源由 [FastGit] 提供 - 缓存:无(或时间很短)'], // 挂了
|
||||||
//['https://git.yumenaka.net/https://raw.githubusercontent.com', '美国', '[美国 圣何塞] - 缓存:无(或时间很短)'], // 连接超时
|
//['https://git.yumenaka.net/https://raw.githubusercontent.com', '美国', '[美国 圣何塞] - 缓存:无(或时间很短)'], // 连接超时
|
||||||
], svg = [
|
];
|
||||||
|
|
||||||
|
const svg = [
|
||||||
'<svg class="octicon octicon-cloud-download" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M9 12h2l-3 3-3-3h2V7h2v5zm3-8c0-.44-.91-3-4.5-3C5.08 1 3 2.92 3 5 1.02 5 0 6.52 0 8c0 1.53 1 3 3 3h3V9.7H3C1.38 9.7 1.3 8.28 1.3 8c0-.17.05-1.7 1.7-1.7h1.3V5c0-1.39 1.56-2.7 3.2-2.7 2.55 0 3.13 1.55 3.2 1.8v1.2H12c.81 0 2.7.22 2.7 2.2 0 2.09-2.25 2.2-2.7 2.2h-2V11h2c2.08 0 4-1.16 4-3.5C16 5.06 14.08 4 12 4z"></path></svg>'
|
'<svg class="octicon octicon-cloud-download" aria-hidden="true" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path d="M9 12h2l-3 3-3-3h2V7h2v5zm3-8c0-.44-.91-3-4.5-3C5.08 1 3 2.92 3 5 1.02 5 0 6.52 0 8c0 1.53 1 3 3 3h3V9.7H3C1.38 9.7 1.3 8.28 1.3 8c0-.17.05-1.7 1.7-1.7h1.3V5c0-1.39 1.56-2.7 3.2-2.7 2.55 0 3.13 1.55 3.2 1.8v1.2H12c.81 0 2.7.22 2.7 2.2 0 2.09-2.25 2.2-2.7 2.2h-2V11h2c2.08 0 4-1.16 4-3.5C16 5.06 14.08 4 12 4z"></path></svg>'
|
||||||
], style = ['padding:0 6px; margin-right: -1px; border-radius: 2px; background-color: var(--XIU2-back-Color); border-color: rgba(27, 31, 35, 0.1); font-size: 11px; color: var(--XIU2-font-Color);'];
|
], style = ['padding:0 6px; margin-right: -1px; border-radius: 2px; background-color: var(--XIU2-back-Color); border-color: rgba(27, 31, 35, 0.1); font-size: 11px; color: var(--XIU2-font-Color);'];
|
||||||
|
|
||||||
|
@ -133,10 +143,10 @@
|
||||||
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');}
|
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 数组后,用户储存的数据大于数组而报错
|
// 避免在减少 raw 数组后,用户储存的数据大于数组而报错
|
||||||
if (menu_rawFast > raw_url.length - 1) menu_rawFast = 0
|
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();});
|
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);
|
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();});
|
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});});
|
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: "点击前往反馈问题或提出建议"});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 切换加速源
|
// 切换加速源
|
||||||
|
|
|
@ -1,16 +1,21 @@
|
||||||
/**
|
/**
|
||||||
|
* 当前脚本为仿照的版本,并非篡改猴插件的源码,仅供学习参考。
|
||||||
|
*
|
||||||
* @name 篡改猴(Tampermonkey)| 油猴(Greasemonkey)浏览器脚本扩展
|
* @name 篡改猴(Tampermonkey)| 油猴(Greasemonkey)浏览器脚本扩展
|
||||||
* @description 篡改猴 (Tampermonkey) 是拥有 超过 1000 万用户 的最流行的浏览器扩展之一。 它适用于 Chrome、Microsoft Edge、Safari、Opera Next 和 Firefox。
|
* @author 由 Wang Liang(王良)仿照的
|
||||||
* 有些人也会把篡改猴(Tampermonkey)称作油猴(Greasemonkey),尽管后者只是一款仅适用于 Firefox 浏览器的浏览器扩展程序。
|
* @authorHomePage https://wangliang1024.cn
|
||||||
* 它允许用户自定义并增强您最喜爱的网页的功能。用户脚本是小型 JavaScript 程序,可用于向网页添加新功能或修改现有功能。使用 篡改猴,您可以轻松在任何网站上创建、管理和运行这些用户脚本。
|
* @description 篡改猴 (Tampermonkey) 是拥有 超过 1000 万用户 的最流行的浏览器扩展之一。 它适用于 Chrome、Microsoft Edge、Safari、Opera Next 和 Firefox。
|
||||||
* 例如,使用 篡改猴,您可以向网页添加一个新按钮,可以快速在社交媒体上分享链接,或自动填写带有个人信息的表格。在数字化时代,这特别有用,因为网页常常被用作访问广泛的服务和应用程序的用户界面。
|
* 有些人也会把篡改猴(Tampermonkey)称作油猴(Greasemonkey),尽管后者只是一款仅适用于 Firefox 浏览器的浏览器扩展程序。
|
||||||
* 此外,篡改猴 使您轻松找到并安装其他用户创建的用户脚本。这意味着您可以快速轻松地访问为您喜爱的网页定制的广泛库,而无需花费数小时编写自己的代码。
|
* 它允许用户自定义并增强您最喜爱的网页的功能。用户脚本是小型 JavaScript 程序,可用于向网页添加新功能或修改现有功能。使用 篡改猴,您可以轻松在任何网站上创建、管理和运行这些用户脚本。
|
||||||
* 无论您是希望为您的站点添加新功能的 Web 开发人员,还是只是希望 改善在线体验的普通用户,篡改猴 都是您的工具箱中的一个很好的工具。
|
* 例如,使用 篡改猴,您可以向网页添加一个新按钮,可以快速在社交媒体上分享链接,或自动填写带有个人信息的表格。在数字化时代,这特别有用,因为网页常常被用作访问广泛的服务和应用程序的用户界面。
|
||||||
* @homepageUrl https://www.tampermonkey.net
|
* 此外,篡改猴 使您轻松找到并安装其他用户创建的用户脚本。这意味着您可以快速轻松地访问为您喜爱的网页定制的广泛库,而无需花费数小时编写自己的代码。
|
||||||
|
* 无论您是希望为您的站点添加新功能的 Web 开发人员,还是只是希望 改善在线体验的普通用户,篡改猴 都是您的工具箱中的一个很好的工具。
|
||||||
|
* @homepageUrl https://www.tampermonkey.net
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
(function () {
|
(function () {
|
||||||
const PRE = "DS-Tampermonkey:"; // 前缀
|
const PRE = "DS-Tampermonkey:"; // 前缀
|
||||||
|
const MENU_ID_PRE = PRE + "menu-";
|
||||||
|
|
||||||
const context = {
|
const context = {
|
||||||
initialized: false, // 是否已经初始化
|
initialized: false, // 是否已经初始化
|
||||||
|
@ -18,11 +23,27 @@
|
||||||
pluginElement: null, // 插件div
|
pluginElement: null, // 插件div
|
||||||
menusElement: null, // 菜单列表div
|
menusElement: null, // 菜单列表div
|
||||||
menus: {}, // 菜单集合
|
menus: {}, // 菜单集合
|
||||||
menuIndex: 0 // 菜单索引,用于生成menuCmdId
|
menuIndex: 0, // 菜单索引,用于生成menuCmdId
|
||||||
|
lastNotification: null // 最后一次通知
|
||||||
|
/*{
|
||||||
|
obj: null, // 通知对象
|
||||||
|
options: null, // 通知选项
|
||||||
|
timeout: null // 通知定时器
|
||||||
|
}*/
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// 创建插件API
|
||||||
|
const api = {};
|
||||||
|
|
||||||
|
|
||||||
|
//region DS自定义的API start
|
||||||
|
|
||||||
|
// 获取上下文
|
||||||
|
api.getContext = () => context;
|
||||||
|
|
||||||
// 创建插件样式
|
// 创建插件样式
|
||||||
function createPluginStyle (options) {
|
api.createPluginStyle = (options) => {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
// 创建一个新的<style>元素
|
// 创建一个新的<style>元素
|
||||||
|
@ -100,10 +121,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
|
||||||
|
@ -117,11 +138,14 @@
|
||||||
// 创建菜单列表div
|
// 创建菜单列表div
|
||||||
context.menusElement = document.createElement('div');
|
context.menusElement = document.createElement('div');
|
||||||
context.menusElement.className = "____ds-menus____";
|
context.menusElement.className = "____ds-menus____";
|
||||||
|
if (options.width > 0) {
|
||||||
|
context.menusElement.style['min-width'] = options.width + "px";
|
||||||
|
}
|
||||||
// 将菜单列表div添加到插件div中
|
// 将菜单列表div添加到插件div中
|
||||||
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";
|
||||||
|
@ -129,18 +153,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
|
||||||
});
|
});
|
||||||
|
@ -154,182 +178,244 @@
|
||||||
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__ = {
|
// 初始化篡改猴操作界面
|
||||||
// 获取上下文
|
api.DS_init = (options) => {
|
||||||
getContext: () => {
|
try {
|
||||||
return context;
|
if (context.initialized) return;
|
||||||
},
|
// 合并默认参数
|
||||||
// 初始化
|
options = {
|
||||||
DS_init: (options) => {
|
...context.defaultPluginOptions,
|
||||||
try {
|
...options
|
||||||
if (context.initialized) return;
|
|
||||||
// 合并默认参数
|
|
||||||
options = {
|
|
||||||
...context.defaultPluginOptions,
|
|
||||||
...options
|
|
||||||
};
|
|
||||||
createPluginStyle(options);
|
|
||||||
createPluginDiv(options);
|
|
||||||
context.initialized = true;
|
|
||||||
|
|
||||||
console.log("ds_tampermonkey: initialization completed")
|
|
||||||
} catch (e) {
|
|
||||||
console.error("ds_tampermonkey: initialization failed:", e);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// 注册菜单
|
|
||||||
GM_registerMenuCommand: (name, callback, options_or_accessKey) => {
|
|
||||||
const options = typeof options_or_accessKey === "string" ? { accessKey: options_or_accessKey } : options_or_accessKey;
|
|
||||||
|
|
||||||
// 生成菜单ID
|
|
||||||
const menuCmdId = PRE + "menu-" + (++context.menuIndex);
|
|
||||||
|
|
||||||
// 创建菜单元素
|
|
||||||
const menuElement = document.createElement('div');
|
|
||||||
menuElement.id = menuCmdId;
|
|
||||||
menuElement.className = "____ds-menu____";
|
|
||||||
menuElement.innerHTML = name;
|
|
||||||
if (callback) {
|
|
||||||
menuElement.onclick = callback;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 将菜单元素添加到菜单列表div中
|
|
||||||
context.menusElement.append(menuElement);
|
|
||||||
|
|
||||||
// 将菜单添加到菜单集合中
|
|
||||||
context.menus[menuCmdId] = {
|
|
||||||
name: name,
|
|
||||||
callback: callback,
|
|
||||||
options: options,
|
|
||||||
element: menuElement
|
|
||||||
};
|
};
|
||||||
|
api.createPluginStyle(options);
|
||||||
|
api.createPluginDiv(options);
|
||||||
|
context.initialized = true;
|
||||||
|
|
||||||
// 返回菜单ID
|
console.log("ds_tampermonkey: initialization completed(篡改猴插件初始化完成,篡改猴图标已显示在页面右侧,鼠标移到上面可展示功能列表!)")
|
||||||
return menuCmdId;
|
} catch (e) {
|
||||||
},
|
console.error("ds_tampermonkey: initialization failed(篡改猴插件初始化失败):", e);
|
||||||
// 删除菜单
|
|
||||||
GM_unregisterMenuCommand: (menuCmdId) => {
|
|
||||||
const menuElement = document.getElementById(menuCmdId)
|
|
||||||
if (menuElement) {
|
|
||||||
menuElement.remove();
|
|
||||||
}
|
|
||||||
delete context.menus[menuCmdId];
|
|
||||||
},
|
|
||||||
// 打开新标签
|
|
||||||
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) => {
|
|
||||||
key = PRE + key;
|
|
||||||
const valueStr = localStorage.getItem(key);
|
|
||||||
if (valueStr == null || valueStr === '') {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return JSON.parse(valueStr).v;
|
|
||||||
} catch (e) {
|
|
||||||
}
|
|
||||||
return valueStr;
|
|
||||||
},
|
|
||||||
// 设置配置
|
|
||||||
GM_setValue: (key, value) => {
|
|
||||||
key = PRE + key;
|
|
||||||
localStorage.setItem(key, JSON.stringify({ v: value }));
|
|
||||||
},
|
|
||||||
// 删除设置
|
|
||||||
GM_deleteValue: (key) => {
|
|
||||||
key = PRE + key;
|
|
||||||
localStorage.removeItem(key);
|
|
||||||
},
|
|
||||||
// 通知
|
|
||||||
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") {
|
|
||||||
console.error("GM_notification: 无效的参数值:details_or_text = " + details_or_text);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// param2
|
|
||||||
if (typeof ondone_or_title === "string") {
|
|
||||||
options.title = ondone_or_title;
|
|
||||||
} else if (typeof ondone_or_title === "function") {
|
|
||||||
options.ondone = ondone_or_title;
|
|
||||||
} else if (ondone_or_title != null) {
|
|
||||||
console.warn("GM_notification: 无效的参数值:ondone_or_title = " + ondone_or_title);
|
|
||||||
}
|
|
||||||
// param3
|
|
||||||
if (typeof image === "string") {
|
|
||||||
options.image = image;
|
|
||||||
} else if (onclick != null) {
|
|
||||||
console.warn("GM_notification: 无效的参数值:image = " + image);
|
|
||||||
}
|
|
||||||
// param4
|
|
||||||
if (typeof onclick === "function") {
|
|
||||||
options.onclick = onclick;
|
|
||||||
} else if (onclick != null) {
|
|
||||||
console.warn("GM_notification: 无效的参数值:onclick = " + onclick);
|
|
||||||
}
|
|
||||||
|
|
||||||
let text = options.text;
|
|
||||||
if (options.title) {
|
|
||||||
text = options.title + ": " + text;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 显示通知方法
|
|
||||||
const showNotification = () => {
|
|
||||||
const notification = new Notification(text);
|
|
||||||
if (options.timeout) {
|
|
||||||
setTimeout(function () {
|
|
||||||
notification.close();
|
|
||||||
if (options.ondone) options.ondone(); // 回调
|
|
||||||
}, options.timeout)
|
|
||||||
}
|
|
||||||
return notification;
|
|
||||||
};
|
|
||||||
const showAlert = () => {
|
|
||||||
alert(text);
|
|
||||||
if (options.ondone) options.ondone(); // 回调
|
|
||||||
};
|
|
||||||
|
|
||||||
// 检查浏览器是否支持Notification API
|
|
||||||
if (!("Notification" in window)) {
|
|
||||||
showAlert(); // 不支持,直接使用alert显示通知
|
|
||||||
}
|
|
||||||
// 检查用户是否已授予权限
|
|
||||||
else if (Notification.permission === "granted") {
|
|
||||||
// 如果用户已授予权限,我们可以显示通知
|
|
||||||
showNotification();
|
|
||||||
}
|
|
||||||
// 否则,先请求权限
|
|
||||||
else if (Notification.permission !== 'denied') {
|
|
||||||
Notification.requestPermission(function (permission) {
|
|
||||||
if (permission === "granted") {
|
|
||||||
showNotification(); // 用户接受权限,我们可以显示通知
|
|
||||||
} else {
|
|
||||||
showAlert(); // 用户驳回了权限,直接使用alert显示通知
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//endregion DS自定义的API end
|
||||||
|
|
||||||
|
|
||||||
|
//region 篡改猴标准API,由DS自定义实现 start
|
||||||
|
|
||||||
|
// 注册菜单
|
||||||
|
api.GM_registerMenuCommand = (name, callback, options_or_accessKey) => {
|
||||||
|
const options = typeof options_or_accessKey === "string" ? { accessKey: options_or_accessKey } : options_or_accessKey;
|
||||||
|
|
||||||
|
// 生成菜单ID
|
||||||
|
let menuCmdId;
|
||||||
|
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 + (++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);
|
||||||
|
|
||||||
|
// 将菜单添加到菜单集合中
|
||||||
|
context.menus[menuCmdId] = {
|
||||||
|
name: name,
|
||||||
|
callback: callback,
|
||||||
|
options: options,
|
||||||
|
element: menuElement
|
||||||
|
};
|
||||||
|
|
||||||
|
// 返回菜单ID
|
||||||
|
return menuCmdId;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 删除菜单
|
||||||
|
api.GM_unregisterMenuCommand = (menuCmdId) => {
|
||||||
|
if (menuCmdId == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (menuCmdId.indexOf(MENU_ID_PRE) !== 0) {
|
||||||
|
menuCmdId = MENU_ID_PRE + menuCmdId;
|
||||||
|
}
|
||||||
|
|
||||||
|
const menuElement = document.getElementById(menuCmdId)
|
||||||
|
if (menuElement) {
|
||||||
|
menuElement.remove();
|
||||||
|
}
|
||||||
|
delete context.menus[menuCmdId];
|
||||||
|
};
|
||||||
|
|
||||||
|
// 打开新标签
|
||||||
|
api.GM_openInTab = (url, options_or_loadInBackground) => {
|
||||||
|
// const options = typeof options_or_loadInBackground === "boolean"
|
||||||
|
// ? { loadInBackground: options_or_loadInBackground }
|
||||||
|
// : (options_or_loadInBackground || {});
|
||||||
|
|
||||||
|
window.open(url)
|
||||||
|
};
|
||||||
|
|
||||||
|
// 获取配置
|
||||||
|
api.GM_getValue = (key, defaultValue) => {
|
||||||
|
key = PRE + key;
|
||||||
|
const valueStr = localStorage.getItem(key);
|
||||||
|
if (valueStr == null || valueStr === '') {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return JSON.parse(valueStr).v;
|
||||||
|
} catch (e) {
|
||||||
|
}
|
||||||
|
return valueStr;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 设置配置
|
||||||
|
api.GM_setValue = (key, value) => {
|
||||||
|
key = PRE + key;
|
||||||
|
localStorage.setItem(key, JSON.stringify({ v: value }));
|
||||||
|
};
|
||||||
|
|
||||||
|
// 删除设置
|
||||||
|
api.GM_deleteValue = (key) => {
|
||||||
|
key = PRE + key;
|
||||||
|
localStorage.removeItem(key);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 通知
|
||||||
|
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") {
|
||||||
|
console.error("GM_notification: 无效的参数值:details_or_text = " + details_or_text);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// param2
|
||||||
|
if (typeof ondone_or_title === "string") {
|
||||||
|
options.title = ondone_or_title;
|
||||||
|
} else if (typeof ondone_or_title === "function") {
|
||||||
|
options.ondone = ondone_or_title;
|
||||||
|
} else if (ondone_or_title != null) {
|
||||||
|
console.warn("GM_notification: 无效的参数值:ondone_or_title = " + ondone_or_title);
|
||||||
|
}
|
||||||
|
// param3
|
||||||
|
if (typeof image === "string") {
|
||||||
|
options.image = image;
|
||||||
|
} else if (onclick != null) {
|
||||||
|
console.warn("GM_notification: 无效的参数值:image = " + image);
|
||||||
|
}
|
||||||
|
// param4
|
||||||
|
if (typeof onclick === "function") {
|
||||||
|
options.onclick = onclick;
|
||||||
|
} else if (onclick != null) {
|
||||||
|
console.warn("GM_notification: 无效的参数值:onclick = " + onclick);
|
||||||
|
}
|
||||||
|
|
||||||
|
let text = options.text;
|
||||||
|
if (options.title) {
|
||||||
|
text = options.title + ": " + text;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 显示通知方法
|
||||||
|
const showNotification = () => {
|
||||||
|
// 先关闭上一个通知
|
||||||
|
let lastNotification = context.lastNotification;
|
||||||
|
if (lastNotification) {
|
||||||
|
if (lastNotification.timeout) {
|
||||||
|
clearTimeout(lastNotification.timeout);
|
||||||
|
}
|
||||||
|
lastNotification.obj.close();
|
||||||
|
if (lastNotification.options && typeof lastNotification.options.ondone === "function") lastNotification.options.ondone();
|
||||||
|
context.lastNotification = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const notification = new Notification(text);
|
||||||
|
lastNotification = {
|
||||||
|
obj: notification,
|
||||||
|
options: options,
|
||||||
|
timeout: null
|
||||||
|
}
|
||||||
|
context.lastNotification = lastNotification;
|
||||||
|
if (options.timeout) {
|
||||||
|
lastNotification.timeout = setTimeout(function () {
|
||||||
|
context.lastNotification = null // 清除最后一个通知
|
||||||
|
notification.close();
|
||||||
|
if (options.ondone) options.ondone(); // 回调
|
||||||
|
}, options.timeout)
|
||||||
|
}
|
||||||
|
return notification;
|
||||||
|
};
|
||||||
|
const showAlert = () => {
|
||||||
|
alert(text);
|
||||||
|
if (options.ondone) options.ondone(); // 回调
|
||||||
|
};
|
||||||
|
|
||||||
|
// 检查浏览器是否支持Notification API
|
||||||
|
if (!("Notification" in window)) {
|
||||||
|
showAlert(); // 不支持,直接使用alert显示通知
|
||||||
|
}
|
||||||
|
// 检查用户是否已授予权限
|
||||||
|
else if (Notification.permission === "granted") {
|
||||||
|
// 如果用户已授予权限,我们可以显示通知
|
||||||
|
showNotification();
|
||||||
|
}
|
||||||
|
// 否则,先请求权限
|
||||||
|
else if (Notification.permission !== 'denied') {
|
||||||
|
Notification.requestPermission(function (permission) {
|
||||||
|
if (permission === "granted") {
|
||||||
|
showNotification(); // 用户接受权限,我们可以显示通知
|
||||||
|
} else {
|
||||||
|
showAlert(); // 用户驳回了权限,直接使用alert显示通知
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//endregion 篡改猴标准API,由DS自定义实现 end
|
||||||
|
|
||||||
|
|
||||||
|
// 设置API
|
||||||
|
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")
|
|
@ -18,6 +18,7 @@ function buildScript (sc, content, scriptName) {
|
||||||
// 代码2:初始化
|
// 代码2:初始化
|
||||||
const options = {
|
const options = {
|
||||||
name: sc.name,
|
name: sc.name,
|
||||||
|
version: sc.version,
|
||||||
icon: sc.icon
|
icon: sc.icon
|
||||||
}
|
}
|
||||||
const initStr = `
|
const initStr = `
|
||||||
|
|
Loading…
Reference in New Issue