Redirect Reddit URLs

Redirect any Reddit domain that isn't "old" to old.reddit.com while keeping the rest of the URL. Remove string after the last / in old.reddit.com URLs. Integrate shouldExcludePath function.

  1. // ==UserScript==
  2. // @name Redirect Reddit URLs
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Redirect any Reddit domain that isn't "old" to old.reddit.com while keeping the rest of the URL. Remove string after the last / in old.reddit.com URLs. Integrate shouldExcludePath function.
  6. // @author You
  7. // @match *://*.reddit.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. function removeDotsFromPath() {
  15. const host = window.location.host;
  16. const path = window.location.pathname;
  17. if (host === "old.reddit.com") {
  18. if (path.includes("/domain/")) {
  19. const newPath = path.replace(/\.[a-z]+$/, '');
  20. const newUrl = `https://${host}${newPath}${window.location.search}${window.location.hash}`;
  21. if (newUrl !== window.location.href) {
  22. window.location.replace(newUrl);
  23. }
  24. } else if (path.includes(".")) {
  25. const newPath = path.replace(/\.[a-z]+$/, '');
  26. const newUrl = `https://${host}${newPath}${window.location.search}${window.location.hash}`;
  27. if (newUrl !== window.location.href) {
  28. window.location.replace(newUrl);
  29. }
  30. }
  31. }
  32. }
  33.  
  34. function shouldExcludePath(path) {
  35. // Define paths to exclude
  36. const excludePaths = ['/poll/', '/gallery/', '/media'];
  37.  
  38. // Check if the path contains any excluded substring and is not preceded by "/r"
  39. return excludePaths.some(exclude => path.includes(exclude) && !path.includes('/r' + exclude));
  40. }
  41.  
  42. function redirectNonOldDomains() {
  43. const host = window.location.host;
  44. const path = window.location.pathname;
  45. const nonOldDomainRegex = /^(?!old\.)/;
  46. if (nonOldDomainRegex.test(host) && !shouldExcludePath(path)) {
  47. window.location.href = 'https://old.reddit.com' + window.location.pathname + window.location.search + window.location.hash;
  48. }
  49. }
  50.  
  51. function redirectToOldRedditIfNotOnOldOrRedditOrTwoLetterDomain() {
  52. const host = window.location.host;
  53. const path = window.location.pathname;
  54.  
  55. if (
  56. host === "www.reddit.com" &&
  57. !shouldExcludePath(path)
  58. ) {
  59. redirectToOldReddit();
  60. } else if (host === "old.reddit.com" && shouldExcludePath(path)) {
  61. const newUrl = `https://www.reddit.com${path}${window.location.search}${window.location.hash}`;
  62. window.location.replace(newUrl);
  63. }
  64. }
  65.  
  66. function redirectToOldReddit() {
  67. window.location.href = 'https://old.reddit.com' + window.location.pathname + window.location.search + window.location.hash;
  68. }
  69.  
  70. // Call the functions
  71. removeDotsFromPath();
  72. redirectNonOldDomains();
  73. redirectToOldRedditIfNotOnOldOrRedditOrTwoLetterDomain();
  74. })();