Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Reddit Compact links
- // @version 0.1
- // @description Adds useful links to threads
- // @author turboevoluzione
- // @match *://*.reddit.com/*
- // @exclude *://old.reddit.com/*
- // @grant none
- // ==/UserScript==
- /*
- * Modifies the page.
- */
- function modifyPage() {
- var loc = document.location;
- // Check the current URL and the link's domain.
- if (testUrl(loc)) {
- createLink('link-top', 'top', getTopUrl(loc));
- createLink('link-new', 'new', getNewUrl(loc));
- createLink('link-stream', 'str', getStreamUrl(loc));
- if (testDomain())
- createLink('link-video', 'vid', getVideoUrl(loc));
- }
- }
- /*
- * Checks whether the URL matches a thread on Reddit's compact website.
- */
- function testUrl(location) {
- var url = new URL(location);
- var isCompact = url.pathname.endsWith('/.i');
- var isThread = !!(url.pathname.indexOf('/comments/') > -1
- && url.pathname.indexOf('/user/') == -1
- && url.pathname.indexOf('/u/') == -1);
- return isCompact && isThread;
- }
- /*
- * Checks whether the link's domain matches a Reddit video.
- */
- function testDomain() {
- var domain = document.getElementsByClassName('domain')[0];
- if (domain != undefined) {
- var text = domain.childNodes[1].textContent;
- if (text == 'v.redd.it')
- return true;
- }
- return false;
- }
- /*
- * Gets the top URL.
- */
- function getTopUrl(location) {
- var url = new URL(location);
- // Sort the thread's comments by number of upvotes.
- url.searchParams.set('sort', 'top');
- return url;
- }
- /*
- * Gets the new URL.
- */
- function getNewUrl(location) {
- var url = new URL(location);
- // Sort the thread's comments by newest.
- url.searchParams.set('sort', 'new');
- return url;
- }
- /*
- * Gets the stream URL.
- */
- function getStreamUrl(location) {
- var url = new URL(location);
- // Switch URL from compact Reddit to Reddit Stream.
- url.hostname = 'reddit-stream.com';
- return url;
- }
- /*
- * Gets the video URL.
- */
- function getVideoUrl(location) {
- var url = new URL(location);
- // Switch URL from compact Reddit to old Reddit.
- url.hostname = 'old.reddit.com';
- url.pathname = url.pathname.replace('/.i', '/');
- return url;
- }
- /*
- * Creates a link.
- */
- function createLink(nodeId, textContent, url) {
- var tabmenu = document.getElementsByClassName('tabmenu')[0];
- if (tabmenu != undefined) {
- var oldNode = document.getElementById(nodeId);
- if (oldNode != undefined)
- oldNode.parentNode.removeChild(oldNode);
- // Create the link.
- var nodeLink = document.createElement('a');
- nodeLink.setAttribute('href', url.href);
- nodeLink.setAttribute('class', 'choice');
- nodeLink.textContent = textContent;
- // Create the link's container.
- var newNode = document.createElement('li');
- newNode.setAttribute('id', nodeId);
- newNode.appendChild(nodeLink);
- tabmenu.appendChild(newNode);
- }
- }
- window.onload = modifyPage;
Advertisement
Comments
-
- This userscript adds some useful links to Reddit comment threads in the compact interface:
- - "top" sorts the comments by Top
- - "new" sorts the comments by New
- - "str" links to reddit-stream.com for the current thread
- - "vid" links to old.reddit.com if the thread refers to a v.redd.it video
-
- 2023-11-12
- Fixed the links being erroneously shown in the comments overview of the user page
Add Comment
Please, Sign In to add comment
Advertisement