iarp / Fuck Reddits Tracking

// ==UserScript==
// @name Fuck Reddits Tracking
// @description Removes Reddit link tracking
// @match *://*.reddit.com/*
// @run-at document-end
// @license MIT
// @version 1.0.1
// ==/UserScript==

(function() {
    "use strict";

    function copy_a_href_attrs() {
        // Store the original href which no longer contains utm_* tracking code in another attribute.
        $.each($('a'), function() {
            var attr = $(this).attr('href');
            if (typeof attr !== typeof undefined && attr !== false) {
                $(this).attr("data-original-href-url", attr);
            }
        });
    }
    copy_a_href_attrs();

    // Finally, replace the href back to the original value when clicked on
    $(document).on('click', 'a', function(e) {
        var attr = $(this).attr('data-original-href-url');
        if (typeof attr !== typeof undefined && attr !== false) {
            $(this).attr("href", attr);
        }
    });

    // If the user has RES installed, call the copy_a_href_attrs function again on the new page.
    window.addEventListener('neverEndingLoad', function() {
        copy_a_href_attrs();
    });
})();