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.

  1. // ==UserScript==
  2. // @name Disable Google Search Result URL Redirector
  3. // @namespace DisableGoogleSearchResultURLRedirector
  4. // @version 1.1.16
  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 = true; //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. } else if (ele.querySelectorAll && (m = ele.querySelectorAll('a[data-cturl]')).length) {
  46. m.forEach(m => {
  47. delete m.dataset.cturl
  48. })
  49. }
  50. return true;
  51. }
  52.  
  53. if ((/(www|groups)\.google\.[a-z]+(\.[a-z]+)?/).test(location.hostname)) {
  54. //Google site
  55. addEventListener("mousedown", (ev, a) => {
  56. //web search
  57. if (unsafeWindow.rwt && (unsafeWindow.rwt !== rwt_)) unsafeWindow.rwt = rwt_;
  58. //image search
  59. if (a = ev.target.closest('a[data-ved]')) a.setAttribute("rlhc", "1");
  60. }, true);
  61. //Google web search site
  62. function removeInterstitials() {
  63. document.querySelectorAll('#rcnt #res #search .g a[href^="/interstitial?"]').forEach(a => {
  64. a.setAttribute("href", decodeURIComponent(a.getAttribute("href").match(/\burl=([^&]+)/)[1]));
  65. a.rel = "noreferrer";
  66. });
  67. }
  68. //Google image search site
  69. let open_ = unsafeWindow.open;
  70. unsafeWindow.open = function(url, target) {
  71. if (!url) {
  72. let wnd = open_.apply(this, arguments);
  73. let wrt = wnd.document.write;
  74. wnd.document.write = function(s) {
  75. let m = s.match(/<meta http-equiv="refresh"[^>]+>/);
  76. if (m) {
  77. let e = document.createElement("DIV");
  78. e.innerHTML = m[0];
  79. e = e.firstElementChild;
  80. e.content = "0; url=" + decodeURIComponent(e.content.match(/https:\/\/www\.google\.[^/]+\/url\?.*?&url=([^&]+)/)[1]);
  81. s = s.replace(m[0], e.outerHTML);
  82. wnd.document.write = wrt;
  83. }
  84. return wrt.apply(this, arguments);
  85. };
  86. return wnd;
  87. } else {
  88. let m = url.match(/https:\/\/www\.google\.[^/]+\/url\?.*?&url=([^&]+)/);
  89. if (m) url = decodeURIComponent(m[1]);
  90. return open_.apply(this, arguments);
  91. }
  92. };
  93. //Google Groups and probably others too
  94. let desc = Object.getOwnPropertyDescriptor(HTMLAnchorElement.prototype, "href");
  95. let setHref = desc.set;
  96. desc.set = function(value) {
  97. if ((/:\/\/[^\/]+\/aclk\b|\/url\b/).test(value)) {
  98. this.rel = "noreferrer";
  99. return this.href;
  100. } else return setHref.apply(this, arguments);
  101. };
  102. Object.defineProperty(HTMLAnchorElement.prototype, "href", desc);
  103. //Any search site
  104. addEventListener("load", () => {
  105. if (disableInterstitials) removeInterstitials();
  106. });
  107. } else {
  108. //other sites
  109. let appendChild_ = Node.prototype.appendChild;
  110. Node.prototype.appendChild = function(ele) {
  111. if (checkElement(ele)) {
  112. return appendChild_.apply(this, arguments);
  113. } else return ele;
  114. };
  115. let insertBefore_ = Node.prototype.insertBefore;
  116. Node.prototype.insertBefore = function(ele) {
  117. if (checkElement(ele)) {
  118. return insertBefore_.apply(this, arguments);
  119. } else return ele;
  120. };
  121. }
  122. })();