Hide Cakeday Comments

Hides comments that contain cakeday-related keywords

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				Hide Cakeday Comments
// @namespace			Hide Cakeday Comments
// @version				2.0
// @description			Hides comments that contain cakeday-related keywords
// @match				https://www.reddit.com/*
// @license              MIT
// ==/UserScript==

(function() {
    'use strict';

    var keywords = ["cakeday", "cake day", "happy cake", "and my axe"];

    function hideElement(element) {
        var innerHTML = element.innerHTML.toLowerCase();
        if (keywords.some(keyword => innerHTML.includes(keyword))) {
            var ancestor = element.closest('._3sf33-9rVAO_v4y0pIW_CH').parentElement.parentElement.parentElement;
            ancestor.style.display = 'none';

            var descendant = ancestor.querySelector('._1RIl585IYPW6cmNXwgRz0J');
            var currentLevel = parseInt(descendant.innerHTML.match(/(\d+)/)[1], 10);

            var newAncestor = ancestor.nextElementSibling;
            while (newAncestor) {
                var newDescendant = newAncestor.querySelector('._1RIl585IYPW6cmNXwgRz0J');
                var nextLevel = parseInt(newDescendant.innerHTML.match(/(\d+)/)[1], 10);

                if (nextLevel > currentLevel) {
                    newAncestor.style.display = 'none';
                } else if (nextLevel <= currentLevel) {
                    break;
                }

                newAncestor = newAncestor.nextElementSibling;
            }
        }
    }

    var elements = document.querySelectorAll('._1qeIAgB0cPwnLhDF9XSiJM, ._7T4UafM1PdBGycd5na9nF');
    elements.forEach(hideElement);

    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.type === 'childList') {
                hideElement();
            }
        });
    });
    var config = { childList: true, subtree: true };
    observer.observe(document.body, config);
})();