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.

Verzia zo dňa 07.01.2020. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name        Disable Google Search Result URL Redirector
// @namespace   DisableGoogleSearchResultURLRedirector
// @version     1.1.10
// @license     GNU AGPLv3
// @author      jcunews
// @website     https://greasyfork.org/en/users/85671-jcunews
// @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.
// @include     *://*/*
// @grant       unsafeWindow
// @run-at      document-start
// ==/UserScript==

(function(createElement_, appendChild_, insertBefore_) {

  //===== CONFIGURATION BEGIN =====

  var disableGoogleAdSense = true;
  var disableInterstitials = false; //e.g. malware warning page

  //===== CONFIGURATION END =====

  function rwt_() { return true }

  function checkElement(ele, m, obj, fn) {
    if (ele.tagName === "SCRIPT") {
      if (disableGoogleAdSense && (/:\/\/cse\.google\.com\/adsense\/search\/(async-)?ads\.js/).test(ele.src)) {
        return false;
      } else if (m = ele.src.match(/:\/\/www.googleapis.com\/customsearch\/.*callback=([^?]+)/)) {
        obj = unsafeWindow;
        m[1].split(".").forEach(function(k, i, a) {
          if (i < (a.length - 1)) {
            obj = obj[k];
          } else {
            fn = obj[k];
            obj[k] = function(data) {
              data.results.forEach(function(res) {
                delete res.clicktrackUrl;
              });
              return fn.apply(this, arguments);
            };
          }
        });
      }
    }
    return true;
  }

  if ((/(www|groups)\.google\.[a-z]+(\.[a-z]+)?/).test(location.hostname)) {
    //Google site
    addEventListener("mousedown", function() {
      if (unsafeWindow.rwt && (unsafeWindow.rwt !== rwt_)) unsafeWindow.rwt = rwt_;
    }, true);
    //Google web search site
    function removeInterstitials() {
      document.querySelectorAll('#rcnt #res #search .g a[href^="/interstitial?"]').forEach(a => {
        a.setAttribute("href", decodeURIComponent(a.getAttribute("href").match(/\burl=([^&]+)/)[1]));
        a.rel = "noreferer";
      });
    }
    //Google image search site
    let setAttribute = HTMLAnchorElement.prototype.setAttribute;
    HTMLAnchorElement.prototype.setAttribute = function(name, value) {
      if ((name === "href") && !this.dataset_) {
        let a = this, href = value;
        this.dataset_ = this.dataset;
        Object.defineProperty(this, "dataset", {
          value: new Proxy(this.dataset_, {
            set: function(tgt, prop, val, recv) {
              if ((prop === "cthref") && (/^\/url\?/).test(val)) {
                a.href = href;
                a.rel = "noreferer";
              } else a.dataset_[prop] = val;
              return true;
            }
          })
        });
      }
      return setAttribute.apply(this, arguments);
    };
    //Google Groups and probably others too
    let desc = Object.getOwnPropertyDescriptor(HTMLAnchorElement.prototype, "href");
    let setHref = desc.set;
    desc.set = function(value) {
      if ((/\/url.*[?&]q=(ht|f)tp/).test(value)) {
        this.rel = "noreferer";
        return this.href;
      } else return setHref.apply(this, arguments);
    };
    Object.defineProperty(HTMLAnchorElement.prototype, "href", desc);
    //Any search site
    addEventListener("load", () => {
      if (disableInterstitials) removeInterstitials();
    });
  } else {
    //other sites
    appendChild_ = Node.prototype.appendChild;
    Node.prototype.appendChild = function(ele) {
      if (checkElement(ele)) {
        return appendChild_.apply(this, arguments);
      } else return ele;
    };
    insertBefore_ = Node.prototype.insertBefore;
    Node.prototype.insertBefore = function(ele) {
      if (checkElement(ele)) {
        return insertBefore_.apply(this, arguments);
      } else return ele;
    };
  }

})();