Rewrite Reddit links to use "Old Reddit"

Rewrites all links to Reddit to "old.reddit.com" to automatically use the original UI, not the new one

  1. // ==UserScript==
  2. // @name Rewrite Reddit links to use "Old Reddit"
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Rewrites all links to Reddit to "old.reddit.com" to automatically use the original UI, not the new one
  6. // @author https://gf.qytechs.cn/en/users/728793-keyboard-shortcuts
  7. // @match https://*/*
  8. // @match http://*/*
  9. // @icon https://www.reddit.com/favicon.ico
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. /* jshint esversion: 6 */
  15.  
  16. (function() {
  17. 'use strict';
  18. /**
  19. * Description
  20. *
  21. * This script looks for links to Reddit on any page and rewrites them to use old.reddit.com which forces
  22. * the use of the "old" (or original) site theme, which many people prefer to the current theme.
  23. *
  24. */
  25.  
  26. /**
  27. * These links break when "old.reddit" is used with them, so we don't replace them.
  28. */
  29. function excludedUrl(url) {
  30. return url.indexOf('reddit.com/poll/') !== -1 || // do not include polls
  31. url.indexOf('reddit.com/gallery/') !== -1 || // or galleries
  32. url.indexOf('reddit.com/sw.') !== -1; // or service workers
  33. }
  34.  
  35. /**
  36. * Each object in the `transforms` array below can have the following fields:
  37. * 1. `regex` matches link targets and selects them for re-writing
  38. * 2. `match` defines a lambda that takes a regex match object and returns whether this is a valid candidate for re-writing
  39. * 3. `replace` defines a lambda that takes a regex match object and returns the new target for the link
  40. */
  41. const transforms = [
  42. {regex: /^(https?:\/\/)((www|new)\.)?(reddit\.com\/.*)/,
  43. match: m => m[3] !== 'old' && !excludedUrl(m[4]), // not already containing `.old` and not a poll or gallery
  44. replace: m => 'https://old.' + m[4]},
  45. {regex: /^(https?:\/\/)redd\.it\/(.*)/,
  46. match: m => true, // always match
  47. replace: m => 'https://old.reddit.com/' + m[2]}
  48. ];
  49.  
  50. function cleanupLinks() {
  51. const anchors = document.getElementsByTagName('a');
  52. for (var i = 0; i < anchors.length; i++) {
  53. for (const tr of transforms) {
  54. const target = anchors[i].href || '';
  55. const match = tr.regex.exec(target);
  56. if (match && tr.match(match)) { // link matches
  57. const newTarget = tr.replace(match);
  58. anchors[i].href = newTarget;
  59. }
  60. }
  61. }
  62. }
  63. setInterval(cleanupLinks, 1000); // call again to deal with links that were added dynamically
  64. cleanupLinks(); // call once first
  65. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址