IDontNeedRedirect

去除常见网站的对域外链接的重定向

  1. // ==UserScript==
  2. // @name IDontNeedRedirect
  3. // @namespace IDontNeedRedirect
  4. // @match https://mail.qq.com/cgi-bin/frame_html*
  5. // @match https://www.zhihu.com/*
  6. // @match https://www.jianshu.com/p/*
  7. // @version 1.0.1
  8. // @author Dreace
  9. // @license GPL-3.0
  10. // @description 去除常见网站的对域外链接的重定向
  11. // @grant unsafeWindow
  12. // ==/UserScript==
  13.  
  14. "use strict";
  15. var handlers = {};
  16.  
  17. function regExpReplace(selector, regExp, className) {
  18. document.querySelectorAll(selector).forEach(function (aTag) {
  19. try {
  20. var results = aTag.href.match(new RegExp(regExp));
  21. if (results) {
  22. aTag.href = decodeURIComponent(results[1]);
  23. if (typeof className === "string") {
  24. aTag.className = className;
  25. }
  26. }
  27. } catch (error) {
  28. log(error);
  29. }
  30. });
  31. }
  32.  
  33. handlers["https://mail.qq.com/cgi-bin/frame_html"] = function () {
  34. unsafeWindow._openExtLink = function () {
  35. return true;
  36. };
  37. };
  38.  
  39. handlers["https://www.zhihu.com"] = function () {
  40. var count = 0;
  41. var interval = setInterval(function () {
  42. regExpReplace("a", "link.zhihu.com/\\?target=(.*)",);
  43. count += 1;
  44. if (count >= 60) {
  45. clearInterval(interval);
  46. }
  47. }, 1000);
  48. };
  49.  
  50. handlers["https://www.jianshu.com/p"] = function () {
  51. regExpReplace("a", "links.jianshu.com/go\\?to=(.*)");
  52. };
  53.  
  54. for (var url in handlers) {
  55. if (location.href.match(url)) {
  56. log(url + " detected");
  57. handlers[url]();
  58. log(url + " done");
  59. }
  60. }
  61.  
  62. function log(content) {
  63. console.log("[IDontNeedRedirect] " + content);
  64. }