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.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==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);