Disable Google Search Result URL Redirector

Disable Google URL redirector (i.e. user data tracking) on Google Search result, including Google Custom Search Engine (CSE) which is used by many websites, and in Google Groups.

目前为 2020-10-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Disable Google Search Result URL Redirector
  3. // @namespace DisableGoogleSearchResultURLRedirector
  4. // @version 1.1.12
  5. // @license GNU AGPLv3
  6. // @author jcunews
  7. // @description Disable Google URL redirector (i.e. user data tracking) on Google Search result, including Google Custom Search Engine (CSE) which is used by many websites, and in Google Groups.
  8. // @website https://greasyfork.org/en/users/85671-jcunews
  9. // @include *://*/*
  10. // @grant unsafeWindow
  11. // @run-at document-start
  12. // ==/UserScript==
  13.  
  14. (function() {
  15.  
  16. //===== CONFIGURATION BEGIN =====
  17.  
  18. var disableGoogleAdSense = true;
  19. var disableInterstitials = false; //e.g. malware warning page
  20.  
  21. //===== CONFIGURATION END =====
  22.  
  23. function rwt_() { return true }
  24.  
  25. function checkElement(ele, m, obj, fn) {
  26. if (ele.tagName === "SCRIPT") {
  27. if (disableGoogleAdSense && (/:\/\/cse\.google\.com\/adsense\/search\/(async-)?ads\.js/).test(ele.src)) {
  28. return false;
  29. } else if (m = ele.src.match(/:\/\/www.googleapis.com\/customsearch\/.*callback=([^?]+)/)) {
  30. obj = unsafeWindow;
  31. m[1].split(".").forEach(function(k, i, a) {
  32. if (i < (a.length - 1)) {
  33. obj = obj[k];
  34. } else {
  35. fn = obj[k];
  36. obj[k] = function(data) {
  37. data.results.forEach(function(res) {
  38. delete res.clicktrackUrl;
  39. });
  40. return fn.apply(this, arguments);
  41. };
  42. }
  43. });
  44. }
  45. }
  46. return true;
  47. }
  48.  
  49. if ((/(www|groups)\.google\.[a-z]+(\.[a-z]+)?/).test(location.hostname)) {
  50. //Google site
  51. addEventListener("mousedown", (ev, a) => {
  52. //web search
  53. if (unsafeWindow.rwt && (unsafeWindow.rwt !== rwt_)) unsafeWindow.rwt = rwt_;
  54. //image search
  55. if ((a = ev.target.parentNode) && (a = a.parentNode) && (a.tagName === "A") && a.attributes["data-ved"]) a.setAttribute("rlhc", "1");
  56. }, true);
  57. //Google web search site
  58. function removeInterstitials() {
  59. document.querySelectorAll('#rcnt #res #search .g a[href^="/interstitial?"]').forEach(a => {
  60. a.setAttribute("href", decodeURIComponent(a.getAttribute("href").match(/\burl=([^&]+)/)[1]));
  61. a.rel = "noreferer";
  62. });
  63. }
  64. //Google image search site
  65. let open_ = unsafeWindow.open;
  66. unsafeWindow.open = function(url, target) {
  67. if (!url) {
  68. let wnd = open_.apply(this, arguments);
  69. let wrt = wnd.document.write;
  70. wnd.document.write = function(s) {
  71. let m = s.match(/<meta http-equiv="refresh"[^>]+>/);
  72. if (m) {
  73. let e = document.createElement("DIV");
  74. e.innerHTML = m[0];
  75. e = e.firstElementChild;
  76. e.content = "0; url=" + decodeURIComponent(e.content.match(/https:\/\/www\.google\.[^/]+\/url\?.*?&url=([^&]+)/)[1]);
  77. s = s.replace(m[0], e.outerHTML);
  78. wnd.document.write = wrt;
  79. }
  80. return wrt.apply(this, arguments);
  81. };
  82. return wnd;
  83. } else {
  84. let m = url.match(/https:\/\/www\.google\.[^/]+\/url\?.*?&url=([^&]+)/);
  85. if (m) url = decodeURIComponent(m[1]);
  86. return open_.apply(this, arguments);
  87. }
  88. };
  89. //Google Groups and probably others too
  90. let desc = Object.getOwnPropertyDescriptor(HTMLAnchorElement.prototype, "href");
  91. let setHref = desc.set;
  92. desc.set = function(value) {
  93. if ((/\/url\b/).test(value)) {
  94. this.rel = "noreferer";
  95. return this.href;
  96. } else return setHref.apply(this, arguments);
  97. };
  98. Object.defineProperty(HTMLAnchorElement.prototype, "href", desc);
  99. //Any search site
  100. addEventListener("load", () => {
  101. if (disableInterstitials) removeInterstitials();
  102. });
  103. } else {
  104. //other sites
  105. let appendChild_ = Node.prototype.appendChild;
  106. Node.prototype.appendChild = function(ele) {
  107. if (checkElement(ele)) {
  108. return appendChild_.apply(this, arguments);
  109. } else return ele;
  110. };
  111. let insertBefore_ = Node.prototype.insertBefore;
  112. Node.prototype.insertBefore = function(ele) {
  113. if (checkElement(ele)) {
  114. return insertBefore_.apply(this, arguments);
  115. } else return ele;
  116. };
  117. }
  118.  
  119. })();