Redirect to compact reddit

Redirects from regular reddit to the compact version

  1. // ==UserScript==
  2. // @name Redirect to compact reddit
  3. // @description Redirects from regular reddit to the compact version
  4. // @version 0.5
  5. // @match https://*.reddit.com/*
  6. // @run-at document-start
  7. // @exclude https://www.reddit.com/gallery/*
  8. // @namespace https://userscripts.56k-modem.online
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. //Thanks to U/Jellysandwich for line 34 to line 50
  13. //Only works with a secure (HTTPS) connection to Reddit. Download
  14. //HTTPS Everywhere at https://www.eff.org/https-everywhere
  15. //Restored from http://web.archive.org/web/*/https://userscripts.org/scripts/show/133891
  16.  
  17. var oldUrlPath = window.location.pathname;
  18.  
  19. /*--- Test that ".compact" is at end of URL, excepting any "hashes"
  20. or searches.
  21. */
  22. if ( ! /\.i$/.test (oldUrlPath) ) {
  23.  
  24. var newURL = window.location.protocol + "//"
  25. + window.location.hostname
  26. + oldUrlPath + ".i"
  27. + window.location.search
  28. + window.location.hash
  29. ;
  30. /*-- replace() puts the good page in the history instead of the
  31. bad page.
  32. */
  33. window.location.replace (newURL);
  34. }
  35. // ------------------------------------------------
  36. // Change links that end in .compact to .i
  37. // this prevents the redirect nonsense .compact -> desktop site -> .i
  38. // so navigation is slightly faster overall
  39. // ------------------------------------------------
  40.  
  41. document.addEventListener ("DOMContentLoaded", function(){
  42.  
  43. // check all links on page
  44. // https://stackoverflow.com/questions/3871358/get-all-the-href-attributes-of-a-web-site/3871370#3871370
  45. var links = document.links;
  46. for(var i=0; i<links.length; i++) {
  47. if (links[i].href.endsWith('/.compact')) {
  48. links[i].href = links[i].href.replace('/.compact', '/.i');
  49. }
  50. }
  51. });