From f7b29d3b8238d9ff3cb6fa451f29d24a4f64097a Mon Sep 17 00:00:00 2001 From: Sebastian Cabrera Date: Tue, 9 Jul 2024 06:38:43 -0400 Subject: [PATCH] 1.9 - hopefully this is the last one --- newnewredditsucks.user.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/newnewredditsucks.user.js b/newnewredditsucks.user.js index 2619ed1..0f95621 100644 --- a/newnewredditsucks.user.js +++ b/newnewredditsucks.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Use old new Reddit // @namespace https://codeberg.org/okseby/newnewredditsucks -// @version 1.8 +// @version 1.9 // @description forward the ugly new reddit to the older new reddit // @author okseby // @match https://www.reddit.com/* @@ -15,16 +15,31 @@ (function () { 'use strict'; - // Get the current URL - const currentUrl = window.location.href; + function redirectIfNecessary(url) { + const mediaPattern = /\/media($|\?)/; + if (mediaPattern.test(url)) { + return; // Abort the redirect + } + const newUrl = url.replace('www.reddit.com', 'new.reddit.com'); + if (newUrl !== url) { + window.location.replace(newUrl); + } + } - // Check if it's a media link - const mediaPattern = /\/media($|\?)/; - if (mediaPattern.test(currentUrl)) return; // Abort the redirect + // Run on initial load + redirectIfNecessary(window.location.href); - // Replace www.reddit.com with new.reddit.com - const newUrl = currentUrl.replace('www.reddit.com', 'new.reddit.com'); + // Observe changes in the document to handle dynamic navigation + const observer = new MutationObserver(() => { + redirectIfNecessary(window.location.href); + }); - // Redirect to the new URL - window.location.replace(newUrl); + observer.observe(document.body, { childList: true, subtree: true }); + + // Handle click events on links + document.addEventListener('click', (event) => { + if (event.target.tagName === 'A' && event.target.href) { + redirectIfNecessary(event.target.href); + } + }); })(); \ No newline at end of file