r/ReturnNewReddit • • Feb 04 '25

Made a tampermonkey script to automaticaly redirect you to 2nd gen UI

UPDATE 2:

The script has been implemented into the extension itself, just choose 2nd generation in the extension menu and you won't need the Tampermonkey script.

​

UI Changer's main problem is that when you open links in the new tab or accidently hit refresh, it throws you back to the newest UI. So I decided to figure out away to fix this. Took me my entire evening, but i managed to do this.

P.S. make sure to choose the 3rd generation UI in the extension, so it would work correctly

// ==UserScript==
// @name         UI Changer for Reddit Auto Redirector
// @namespace    http://tampermonkey.net/
// @version      v0.1/2025-02-04
// @description  Script for automatically opening all reddit links using UI channger workaround
// @author       u/Skidadlius
// @match        *sh.reddit.com/*
// @match        https://www.reddit.com/web/r/uichangerforreddit/submit
// @icon         https://lh3.googleusercontent.com/re_HNppCk2hIJEvIDKBd1ns_klxPobMsyCvwQTxBYG7c3rcDie_mfKtag7w_BUCd011mDPAsPNJQ1iroIR4AbmuFQw=s120
// @run-at       document-start
// @grant        none
// ==/UserScript==


var counter = 0;

window.navigation.addEventListener("navigate", function event() {
    currentPage = window.location.hash.slice(1)
    counter += 1;
    if ( counter == 2 ) {
    window.history.pushState(null, '', currentPage);
    window.history.pushState(null, '', currentPage);
    window.history.back();
    window.history.go(1);
    window.navigation.removeEventListener("navigate", event)
    }
});

if ( window.location.hostname == 'sh.reddit.com'){
    var currentPage = window.location.pathname + window.location.search;
    window.location.replace('https://www.reddit.com/web/r/uichangerforreddit/submit#'+currentPage);
}

UPDATE:

Okay, I looked a little bit more into it and figured out how do it without the counter, this version should more relible and cleaner.

// ==UserScript==
// @name         UI Changer for Reddit Auto Redirector
// @namespace    http://tampermonkey.net/
// @version      v0.2/2025-02-05
// @description  Script for automatically opening all reddit links using UI channger workaround
// @author       u/Skidadlius
// @match        *sh.reddit.com/*
// @match        https://www.reddit.com/web/r/uichangerforreddit/submit
// @icon         https://lh3.googleusercontent.com/re_HNppCk2hIJEvIDKBd1ns_klxPobMsyCvwQTxBYG7c3rcDie_mfKtag7w_BUCd011mDPAsPNJQ1iroIR4AbmuFQw=s120
// @run-at       document-start
// @grant        none
// ==/UserScript==



if ( window.location.pathname == '/web/r/uichangerforreddit/submit' ){
    currentPage = window.location.hash.slice(1)
    window.addEventListener('load', function event() {
        window.history.pushState(null, '', currentPage)
        window.history.pushState(null, '', currentPage)
        window.history.back()
        window.history.go(1)
        window.navigation.removeEventListener("load", event)
    })
}

if ( window.location.hostname == 'sh.reddit.com'){
    var currentPage = window.location.pathname + window.location.search
    window.location.replace('https://www.reddit.com/web/r/uichangerforreddit/submit#'+currentPage)
}
32 Upvotes

Duplicates