网站好帮手

本脚本提供了一系列小工具,在您访问互联网网站的时候加快您访问网站的速度和提高您的工作效率。包括删除服务器重定向(支持全网网站)

当前为 2021-01-02 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 网站好帮手
  3. // @name:en Website helper
  4. // @name:zh-CN 网站好帮手
  5. // @namespace https://github.com/kingphoenix2000/tampermonkey_scripts
  6. // @supportURL https://github.com/kingphoenix2000/tampermonkey_scripts
  7. // @version 0.1.6
  8. // @author 浴火凤凰(QQ:307053741,油猴脚本讨论QQ群:194885662)
  9. // @description 本脚本提供了一系列小工具,在您访问互联网网站的时候加快您访问网站的速度和提高您的工作效率。包括删除服务器重定向(支持全网网站)
  10. // @description:en This script provides a series of small tools to speed up the speed of your visit to the website and improve your work efficiency.
  11. // @description:zh-CN 本脚本提供了一系列小工具,在您访问互联网网站的时候加快您访问网站的速度和提高您的工作效率。包括删除服务器重定向(支持全网网站)
  12. // @homepage https://blog.csdn.net/kingwolf_javascript/
  13. // @include *
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_registerMenuCommand
  17. // @noframes
  18. // @note 2020-04-10:添加删除服务器跳转功能。
  19. // @note 2020-04-12:修改域名解析函数。
  20. // @note 2020-04-19:增加 回到顶部 功能。
  21. // ==/UserScript==
  22.  
  23. (function () {
  24. 'use strict';
  25.  
  26. let div1 = document.createElement("div");
  27. div1.id = "returnToTop";
  28. div1.innerText = "回到顶部";
  29. div1.style.cssText = "display:none;font-size:15px;padding: 10px;bottom: 5px;right: 5px;z-index: 1000;background-color: gray;position: fixed;border-radius: 25px;text-align: center;cursor: pointer;color: #fff;";
  30. div1.onclick = function () { scrollTo(0, 0); }
  31. let height = document.documentElement.scrollHeight;
  32. if (height > 2000) { div1.style.display = "block"; }
  33. document.body.appendChild(div1);
  34.  
  35.  
  36. let WebSite = JSON.parse(GM_getValue("Phoenix_City_WebSite", false));
  37. // console.log(WebSite);
  38. let redirect_URL_list = [];
  39. if (WebSite) {
  40. //以下代码为升级版本的兼容性代码
  41. if (!WebSite.Redirect_URL_List) {
  42. for (let domain in WebSite) {
  43. let links = WebSite[domain].removeURLList;
  44. for (let i = 0; i < links.length; i++) {
  45. let url = links[i];
  46. if (!redirect_URL_list.includes(url)) {
  47. redirect_URL_list.push(url);
  48. }
  49. }
  50. }
  51. WebSite = {};
  52. WebSite.Redirect_URL_List = redirect_URL_list;
  53. console.log(WebSite);
  54. GM_setValue("Phoenix_City_WebSite", JSON.stringify(WebSite));
  55. }
  56. }
  57. else {
  58. WebSite = {};
  59. WebSite.Redirect_URL_List = redirect_URL_list;
  60. }
  61. console.log(WebSite);
  62.  
  63. let num = 0;
  64. function removeRedirectURL(msg, links) {
  65. let len1 = WebSite.Redirect_URL_List.length;
  66. for (let i = 0; i < links.length; i++) {
  67. let href = links[i].href;
  68. if (!href.startsWith("http://") && !href.startsWith("https://")) { continue; }
  69. for (let j = 0; j < len1; j++) {
  70. const url = WebSite.Redirect_URL_List[j];
  71. //只删除 网址开头 是重定向URL的链接,并且只删除一个重定向URL
  72. if (href.startsWith(url)) {
  73. href = href.replace(url, ''); break;
  74. }
  75. }
  76. if (href.startsWith("http")) {
  77. try {
  78. href = decodeURIComponent(href);
  79. links[i].href = href;
  80. }
  81. catch (e) {
  82. console.log(`URL解码发生错误:${href}`);
  83. }
  84. }
  85. else {
  86. console.log("发现类似重定向,实际上不是重定向的网址: ", links[i].href);
  87. }
  88. }
  89. num = num + links.length;
  90. console.log(msg, "目前已经删除重定向的链接数:", num);
  91. }
  92. //首先删除已经加载的超链接的重定向
  93. removeRedirectURL("页面初始化阶段-->", document.links);
  94.  
  95.  
  96. const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  97. var mutationObserver = new MutationObserver(function (mutations, instance) {
  98. //console.log(instance);
  99. mutations.forEach(function (mutation) {
  100. let addedNodes = mutation.addedNodes;
  101. if (addedNodes.length > 0) {
  102. addedNodes.forEach(function (node) {
  103. if (node.nodeType == 1) {
  104. removeRedirectURL("页面链接动态加载阶段-->", node.querySelectorAll("a"));
  105. }
  106. });
  107. }
  108. });
  109. });
  110.  
  111. // 开始监听页面根元素 HTML 变化。
  112. mutationObserver.observe(document.body, {
  113. childList: true,
  114. subtree: true
  115. });
  116. GM_registerMenuCommand("添加重定向链接", () => {
  117. let url = prompt("链接必须以http或者https开头,并且带有?和=\n类似于:https://link.zhihu.com/?target=", "");
  118. if (url) {
  119. if (WebSite.Redirect_URL_List.includes(url)) {
  120. console.log(`要添加的重定向URL${url}已经存在。`);
  121. }
  122. else {
  123. WebSite.Redirect_URL_List.push(url);
  124. GM_setValue("Phoenix_City_WebSite", JSON.stringify(WebSite));
  125. console.log(`重定向URL${url} 添加成功。`);
  126. }
  127. }
  128. });
  129. GM_registerMenuCommand("删除重定向链接", () => {
  130. let url = prompt("链接必须以http或者https开头,并且带有?和=\n类似于:https://link.zhihu.com/?target=", "");
  131. if (url) {
  132. let index = WebSite.Redirect_URL_List.indexOf(url);
  133. if (index == -1) {
  134. console.log(`找不到要删除的重定向URL${url}。`);
  135. }
  136. else {
  137. WebSite.Redirect_URL_List.splice(index, 1);
  138. GM_setValue("Phoenix_City_WebSite", JSON.stringify(WebSite));
  139. console.log(`重定向URL${url} 删除成功。`);
  140. }
  141. }
  142. });
  143.  
  144. })();