eduardog3000 / hexbear post filter

// ==UserScript==
// @name         hexbear post filter
// @namespace    https://openuserjs.org/users/eduardog3000
// @version      0.1
// @description  Filter posts on chapo.chat based on keywords.
// @author       eduardog3000
// @match        https://www.chapo.chat/*
// @match        https://hexbear.net/*
// @grant        none
// @require      http://code.jquery.com/jquery-1.7.2.min.js
// @require      https://gist.github.com/raw/2625891/waitForKeyElements.js
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    // Don't change above this line unless you know what you're doing.

    let filters = ['dog', 'vegan', 'vaush', 'v*ush', 'slaughter'];
    let caseSensitive = false;

    // Don't change below this line unless you know what yuu're doing.
    String.prototype.includesAny = function(search, caseSensitive = true) {
        if(Array.isArray(search)) {
            for(let s of search) {
                if(!caseSensitive && this.toLowerCase().includes(s.toLowerCase())) return true;
                else if(caseSensitive && this.includes(s)) return true;
            }
        } else {
            if(!caseSensitive && this.toLowerCase().includes(search.toLowerCase())) return true;
            else if(caseSensitive && this.includes(search)) return true;
        }
        return false;
    }

    let filtered = [];
    let filter = true;

    // Find posts to be filtered, hide them, and add them to the list for later reference.
    waitForKeyElements('.post-listing-row', ({ context: post }) => {
        if(post.textContent.includesAny(filters, caseSensitive)) {
            filtered.push(post);
            post.style.display = 'none';
        }
    });

    // Create button for toggling the filter.
    waitForKeyElements('.main-content-wrapper', ({ context: elm }) => {
        let dest = elm.querySelector('div:first-child > div:first-child > div:nth-of-type(2)');
        let sib = dest.querySelector('div:last-child > div');
        let active = sib.querySelector('label:first-child').className;
        let inactive = sib.querySelector('label:last-child').className;

        let button = document.createElement('button');
        button.textContent = 'Filter';
        button.className = active;
        button.addEventListener('click', function() {
            filter = !filter;
            if(filter) {
                //button.textContent = 'Filter On';
                button.className = active;
            } else {
                //button.textContent = 'Filter Off';
                button.className = inactive;
            }
            for(let post of filtered) {
                if(filter) post.style.display = 'none';
                else post.style.display = '';
            }
        });

        //let showList = false;
        //let edit = document.createElement('button');
        //edit.textContent = 'Filter List';
        //edit.className = inactive;
        //edit.addEventListener('click')

        dest.appendChild(button);
    });
})();