Reddit: Hide/Show NSFW posts

Adds dropdown list, allowing to select prefferable way of watching Reddit: Show All Posts, Hide NSFW Posts or Show Only NSFW Posts.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        Reddit: Hide/Show NSFW posts
// @description Adds dropdown list, allowing to select prefferable way of watching Reddit: Show All Posts, Hide NSFW Posts or Show Only NSFW Posts.
// @include     http://*.reddit.com/*
// @include     https://*.reddit.com/*
// @version     1.4
// @icon        https://www.redditstatic.com/desktop2x/img/favicon/apple-icon-72x72.png
// @require	    https://code.jquery.com/jquery-3.7.1.min.js
// @run-at      document-end
// @author      Delmor_S
// @grant       none
// @namespace   https://greasyfork.org/users/4638
// ==/UserScript==

/* global $, jQuery */
/* eslint-env browser */

function showAllPosts() {
    $('article.w-full.m-0').each(function() {
        $(this).css('display', 'block');
    });
}

function hideNSFWPosts() {
    $('article.w-full.m-0').each(function() {
        const nsfwPost = $(this).find('shreddit-post[nsfw=""]');
        if (nsfwPost.length > 0) {
            $(this).css('display', 'none');
        }
    });
}

function showOnlyNSFWposts() {
    $('article.w-full.m-0').each(function() {
        const nsfwPost = $(this).find('shreddit-post[nsfw=""]');
        if (nsfwPost.length > 0) {
            $(this).css('display', 'block');
        }
        else {
            $(this).css('display', 'none');
        }
    });
}

function createEl(elementName, id /*optional*/ , attrArr /*optional*/ , parentEl /*optional*/ ) {

    var el = document.createElement(elementName);
    if (id) {
        el.id = id;
    }
    if (attrArr) {
        for (var attr in attrArr) {
            el.setAttribute(attr, attrArr[attr]);
        }
    }
    if (parentEl) {
        parentEl.appendChild(el);
    }
    return el;
}

function createText(txt) {

    return document.createTextNode(txt);
}

function appendCSS(obj) {

    var cssString = "",
        propString = "",
        eachSelector = "",
        style = createEl("style");
    for (var selector in CSS) {
        eachSelector = CSS[selector];
        propString = "";
        for (var property in eachSelector) {
            propString += property + ":" + eachSelector[property] + ";";
        }
        cssString += selector + "{" + propString + "}";
    }
    style.appendChild(createText(cssString));
    document.head.appendChild(style);
}


var nsfwSelect = document.createElement('select');
nsfwSelect.id = 'nsfwSelect';
document.body.appendChild(nsfwSelect);

var nsfwShowAll = document.createElement('option');
nsfwShowAll.value = 'Show All Posts';
nsfwShowAll.innerHTML = 'Show All Posts';
nsfwSelect.appendChild(nsfwShowAll);

var nsfwHideNSFW = document.createElement('option');
nsfwHideNSFW.value = 'Hide NSFW Posts';
nsfwHideNSFW.innerHTML = 'Hide NSFW Posts';
nsfwSelect.appendChild(nsfwHideNSFW);

var nsfwShowOnlyNSFW = document.createElement('option');
nsfwShowOnlyNSFW.value = 'Show Only NSFW Posts';
nsfwShowOnlyNSFW.innerHTML = 'Show Only NSFW Posts';
nsfwSelect.appendChild(nsfwShowOnlyNSFW);

var timer = setInterval(
    function() {

        if (nsfwSelect.options[nsfwSelect.selectedIndex].text == "Hide NSFW Posts") {
            hideNSFWPosts();
        } else if (nsfwSelect.options[nsfwSelect.selectedIndex].text == "Show Only NSFW Posts") {

            showOnlyNSFWposts();
        } else {
            showAllPosts();
        }
    },
    1000

);

var CSS = {
    '#nsfwSelect': {
        'position': 'fixed',
        'bottom': '20px',
        'left': '3px',
        'z-index': '999',
        'padding': '8px 12px',
        'border-radius': '6px',
        'border': '1px solid rgba(0, 123, 255, 0.3)',
        'box-shadow': '0 0 12px rgba(0, 123, 255, 0.4), 0 0 20px rgba(0, 123, 255, 0.2)',
        'transition': 'all 0.3s ease',
        'outline': 'none'
    },
    '#nsfwSelect:hover': {
        'box-shadow': '0 0 16px rgba(0, 123, 255, 0.6), 0 0 30px rgba(0, 123, 255, 0.3)',
        'border-color': 'rgba(0, 123, 255, 0.6)'
    },
    '#nsfwSelect:focus': {
        'box-shadow': '0 0 20px rgba(0, 123, 255, 0.8), 0 0 40px rgba(0, 123, 255, 0.4)',
        'border-color': '#007bff'
    }
};

appendCSS(CSS);