Changes hrefs to old.reddit.com for all posts and comment pages, and redirects to them if necessary. Keeps all other pages as the default design. For this script to work it requires that you NOT opt into old.reddit.com in preferences.
- // ==UserScript==
- // @name New Reddit for Everything but Post Pages
- // @namespace plennhar-new-reddit-for-everything-but-post-pages
- // @version 3.0
- // @description Changes hrefs to old.reddit.com for all posts and comment pages, and redirects to them if necessary. Keeps all other pages as the default design. For this script to work it requires that you NOT opt into old.reddit.com in preferences.
- // @author Plennhar
- // @match *://*reddit.com/*
- // @license GPL-3.0-or-later
- // @grant none
- // ==/UserScript==
- // SPDX-FileCopyrightText: 2024 Plennhar
- // SPDX-License-Identifier: GPL-3.0-or-later
- (function() {
- 'use strict';
- const oldRedditPrefix = 'https://old.reddit.com';
- const redditPrefix = 'https://www.reddit.com';
- function updateLinks() {
- document.querySelectorAll('a').forEach(anchor => {
- const href = anchor.getAttribute('href');
- if (href) {
- if (window.location.href.startsWith(redditPrefix)) {
- // On reddit.com, change post links to old.reddit.com
- if (href.includes('/comments/')) {
- if (href.startsWith('/')) {
- const oldUrl = oldRedditPrefix + href;
- anchor.setAttribute('href', oldUrl);
- } else if (href.startsWith(redditPrefix)) {
- const oldUrl = href.replace(redditPrefix, oldRedditPrefix);
- anchor.setAttribute('href', oldUrl);
- }
- }
- } else if (window.location.href.startsWith(oldRedditPrefix)) {
- // On old.reddit.com, change non-post links to reddit.com
- if (!href.includes('/comments/')) {
- if (href.startsWith('/')) {
- const newUrl = redditPrefix + href;
- anchor.setAttribute('href', newUrl);
- } else if (href.startsWith(oldRedditPrefix)) {
- const newUrl = href.replace(oldRedditPrefix, redditPrefix);
- anchor.setAttribute('href', newUrl);
- }
- }
- }
- // Ensure default behavior for middle-clicks and user preferences
- anchor.addEventListener('click', (event) => {
- if (event.button === 1) { // Middle-click
- return; // Let default behavior handle middle-clicks
- } else if (event.button === 0) { // Left-click
- const openInNewTab = anchor.hasAttribute('target') && anchor.getAttribute('target') === '_blank';
- if (!openInNewTab) {
- event.preventDefault();
- window.location.href = anchor.getAttribute('href');
- }
- }
- });
- }
- });
- }
- function redirectIfNecessary() {
- if (window.location.href.startsWith(redditPrefix)) {
- const urlParts = window.location.pathname.split('/');
- // Redirect /comments/ pages on reddit.com to old.reddit.com
- if (urlParts.length > 3 && urlParts[1] === 'r' && urlParts[3] === 'comments') {
- const oldUrl = window.location.href.replace(redditPrefix, oldRedditPrefix);
- window.location.href = oldUrl;
- }
- }
- }
- // Function to observe DOM changes
- function observeDOMChanges() {
- const observer = new MutationObserver(() => {
- updateLinks();
- });
- observer.observe(document.body, {
- childList: true,
- subtree: true,
- });
- }
- // Initial link update, start observing DOM changes, and check for redirects
- window.addEventListener('load', () => {
- redirectIfNecessary();
- updateLinks();
- observeDOMChanges();
- });
- })();