turboevoluzione

reddit-compact-redirect.user.js

Nov 9th, 2023 (edited)
515
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.25 KB | Software | 0 0
  1. // ==UserScript==
  2. // @name         Reddit Compact redirect
  3. // @version      0.2
  4. // @description  Converts reddit.com/* links to reddit.com/*/.i links
  5. // @author       turboevoluzione
  6. // @match        *://*.reddit.com/*
  7. // @exclude      *://old.reddit.com/*
  8. // @grant        none
  9. // @run-at       document-start
  10. // ==/UserScript==
  11.  
  12. /*
  13.  * Checks whether the URL must be changed.
  14.  */
  15. function testUrl(location) {
  16.     var url = new URL(location);
  17.  
  18.     if (url.hostname.endsWith('reddit.com')) {
  19.         var isCompact = url.pathname.endsWith('/.i');
  20.         var isOld = url.hostname == 'old.reddit.com';
  21.         var isSpecial = url.pathname.indexOf('/gallery/') > -1
  22.             || url.pathname.indexOf('/svc/') > -1
  23.             || url.pathname.indexOf('/u/') > -1
  24.             || url.pathname.indexOf('/s/') > -1
  25.             || url.pathname.indexOf('/gallery/') > -1
  26.             || url.pathname.endsWith('/media')
  27.             || url.pathname.endsWith('/logout');
  28.  
  29.         return !!!(isCompact
  30.             || isOld
  31.             || isSpecial);
  32.     }
  33.  
  34.     return false;
  35. }
  36.  
  37. /*
  38.  * Modifies the URL accordingly.
  39.  */
  40. function getCompactUrl(location) {
  41.     var url = new URL(location);
  42.  
  43.     if (url.pathname.endsWith('/.i'))
  44.         return url.href;
  45.  
  46.     if (url.pathname.endsWith('.compact'))
  47.         url.pathname = url.pathname.replace('.compact', '/.i');
  48.     else
  49.         url.pathname = url.pathname.concat('/.i');
  50.  
  51.     url.pathname = url.pathname.replace('//.i', '/.i');
  52.  
  53.     return url;
  54. }
  55.  
  56. /*
  57.  * Fixes the links in the page.
  58.  */
  59. function fixRedditLinks() {
  60.     // Obtain the links.
  61.     var links = Array.prototype.slice.call(document.links, 0);
  62.  
  63.     // Filter the links by their URL.
  64.     links.filter(function (link) {
  65.         if (testUrl(link)) {
  66.             var compactUrl = getCompactUrl(link);
  67.  
  68.             if (link.hasAttribute('data-outbound-url'))
  69.                 link.setAttribute('data-outbound-url', compactUrl.href);
  70.             link.setAttribute('href', compactUrl.href);
  71.         }
  72.     });
  73. }
  74.  
  75. // Check the current URL.
  76. if (testUrl(document.location)) {
  77.     // Redirect to the new URL.
  78.     document.location.replace(getCompactUrl(window.location));
  79. }
  80.  
  81. window.onload = fixRedditLinks;
  82. setInterval(fixRedditLinks, 50);
Tags: Reddit
Advertisement
Comments
  • turboevoluzione
    3 years (edited)
    # text 0.48 KB | 0 0
    1. This userscript redirects any Reddit link to the compact interface by adding ".i" at the end of the path. Additionally, any Reddit link in the page will be converted as well to reduce the number of redirects.
    2.  
    3. Some special pages (galleries, native images etc.) are not redirected because they aren't supported in the compact interface, also old.reddit.com links are excluded in order to provide an alternative interface.
    4.  
    5. Known issues:
    6. - Logout doesn't work from the compact interface
Add Comment
Please, Sign In to add comment