Skip to content

Instantly share code, notes, and snippets.

@ignamiranda
Last active August 2, 2023 11:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ignamiranda/e8d754d337631c508e412765019a09cc to your computer and use it in GitHub Desktop.
Save ignamiranda/e8d754d337631c508e412765019a09cc to your computer and use it in GitHub Desktop.
Redirects to Wayback Machine archive of private Reddit posts and subreddits with user confirmation
// ==UserScript==
// @name Reddit Private Post Redirector
// @namespace ignamiranda
// @version 1.1
// @description Redirects the "Browse Reddit" button to the Wayback Machine archive of a private Reddit post or subreddit
// @author Ignacio Miranda
// @match https://www.reddit.com/*
// @grant GM_addStyle
// ==/UserScript==
(function () {
'use strict';
// Function to redirect to the latest Wayback Machine archive of the given URL
function redirectToWaybackMachineArchive(url) {
var waybackUrl = 'https://web.archive.org/web/2/' + url;
console.log('Redirecting to Wayback Machine archive:', waybackUrl);
window.location.href = waybackUrl;
}
// Function to check if the current page is a private subreddit or post
function isPrivatePage() {
var privateElement = document.getElementById('privateSubredditDetails');
return privateElement !== null;
}
console.log('Running Reddit Private Post Redirector script.');
// Check if the current page is a private subreddit or post
if (isPrivatePage()) {
console.log('Detected private subreddit or post.');
// Modify the "Browse Reddit" button to redirect to Wayback Machine
var browseRedditButtons = document.querySelectorAll('a[href="/"]');
browseRedditButtons.forEach(function (button) {
if (button.innerText === 'Browse Reddit') {
var postUrl = window.location.href;
console.log('Post URL:', postUrl);
button.innerText = 'Archive.org';
button.href = '#'; // Remove the original href
button.addEventListener('click', function (event) {
event.preventDefault(); // Prevent default click behavior
redirectToWaybackMachineArchive(postUrl);
});
console.log('Modified "Browse Reddit" button to redirect to Wayback Machine.');
}
});
// Apply CSS styles to enforce single line text
GM_addStyle(`
a[href="/"] {
white-space: nowrap;
}
`);
} else {
console.log('Not a private subreddit or post.');
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment