🏠 Home 

New Reddit for Everything but Post Pages

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.


Install this script?
  1. // ==UserScript==
  2. // @name New Reddit for Everything but Post Pages
  3. // @namespace plennhar-new-reddit-for-everything-but-post-pages
  4. // @version 3.0
  5. // @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.
  6. // @author Plennhar
  7. // @match *://*reddit.com/*
  8. // @license GPL-3.0-or-later
  9. // @grant none
  10. // ==/UserScript==
  11. // SPDX-FileCopyrightText: 2024 Plennhar
  12. // SPDX-License-Identifier: GPL-3.0-or-later
  13. (function() {
  14. 'use strict';
  15. const oldRedditPrefix = 'https://old.reddit.com';
  16. const redditPrefix = 'https://www.reddit.com';
  17. function updateLinks() {
  18. document.querySelectorAll('a').forEach(anchor => {
  19. const href = anchor.getAttribute('href');
  20. if (href) {
  21. if (window.location.href.startsWith(redditPrefix)) {
  22. // On reddit.com, change post links to old.reddit.com
  23. if (href.includes('/comments/')) {
  24. if (href.startsWith('/')) {
  25. const oldUrl = oldRedditPrefix + href;
  26. anchor.setAttribute('href', oldUrl);
  27. } else if (href.startsWith(redditPrefix)) {
  28. const oldUrl = href.replace(redditPrefix, oldRedditPrefix);
  29. anchor.setAttribute('href', oldUrl);
  30. }
  31. }
  32. } else if (window.location.href.startsWith(oldRedditPrefix)) {
  33. // On old.reddit.com, change non-post links to reddit.com
  34. if (!href.includes('/comments/')) {
  35. if (href.startsWith('/')) {
  36. const newUrl = redditPrefix + href;
  37. anchor.setAttribute('href', newUrl);
  38. } else if (href.startsWith(oldRedditPrefix)) {
  39. const newUrl = href.replace(oldRedditPrefix, redditPrefix);
  40. anchor.setAttribute('href', newUrl);
  41. }
  42. }
  43. }
  44. // Ensure default behavior for middle-clicks and user preferences
  45. anchor.addEventListener('click', (event) => {
  46. if (event.button === 1) { // Middle-click
  47. return; // Let default behavior handle middle-clicks
  48. } else if (event.button === 0) { // Left-click
  49. const openInNewTab = anchor.hasAttribute('target') && anchor.getAttribute('target') === '_blank';
  50. if (!openInNewTab) {
  51. event.preventDefault();
  52. window.location.href = anchor.getAttribute('href');
  53. }
  54. }
  55. });
  56. }
  57. });
  58. }
  59. function redirectIfNecessary() {
  60. if (window.location.href.startsWith(redditPrefix)) {
  61. const urlParts = window.location.pathname.split('/');
  62. // Redirect /comments/ pages on reddit.com to old.reddit.com
  63. if (urlParts.length > 3 && urlParts[1] === 'r' && urlParts[3] === 'comments') {
  64. const oldUrl = window.location.href.replace(redditPrefix, oldRedditPrefix);
  65. window.location.href = oldUrl;
  66. }
  67. }
  68. }
  69. // Function to observe DOM changes
  70. function observeDOMChanges() {
  71. const observer = new MutationObserver(() => {
  72. updateLinks();
  73. });
  74. observer.observe(document.body, {
  75. childList: true,
  76. subtree: true,
  77. });
  78. }
  79. // Initial link update, start observing DOM changes, and check for redirects
  80. window.addEventListener('load', () => {
  81. redirectIfNecessary();
  82. updateLinks();
  83. observeDOMChanges();
  84. });
  85. })();