Reddit Chemo

Remove ads and unwanted subreddit posts on new reddit

Mint 2022.01.24.. Lásd a legutóbbi verzió

// ==UserScript==
// @name         Reddit Chemo
// @namespace    Reddit Chemo
// @version      0.2.0
// @description  Remove ads and unwanted subreddit posts on new reddit
// @author       Lawrence Sim
// @license      WTFPL (http://www.wtfpl.net)
// @grant        none
// @match        *://*.reddit.com/*
// ==/UserScript==
(function() {
    var banSubreddits = [
        "amcstock",
        "animalcrossing",
        "anime_irl",
        "animemes",
        "animememes",
        "apexlegends",
        "apexoutlands",
        "askouija",
        "bokunoheroacademia",
        "bokunometaacademia",
        "breath_of_the_wild",
        "chloe",
        "codwarzone",
        "conservative",
        "copypasta",
        "cyberpunkgame",
        "dankmemes",
        "destinythegame",
        "destiny2",
        "dnd",
        "dndmemes",
        "dndnext",
        "dogelore",
        "dota2",
        "fortnitebr",
        "funhaus",
        "gamingcirclejerk",
        "genshin_impact",
        "genshin_impact_leaks",
        "genshin_memepact",
        "globaloffensive",
        "gme",
        "grandorder",
        "greentext",
        "grimdank",
        "gtaonline",
        "h3h3productions",
        "halo",
        "heathstone",
        "highqualitygifs",
        "hoi4",
        "hollowknightmemes",
        "hololive",
        "kpop",
        "ksi",
        "leagueoflegends",
        "leagueofmemes",
        "livestreamfail",
        "lotrmemes",
        "lowsodiumcyberpunk",
        "marvelmemes",
        "marvelstudios",
        "memeeconomy",
        "minecraft",
        "modernwarfare",
        "nintendoswitch",
        "nukedmemes",
        "onepiece",
        "otmemes",
        "overwatch",
        "overwatch_memes",
        "pcgaming",
        "pcmasterrace",
        "pewdiepiesubmissions",
        "pokemon",
        "pokemongo",
        "politcalcompassmemes",
        "prequelmemes",
        "ps4",
        "ps5",
        "raimimemes",
        "roosterteeth",
        "sequelmemes",
        "skyrim",
        "shitpostcrusaders",
        "shitposting",
        "smashbros",
        "superstonk",
        "surrealmemes",
        "tf2",
        "theboys",
        "themonkeyspaw",
        "tinder",
        "titanfall",
        "valorant",
        "warhammer40k",
        "wholesomeanimemes",
        "wholesomejojo",
        "wow",
        "xboxone"
    ];
    var showRemovedMessage = true;

    //-------------------------------------------------------------------------------------

    banSubreddits = banSubreddits.map(n => (n.startsWith("r/") ? n : `r/${n}`).toLowerCase());

    function blockPost(post, sub) {
        if(!showRemovedMessage) return post.remove();
        post.style.background = "rgb(80 70 70)";
        for(let i = 0; i < post.children.length; ++i) {
            let child = post.children[i];
            if(child.children[0] && child.children[0].id.startsWith("vote-arrows")) {
                child.style.top = "-0.7em";
                let voteElem = child.children[0];
                for(let j = voteElem.children.length-1; j >= 0; --j) {
                    let subelm = voteElem.children[j];
                    if(subelm.nodeName.toLowerCase() === "button" && subelm.getAttribute("data-click-id") === "downvote") {
                        let icon = subelm.querySelector(".icon");
                        icon.style["line-height"] = "15px";
                        icon.style["font-size"] = "14px";
                    } else {
                        subelm.remove();
                    }
                }
            } else if(child.getAttribute("data-click-id") === "background") {
                child.style.background = "none";
                child.style.color = "rgb(163 149 149)";
                child.style.padding = "0.3em";
                child.style["font-size"] = "0.7em";
                child.innerHTML = `Post from ${sub} removed`;
            } else {
                child.remove()
            }
        }
        console.log("Banned subreddit ("+sub+") post removed.");
    }

    function fuckRedditAds(mutated) {
        (mutated || [{target: document.body}]).forEach(mutant => {
            mutant.target.querySelectorAll("div[data-testid='post-container']").forEach(post => {
                if(post.getAttribute("chemo")) return;
                let ad = false;
                post.querySelectorAll("span").forEach(span => !ad ? (ad = span.innerText === "promoted") : ad);
                if(ad) {
                    post.remove();
                    post.setAttribute("chemo", 1);
                    console.log("Ad removed.");
                    return;
                }
                let subreddit = post.querySelector("a[data-click-id='subreddit']");
                if(subreddit && subreddit.innerText) {
                    let ban = ~banSubreddits.indexOf(subreddit.innerText.toLowerCase());
                    if(ban) blockPost(post, banSubreddits[~ban]);
                    post.setAttribute("chemo", 1);
                    return;
                }
                let postObserver = new MutationObserver((mutated, observer) => {
                    subreddit = post.querySelector("a[data-click-id='subreddit']")
                    if(subreddit && subreddit.innerText) {
                        let ban = ~banSubreddits.indexOf(subreddit.innerText.toLowerCase());
                        if(ban) blockPost(post, banSubreddits[~ban]);
                        observer.disconnect();
                    }
                });
                postObserver.observe(post, {childList:true, subtree:true, attributes:true});
                post.setAttribute("chemo", 1);
            });
        });
    };

    fuckRedditAds();
    (new MutationObserver(fuckRedditAds)).observe(document.body, {childList:true, subtree:true});

})();