Twitch Shared Chat Filter

I got annoyed by spam from other chats so I yeet it

// ==UserScript==
// @name         Twitch Shared Chat Filter
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  I got annoyed by spam from other chats so I yeet it
// @author       Tilted Toast
// @match        https://www.twitch.tv/*
// @run-at       document-start
// @license      MIT
// @website      https://greasyfork.org/en/scripts/520551-twitch-shared-chat-filter
// @grant        GM_registerMenuCommand
// ==/UserScript==

(async function() {
    'use strict';

    let isFilterActive = true;

    function createToggleButton() {
        if (!isFilterActive) return;
        const btn = document.createElement('button');
        btn.textContent = 'Active';
        btn.style.position = 'fixed';
        btn.style.bottom = '10px';
        btn.style.right = '130px';
        btn.style.zIndex = 10000;
        btn.style.padding = '5px';
        btn.style.backgroundColor = '#9147ff';
        btn.style.color = 'white';
        btn.style.border = 'none';
        btn.style.borderRadius = '5px';
        btn.style.cursor = 'pointer';

        btn.addEventListener('click', () => {
            isFilterActive = !isFilterActive;
            btn.textContent = `${isFilterActive ? 'Active' : 'Inactive'}`;
            console.log(`Shared Chat Filter is now ${isFilterActive ? 'enabled' : 'disabled'}.`);
        });

        const parent = document.querySelector(".chat-input__buttons-container");

        parent.appendChild(btn);
    }

    function filterSharedChatMessages() {
        isFilterActive = true

        // Get the current streamer's name from the URL
        const currentStreamer = window.location.pathname.split('/')[1].toLowerCase();

        if (!currentStreamer) {
            return console.error('Unable to detect streamer name from the URL.');
        }

        // Check if Shared Chat mode is active
        const sharedChatIndicator = document.querySelector('#chat-room-header-label');
        if (!sharedChatIndicator || sharedChatIndicator.textContent !== 'Shared Chat') {
            return console.error('Shared Chat is not active.');
        }

        // Get a list of streamer participants
        const streamers = Array.from(
            document.querySelectorAll('[class*="sharedChatHeaderAvatarSmall--"] > div > div > div > .tw-image-avatar')
        ).map(s => s.alt.toLowerCase());

        if (!streamers || streamers.length == 0) {
            return console.error("Unable to find shared chat avatars");
        }

        console.log("Streamers:", streamers);

        console.log(`Shared Chat is active for ${currentStreamer}.`);

        const chatContainer = document.querySelector('.seventv-chat-list');
        if (!chatContainer) {
            return console.error('Chat container not found!');
        }

        createToggleButton();


        const observer = new MutationObserver((mutationsList) => {
            if (!isFilterActive) return;

            for (const mutation of mutationsList) {
                if (mutation.addedNodes.length) {
                    mutation.addedNodes.forEach((node) => {
                        if (node.nodeType === Node.ELEMENT_NODE) {

                            // We allow either a message with no "streamer badge" or a message with the current page's streamer's badge
                            // We also only care about the first badge, since if there's a "streamer badge" they're always first
                            const badge = node.querySelector('.seventv-chat-badge img');
                            if (!badge) return;

                            const badgeAlt = badge.alt.toLowerCase();
                            if (!streamers.includes(badgeAlt)) return;

                            if (badgeAlt != currentStreamer) {
                                node.style.display = "none";
                            }
                        }
                    });
                }
            }
        });

        observer.observe(chatContainer, { childList: true, subtree: true });
    }

    const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));

    await sleep(10000)
    filterSharedChatMessages();

    GM_registerMenuCommand("Run Script", filterSharedChatMessages);
})();