Filter reddit post with certain keywords in the title

Hide elements with specific text content

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Filter reddit post with certain keywords in the title
// @namespace    Filter reddit post
// @version      1.0.1
// @description  Hide elements with specific text content
// @author       ein
// @match        https://new.reddit.com/*
// @license MIT
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function getKeywords() {
        // edit your keywords here
        return /keyword1|keyword2|keyword3/i;
    }

    function hideElements() {
        var elements = document.querySelectorAll('._2FCtq-QzlfuN-SwVMUZMM3');
        var keywords = getKeywords();
        elements.forEach(function(el) {
            if (keywords.test(el.innerText)) {
                var ancestor = el.closest('._1Qs6zz6oqdrQbR7yE_ntfY, ._3xuFbFM3vrCqdGuKGhhhn0').parentElement.parentElement;
                if (ancestor) {
                    ancestor.style.display = 'none';
                }
            }
        });
    }

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