Old new reddit redirect

Gets rid of Reddit's new December 2023 layout.

As of 20.12.2023. See ბოლო ვერსია.

// ==UserScript==
// @name Old new reddit redirect
// @namespace https://reddit.com/
// @description Gets rid of Reddit's new December 2023 layout.
// @version 1.0
// @author nitro2k01
// @license MIT
// @match https://reddit.com/*
// @match https://*.reddit.com/*
// @grant none
// ==/UserScript==

(function() {
	var old_reddit_regex = /^https:\/\/old.reddit.com\//;
	var reddit_regex = /^https:\/\/(www.)?reddit.com\//;
	var current_url = document.location.href;
    console.log("Old new reddit redirect running on: " + current_url);

    function install_event_listener(body_element){
        body_element.addEventListener("mouseover",(function(e){
            //console.log(e.srcElement);
            if(e.srcElement.tagName=='A' && typeof e.srcElement.href !== 'undefined'){
                e.srcElement.href=e.srcElement.href.replace(/^https:\/\/(www.)?reddit.com\//,"http://new.reddit.com/")
            }
        }));
    }

	if(old_reddit_regex.test(current_url)){
		// If we're on old reddit, do nothing as this was likely an intentional choice by the user.
        console.log("Skipping redirect because we're on old.reddit.com");
		return;
    }

	if(/^https\:\/\/www.reddit.com\/account\/sso\/one_tap\//.test(current_url)){
		// Do nothing for an iframe that loads on the page. (Probably not necessary.)
        console.log("Skipping redirect for the one-tap iframe");
		return;
	}

    if(null != document.getElementById("login-button") ){
		// If we're not logged in, do nothing to prevent infinite redirects.
        console.log("Skipping redirect because we're not logged in.");
		return;
	}

	if(reddit_regex.test(current_url)){
		// If we're on "new new" reddit, redirect to "old new" reddit.
        var new_url = current_url.replace(reddit_regex,"http://new.reddit.com/");

        console.log("Redirecting now...");
        console.log("From: " + current_url);
        console.log("To: " + new_url);


		location.replace(new_url);
	}else{
		// If we're on "old new" reddit, set up an event listener to rewrite all links on demand.
		// We need to do it this way because a one time replace wouldn't catch links generated in notifications, which are
        console.log("Installing event listener.");


        install_event_listener(document.body)
    }
})();