Reddit Remove Spy Linking

Remove Spy Linking on Reddit.com

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Reddit Remove Spy Linking
// @namespace   http://reddit.co.uk
// @description Remove Spy Linking on Reddit.com
// @include     https://*.reddit.com/*
// @include     https://reddit.com/*
// @include     http://reddit.com/*
// @include     http://*.reddit.com/*
// @version     1.0.1
// @grant       none
// ==/UserScript==

var a = document.getElementsByTagName("a");
for (var x=1; x<a.length; x++) {
  a[x].removeAttribute("data-outbound-url");
  a[x].removeAttribute("data-href-url");
  a[x].removeAttribute("data-outbound-expiration");
  a[x].removeAttribute("data-event-action");
  a[x].removeAttribute("rel");
  if (a[x].className.indexOf("comments")!=-1) {
    var nid = a[x].parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute("data-fullname").split("_")[1];
    var sbr = a[x].parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute("data-subreddit");
    var tid = a[x].parentNode.parentNode.parentNode.getElementsByTagName("p")[0].getElementsByTagName("a")[0].textContent.toLowerCase();
    tid = tid.replace(/\s+/g, "_");
    tid = tid.replace(/[^a-zA-z0-9]/g, "");
    tid = tid.replace(/__/g, "_");
    if (tid.length > 50) {
      tid=tid.substr(0,50);
      while (tid.length>=1 && tid.charAt(tid.length-1)!="_") {
        tid = tid.substr(0, tid.length-1)
      }
     tid = tid.substr(0, tid.length-1)
    }
    newstr = "https://www.reddit.com/" + "r/"+ sbr + "/comments/" + nid + "/" + tid;
    a[x].setAttribute("href", newstr);
  }
  a[x].removeAttribute("data-inbound-url");
}