Wanta

移除跳转外链提示

目前为 2022-10-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Wanta
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.1
  5. // @description 移除跳转外链提示
  6. // @author PRO
  7. // @match *://www.jianshu.com/p/*
  8. // @match *://juejin.cn/post/*
  9. // @match *://gitee.com/*
  10. // @icon https://greasyfork.org/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBMWhLQVE9PSIsImV4cCI6bnVsbCwicHVyIjoiYmxvYl9pZCJ9fQ==--2831c7f8ea43fc8b8e3eed3818b98e88bb689285/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202022-07-16%20105357.png?locale=zh-CN
  11. // @grant none
  12. // @license unlicense
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17. let debug = false;
  18. // domain: [link_prefix query_parameter main_article_path dynamic_query urldecode]
  19. let fuck = {
  20. 'www.jianshu.com': ['https://links.jianshu.com/go', 'to', 'article', '', true],
  21. 'juejin.cn': ['https://link.juejin.cn', 'target', '#juejin > div.view-container > main > div > div.main-area.article-area > article', ' > .article-content', true],
  22. 'gitee.com': ['https://gitee.com/link', 'target', '#git-readme > div > div.file_content.markdown-body', '', true]
  23. };
  24. let domain = document.domain;
  25. let suffix = fuck[domain][0];
  26. let query_name = fuck[domain][1];
  27. let main_path = fuck[domain][2];
  28. let dynamic = fuck[domain][3];
  29. let urldecode = fuck[domain][4];
  30. let name = 'Wanta';
  31. function getQueryValue(url, query_string) {
  32. let index = url.indexOf('?');
  33. if (index == -1) return null;
  34. let search = url.slice(index + 1);
  35. let queries = search.split('&');
  36. for (let i = 0; i < queries.length; i++) {
  37. let query = queries[i].split('=');
  38. if (query[0] === query_string) return query[1];
  39. }
  40. return null;
  41. }
  42. // function removeSuffix(string, suffix) {
  43. // if (string.startsWith(suffix)) return string.slice(suffix.length);
  44. // else return string;
  45. // }
  46. function purify(link) {
  47. let new_href = getQueryValue(link.href, query_name);
  48. if (urldecode) new_href = decodeURIComponent(new_href);
  49. if (new_href) {
  50. link.href = new_href;
  51. if (debug) console.log(`[${name} DEBUG] ${link.href} -> ${new_href}`);
  52. return true;
  53. }
  54. else {
  55. console.log(`[${name}] Failed to purify below link element:`);
  56. console.log(link);
  57. return false;
  58. }
  59. }
  60. function main() {
  61. let links = document.querySelector(main_path + dynamic).getElementsByTagName('a');
  62. if (debug) console.log(links);
  63. let purified = 0;
  64. for (let i = 0;i < links.length; i++) {
  65. if (links[i].href.startsWith(suffix)) {
  66. if (purify(links[i])) purified++;
  67. } else if (debug) console.log(`[${name} DEBUG] Skipped "${links[i].href}".`);
  68. }
  69. console.log(`[${name}] Purified ${purified} links out of ${links.length} links.`);
  70. }
  71. if (dynamic) {
  72. const node = document.querySelector(main_path);
  73. const config = { attributes: false, childList: true, subtree: true };
  74. const callback = function(mutations, observer) {
  75. let article = node.querySelector(dynamic.slice(3));
  76. if (article) {
  77. main();
  78. observer.disconnect();
  79. }
  80. }
  81. const observer = new MutationObserver(callback);
  82. observer.observe(node, config);
  83. }
  84. else main();
  85. })();