newnewredditsucks/newnewredditsucks.user.js

45 lines
No EOL
1.5 KiB
JavaScript

// ==UserScript==
// @name Use old new Reddit
// @namespace https://codeberg.org/okseby/newnewredditsucks
// @version 1.13
// @description forward the ugly new reddit to the older new reddit
// @author okseby
// @match https://www.reddit.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @grant none
// @run-at document-start
// @updateURL https://codeberg.org/okseby/newnewredditsucks/raw/branch/main/newnewredditsucks.user.js
// @downloadURL https://codeberg.org/okseby/newnewredditsucks/raw/branch/main/newnewredditsucks.user.js
// ==/UserScript==
(function () {
'use strict';
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);
}
}
// Run on initial load
redirectIfNecessary(window.location.href);
// Observe changes in the document to handle dynamic navigation
const observer = new MutationObserver(() => {
redirectIfNecessary(window.location.href);
});
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);
}
});
})();