Greasy Fork is available in English.

Reddit redirect external links to comments page

Replace all external post links with the comment link. Every external Link will redirect to the comments page instead of the external url (currently works on home, subreddits and user profiles). Also adds an "(Open external Link)" link after the post title, if you still want to visit the external url.

Version vom 10.08.2017. Aktuellste Version

// ==UserScript==
// @name         Reddit redirect external links to comments page
// @namespace    
// @version      0.7
// @description  Replace all external post links with the comment link. Every external Link will redirect to the comments page instead of the external url (currently works on home, subreddits and user profiles). Also adds an "(Open external Link)" link after the post title, if you still want to visit the external url.
// @author       TheExoduser
// @match        *://*.reddit.com/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

// Adds functions to load jQuery via UserScript
var load, execute, loadAndExecute; load = function (a, b, c) { var d; d = document.createElement("script"), d.setAttribute("src", a), b != null && d.addEventListener("load", b), c != null && d.addEventListener("error", c), document.body.appendChild(d); return d }, execute = function (a) { var b, c; typeof a == "function" ? b = "(" + a + ")();" : b = a, c = document.createElement("script"), c.textContent = b, document.body.appendChild(c); return c }, loadAndExecute = function (a, b) { return load(a, function () { return execute(b) }) };

// Link Parameter Parse function
function getAllUrlParams(e){var t=e?e.split("?")[1]:window.location.search.slice(1),i={};if(t){t=t.split("#")[0];for(var r=t.split("&"),n=0;n<r.length;n++){var o=r[n].split("="),l=void 0,s=o[0].replace(/\[\d*\]/,function(e){return l=e.slice(1,-1),""}),a="undefined"==typeof o[1]?!0:o[1];s=s.toLowerCase(),a=a.toLowerCase(),i[s]?("string"==typeof i[s]&&(i[s]=[i[s]]),"undefined"==typeof l?i[s].push(a):i[s][l]=a):i[s]=a}}return i}

// Function to parse and replace all post links with the corresponding comment links
function parseAndReplaceLinks( parent ) {
    var posts = null;
    var itemLink = null;
    var commentLink = null;
    
    if(window.location.href.match('(http|https):\/\/(?:www.)reddit.com\/.*\/comments\/.*')) {
        // Do nothing if on comments page
        return;
    } else if(window.location.href.match('(http|https):\/\/(?:www.)reddit.com\/(u|user)\/.*')) {
        posts = $(parent + ' > .thing');
        
        $(posts).each(function (index) {
            if (($(this).find('p.parent').html()).length > 0) {
                itemLink = $(this).find('.parent a.title').attr('href');
                commentLink = $(this).find('.entry li.first:nth-child(2) > a').attr('href');
            
                if (itemLink.startsWith('https://') || itemLink.startsWith('http://')) {
                    if (!itemLink.match('(http|https):\/\/(?:www.)reddit.com\/r\/.*')) {
                        $(this).find('a.title').append(' <span class="domain">(<a href="' + $(this).find('a.title').attr('href') + '" target="_blank">Open external Link</a>)</span>');
                        $(this).find('a.title').attr('href', commentLink);
                    }
                } else { 
                    // Internal Reddit link or already replaced -> no replace needed
                    return;
                }
            } else {
                itemLink = $(this).find('.entry a.title').attr('href');
                commentLink = $(this).find('.entry li.first > a').attr('href');
            
                if (itemLink.startsWith('https://') || itemLink.startsWith('http://')) {
                    if (!itemLink.match('(http|https):\/\/(?:www.)reddit.com\/r\/.*')) {
                        $(this).find('a.title').append(' <span class="domain">(<a href="' + $(this).find('a.title').attr('href') + '" target="_blank">Open external Link</a>)</span>');
                        $(this).find('a.title').attr('href', commentLink);
                        $(this).find('a.title').attr('data-href-url', commentLink);
                        $(this).find('a.title').removeAttr('data-outbound-url');
                        $(this).find('a.title').removeAttr('data-outbound-expiration');
                    }
                } else { 
                    // Internal Reddit link or already replaced -> no replace needed
                    return;
                }
            }
        });
    } else if (window.location.href.match('(http|https):\/\/(?:www.)reddit.com\/.*') || window.location.href.match('(http|https):\/\/(?:www.)reddit.com\/r\/*')) {
        posts = $(parent + ' div.entry');
            
        $(posts).each(function (index) {
            itemLink = $(this).find('a.title').attr('href');
            commentLink = $(this).find('li.first > a').attr('href');

            if (itemLink.startsWith('https://') || itemLink.startsWith('http://')) {
                if (!itemLink.match('(http|https):\/\/(?:www.)reddit.com\/r\/.*')) {
                    $(this).find('a.title').append(' <span class="domain">(<a href="' + $(this).find('a.title').attr('href') + '" target="_blank">Open external Link</a>)</span>');
                    $(this).find('a.title').attr('href', commentLink);
                    $(this).find('a.title').attr('data-href-url', commentLink);
                    $(this).find('a.title').removeAttr('data-outbound-url');
                    $(this).find('a.title').removeAttr('data-outbound-expiration');
                }
            } else { 
                // Internal Reddit link or already replaced -> no replace needed
                return;
            }
        });
    } 
}


// On page load check if jQuery is available
if (window.jQuery) {
    // jQuery is loaded  
} else {
    // jQuery is not loaded -> load it
    load("//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js");
}


// First call of main function to replace all post links on initial page load
parseAndReplaceLinks('#siteTable');


/////////////////////////////////////////////////////////////////////////////////////////
// Define mutation observer to detect changes by extensions like RES (infinite scroll)
var observer = new MutationObserver(function(mutations, observer) {
    parseAndReplaceLinks('.content #siteTable > #siteTable');
});

// Activate observer
observer.observe(document.querySelector('.content #siteTable'), {
    childList: true, 
    attributes: true
});
/////////////////////////////////////////////////////////////////////////////////////////