Skip to content

Instantly share code, notes, and snippets.

@historical-lady
Created May 13, 2025 13:13
Show Gist options
  • Save historical-lady/6bf2574741cda2b94a2dcff04a42def5 to your computer and use it in GitHub Desktop.
Save historical-lady/6bf2574741cda2b94a2dcff04a42def5 to your computer and use it in GitHub Desktop.
TamperMonkey script to forcibly disable NSFW Reddit
// ==UserScript==
// @name Reddit NSFW Disabler
// @namespace http://tampermonkey.net/
// @version 0.1
// @description WIP NSFW disabler
// @author You
// @match https://www.reddit.com/*
// @icon https://www.redditstatic.com/shreddit/assets/favicon/192x192.png
// @grant none
// ==/UserScript==
(function() {
'use strict';
const PAGE_BLOCKED_P_STR = `<p class="text-14 font-normal w-full mb-0">
This page is only for users 18 and older, and your account is registered as underage. You're not allowed to view it.
<br><br>
If you think it's important, talk to your parent or guardian who manages your parental controls.
</p>`;
const SETTING_BLOCKED_STR = `
This setting is only for users 18 and older, and your account is registered as underage. You're not allowed to access or change it.
<br><br>
If you think it's important, talk to your parent or guardian who manages your parental controls.
`;
// https://www.w3schools.com/js/js_cookies.asp
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
setInterval(() => {
(() => {
// NSFW post detected, go to preferences if logged in or home if not
if (document.querySelectorAll('shreddit-post[nsfw]').length > 0) {
window.location.replace(getCookie('USER') === '' ? 'https://www.reddit.com/' : 'https://www.reddit.com/settings/preferences');
}
})();
(() => {
// Modify logged in NSFW community warning modal
const button = document.querySelector('#nsfw-action-button');
if (button) {
button.style.display = 'none';
}
const text = document.querySelector('guard-community-modal p')?.parentElement;
if (text) {
text.innerHTML = PAGE_BLOCKED_P_STR;
}
})();
(() => {
// Modify the logged in NSFW post warning page
const button = document.querySelector('confirm-over-18');
if (button) {
button.style.display = 'none';
const text = button.parentElement.parentElement.querySelector('div');
if (text) {
text.innerHTML = PAGE_BLOCKED_P_STR;
}
}
})();
if (window.location.pathname === '/settings/preferences') {
// Uncheck logged in NSFW setting switch
const pref = document.querySelector('settings-preferences');
if (pref) {
const checkbox = pref.shadowRoot.querySelector('faceplate-switch-input[aria-label="Show mature content (I\'m over 18)"][checked]');
if (checkbox) {
checkbox.click();
}
}
// Modify logged in NSFW setting change warning modal
const desc = document.querySelector('.settings-row-modal-card > #nsfw-desc');
if (desc) {
desc.innerHTML = SETTING_BLOCKED_STR;
const enableButton = document.querySelector('.settings-row-modal-card > button[slot="primary-button"]');
if (enableButton) enableButton.style.display = 'none';
document.querySelector('.settings-row-modal-card > button[slot="secondary-button"]')?.classList.add('button-primary');
}
}
}, 500);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment