fix the script

// ==UserScript==
// @name         4chan View Toggle with Thread Removal Fix
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Toggle viewing of images, videos, GIFs, and text on 4chan boards. Optionally remove threads without matching posts. OP thumbnail preserved.
// @author
// @match        *://boards.4chan.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Helper functions to set and get settings using localStorage
    function setSetting(name, value) {
        localStorage.setItem(name, value);
    }

    function getSetting(name) {
        return localStorage.getItem(name);
    }

    // Create the control panel
    var controlPanel = document.createElement('div');
    controlPanel.style.position = 'fixed';
    controlPanel.style.top = '0';
    controlPanel.style.left = '0';
    controlPanel.style.zIndex = '1000';
    controlPanel.style.backgroundColor = 'rgba(255,255,255,0.9)';
    controlPanel.style.padding = '5px';
    controlPanel.style.fontSize = '14px';
    controlPanel.style.borderBottomRightRadius = '5px';

    // Create checkboxes
    var checkboxImages = document.createElement('input');
    checkboxImages.type = 'checkbox';
    checkboxImages.id = 'toggleImages';

    var labelImages = document.createElement('label');
    labelImages.htmlFor = 'toggleImages';
    labelImages.innerText = ' Images';

    var checkboxGifs = document.createElement('input');
    checkboxGifs.type = 'checkbox';
    checkboxGifs.id = 'toggleGifs';
    checkboxGifs.style.marginLeft = '10px';

    var labelGifs = document.createElement('label');
    labelGifs.htmlFor = 'toggleGifs';
    labelGifs.innerText = ' GIFs';

    var checkboxVideos = document.createElement('input');
    checkboxVideos.type = 'checkbox';
    checkboxVideos.id = 'toggleVideos';
    checkboxVideos.style.marginLeft = '10px';

    var labelVideos = document.createElement('label');
    labelVideos.htmlFor = 'toggleVideos';
    labelVideos.innerText = ' Videos';

    var checkboxText = document.createElement('input');
    checkboxText.type = 'checkbox';
    checkboxText.id = 'toggleText';
    checkboxText.style.marginLeft = '10px';

    var labelText = document.createElement('label');
    labelText.htmlFor = 'toggleText';
    labelText.innerText = ' Text Posts';

    // Create toggle for removing threads without matching posts
    var checkboxRemoveThreads = document.createElement('input');
    checkboxRemoveThreads.type = 'checkbox';
    checkboxRemoveThreads.id = 'toggleRemoveThreads';
    checkboxRemoveThreads.style.marginLeft = '10px';

    var labelRemoveThreads = document.createElement('label');
    labelRemoveThreads.htmlFor = 'toggleRemoveThreads';
    labelRemoveThreads.innerText = ' Remove Threads Without Matching Posts';

    // Set checkbox states from settings (or default to true)
    checkboxImages.checked = getSetting('showImages') !== 'false';
    checkboxGifs.checked = getSetting('showGifs') !== 'false';
    checkboxVideos.checked = getSetting('showVideos') !== 'false';
    checkboxText.checked = getSetting('showText') !== 'false';
    checkboxRemoveThreads.checked = getSetting('removeThreads') === 'true'; // Default to false

    // Append checkboxes and labels to control panel
    controlPanel.appendChild(checkboxImages);
    controlPanel.appendChild(labelImages);
    controlPanel.appendChild(checkboxGifs);
    controlPanel.appendChild(labelGifs);
    controlPanel.appendChild(checkboxVideos);
    controlPanel.appendChild(labelVideos);
    controlPanel.appendChild(checkboxText);
    controlPanel.appendChild(labelText);
    controlPanel.appendChild(document.createElement('br')); // Line break
    controlPanel.appendChild(checkboxRemoveThreads);
    controlPanel.appendChild(labelRemoveThreads);

    // Append control panel to body
    document.body.appendChild(controlPanel);

    // Function to update visibility
    function updateVisibility() {
        var showImages = checkboxImages.checked;
        var showGifs = checkboxGifs.checked;
        var showVideos = checkboxVideos.checked;
        var showText = checkboxText.checked;
        var removeThreads = checkboxRemoveThreads.checked;

        // Save settings to localStorage
        setSetting('showImages', showImages);
        setSetting('showGifs', showGifs);
        setSetting('showVideos', showVideos);
        setSetting('showText', showText);
        setSetting('removeThreads', removeThreads);

        // Get all threads
        var threads = document.querySelectorAll('.thread');

        threads.forEach(function(thread) {
            var posts = thread.querySelectorAll('.postContainer');
            var threadHasVisiblePosts = false;

            posts.forEach(function(post, index) {
                var isOP = index === 0;

                // Apply the filters to determine whether the post matches
                var matchesFilters = doesPostMatchFilters(post);

                if (isOP) {
                    // Always display the OP post
                    post.style.display = '';
                    // Always display the thumbnail
                    var thumb = post.querySelector('.postFile, .file');
                    if (thumb) {
                        thumb.style.display = '';
                    }

                    // For filtering purposes, consider whether OP matches filters
                    if (matchesFilters) {
                        threadHasVisiblePosts = true;
                    }
                } else {
                    // For other posts, show or hide based on filters
                    if (matchesFilters) {
                        post.style.display = '';
                        threadHasVisiblePosts = true;
                    } else {
                        post.style.display = 'none';
                    }
                }
            });

            // Remove or show the thread based on visibility of its posts
            if (removeThreads) {
                if (!threadHasVisiblePosts) {
                    thread.style.display = 'none';
                } else {
                    thread.style.display = '';
                }
            } else {
                // If we're not removing threads, ensure they're displayed
                thread.style.display = '';
            }
        });
    }

    // Function to determine if a post matches the filters
    function doesPostMatchFilters(post) {
        var hasFileThumb = post.querySelector('.fileThumb');
        var fileExtension = '';
        var isGif = false;
        var isVideo = false;
        var isImage = false;
        var isText = !hasFileThumb;

        if (hasFileThumb) {
            var href = hasFileThumb.getAttribute('href');
            if (href) {
                var extMatch = href.match(/\.(\w+)(\?|$)/);
                if (extMatch) {
                    fileExtension = extMatch[1].toLowerCase();
                    if (fileExtension === 'gif') {
                        isGif = true;
                    } else if (fileExtension === 'webm' || fileExtension === 'mp4') {
                        isVideo = true;
                    } else {
                        isImage = true;
                    }
                }
            }
        }

        // Decide whether it matches the filters
        var matchesFilters = false;
        if (isGif && showGifs) {
            matchesFilters = true;
        } else if (isImage && showImages) {
            matchesFilters = true;
        } else if (isVideo && showVideos) {
            matchesFilters = true;
        } else if (isText && showText) {
            matchesFilters = true;
        }

        return matchesFilters;
    }

    // Add event listeners
    checkboxImages.addEventListener('change', updateVisibility);
    checkboxGifs.addEventListener('change', updateVisibility);
    checkboxVideos.addEventListener('change', updateVisibility);
    checkboxText.addEventListener('change', updateVisibility);
    checkboxRemoveThreads.addEventListener('change', updateVisibility);

    // Initial update
    updateVisibility();

    // Reapply visibility when new nodes are added (for dynamic content or navigation)
    var observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.addedNodes.length) {
                updateVisibility();
            }
        });
    });

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

})();