Hide Cakeday Comments

Hides comments that contain cakeday-related keywords

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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