Advertisement
turboevoluzione

reddit-compact-links.user.js

Nov 9th, 2023 (edited)
105
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.15 KB | Software | 0 0
  1. // ==UserScript==
  2. // @name         Reddit Compact links
  3. // @version      0.1
  4. // @description  Adds useful links to threads
  5. // @author       turboevoluzione
  6. // @match        *://*.reddit.com/*
  7. // @exclude      *://old.reddit.com/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. /*
  12.  * Modifies the page.
  13.  */
  14. function modifyPage() {
  15.     var loc = document.location;
  16.  
  17.     // Check the current URL and the link's domain.
  18.     if (testUrl(loc)) {
  19.         createLink('link-top', 'top', getTopUrl(loc));
  20.         createLink('link-new', 'new', getNewUrl(loc));
  21.         createLink('link-stream', 'str', getStreamUrl(loc));
  22.  
  23.         if (testDomain())
  24.             createLink('link-video', 'vid', getVideoUrl(loc));
  25.     }
  26. }
  27.  
  28. /*
  29.  * Checks whether the URL matches a thread on Reddit's compact website.
  30.  */
  31. function testUrl(location) {
  32.     var url = new URL(location);
  33.  
  34.     var isCompact = url.pathname.endsWith('/.i');
  35.     var isThread = !!(url.pathname.indexOf('/comments/') > -1
  36.         && url.pathname.indexOf('/user/') == -1
  37.         && url.pathname.indexOf('/u/') == -1);
  38.  
  39.     return isCompact && isThread;
  40. }
  41.  
  42. /*
  43.  * Checks whether the link's domain matches a Reddit video.
  44.  */
  45. function testDomain() {
  46.     var domain = document.getElementsByClassName('domain')[0];
  47.  
  48.     if (domain != undefined) {
  49.         var text = domain.childNodes[1].textContent;
  50.  
  51.         if (text == 'v.redd.it')
  52.             return true;
  53.     }
  54.  
  55.     return false;
  56. }
  57.  
  58. /*
  59.  * Gets the top URL.
  60.  */
  61. function getTopUrl(location) {
  62.     var url = new URL(location);
  63.  
  64.     // Sort the thread's comments by number of upvotes.
  65.     url.searchParams.set('sort', 'top');
  66.  
  67.     return url;
  68. }
  69.  
  70. /*
  71.  * Gets the new URL.
  72.  */
  73. function getNewUrl(location) {
  74.     var url = new URL(location);
  75.  
  76.     // Sort the thread's comments by newest.
  77.     url.searchParams.set('sort', 'new');
  78.  
  79.     return url;
  80. }
  81.  
  82.  
  83. /*
  84.  * Gets the stream URL.
  85.  */
  86. function getStreamUrl(location) {
  87.     var url = new URL(location);
  88.  
  89.     // Switch URL from compact Reddit to Reddit Stream.
  90.     url.hostname = 'reddit-stream.com';
  91.  
  92.     return url;
  93. }
  94.  
  95. /*
  96.  * Gets the video URL.
  97.  */
  98. function getVideoUrl(location) {
  99.     var url = new URL(location);
  100.  
  101.     // Switch URL from compact Reddit to old Reddit.
  102.     url.hostname = 'old.reddit.com';
  103.     url.pathname = url.pathname.replace('/.i', '/');
  104.  
  105.     return url;
  106. }
  107.  
  108. /*
  109.  * Creates a link.
  110.  */
  111. function createLink(nodeId, textContent, url) {
  112.     var tabmenu = document.getElementsByClassName('tabmenu')[0];
  113.  
  114.     if (tabmenu != undefined) {
  115.         var oldNode = document.getElementById(nodeId);
  116.         if (oldNode != undefined)
  117.             oldNode.parentNode.removeChild(oldNode);
  118.  
  119.         // Create the link.
  120.         var nodeLink = document.createElement('a');
  121.         nodeLink.setAttribute('href', url.href);
  122.         nodeLink.setAttribute('class', 'choice');
  123.         nodeLink.textContent = textContent;
  124.  
  125.         // Create the link's container.
  126.         var newNode = document.createElement('li');
  127.         newNode.setAttribute('id', nodeId);
  128.         newNode.appendChild(nodeLink);
  129.  
  130.         tabmenu.appendChild(newNode);
  131.     }
  132. }
  133.  
  134. window.onload = modifyPage;
Tags: Reddit
Advertisement
Comments
  • turboevoluzione
    309 days
    # text 0.29 KB | 0 0
    1. This userscript adds some useful links to Reddit comment threads in the compact interface:
    2. - "top" sorts the comments by Top
    3. - "new" sorts the comments by New
    4. - "str" links to reddit-stream.com for the current thread
    5. - "vid" links to old.reddit.com if the thread refers to a v.redd.it video
  • turboevoluzione
    306 days
    # text 0.09 KB | 0 0
    1. 2023-11-12
    2.  
    3. Fixed the links being erroneously shown in the comments overview of the user page
Add Comment
Please, Sign In to add comment
Advertisement