Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Reddit Compact redirect
- // @version 0.2
- // @description Converts reddit.com/* links to reddit.com/*/.i links
- // @author turboevoluzione
- // @match *://*.reddit.com/*
- // @exclude *://old.reddit.com/*
- // @grant none
- // @run-at document-start
- // ==/UserScript==
- /*
- * Checks whether the URL must be changed.
- */
- function testUrl(location) {
- var url = new URL(location);
- if (url.hostname.endsWith('reddit.com')) {
- var isCompact = url.pathname.endsWith('/.i');
- var isOld = url.hostname == 'old.reddit.com';
- var isSpecial = url.pathname.indexOf('/gallery/') > -1
- || url.pathname.indexOf('/svc/') > -1
- || url.pathname.indexOf('/u/') > -1
- || url.pathname.indexOf('/s/') > -1
- || url.pathname.indexOf('/gallery/') > -1
- || url.pathname.endsWith('/media')
- || url.pathname.endsWith('/logout');
- return !!!(isCompact
- || isOld
- || isSpecial);
- }
- return false;
- }
- /*
- * Modifies the URL accordingly.
- */
- function getCompactUrl(location) {
- var url = new URL(location);
- if (url.pathname.endsWith('/.i'))
- return url.href;
- if (url.pathname.endsWith('.compact'))
- url.pathname = url.pathname.replace('.compact', '/.i');
- else
- url.pathname = url.pathname.concat('/.i');
- url.pathname = url.pathname.replace('//.i', '/.i');
- return url;
- }
- /*
- * Fixes the links in the page.
- */
- function fixRedditLinks() {
- // Obtain the links.
- var links = Array.prototype.slice.call(document.links, 0);
- // Filter the links by their URL.
- links.filter(function (link) {
- if (testUrl(link)) {
- var compactUrl = getCompactUrl(link);
- if (link.hasAttribute('data-outbound-url'))
- link.setAttribute('data-outbound-url', compactUrl.href);
- link.setAttribute('href', compactUrl.href);
- }
- });
- }
- // Check the current URL.
- if (testUrl(document.location)) {
- // Redirect to the new URL.
- document.location.replace(getCompactUrl(window.location));
- }
- window.onload = fixRedditLinks;
- setInterval(fixRedditLinks, 50);
Advertisement
Comments
-
- 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.
- 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.
- Known issues:
- - Logout doesn't work from the compact interface
Add Comment
Please, Sign In to add comment