Word and Image Blocker

Block words, phrases and images associated with those phrases. For the UI to add and delete words and phrases to block, please also install the following, https://greasyfork.org/en/scripts/481834-word-and-image-blocker-settings-ui

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Word and Image Blocker
// @namespace    https://github.com/def-initive/
// @version      1.1
// @description  Block words, phrases and images associated with those phrases. For the UI to add and delete words and phrases to block, please also install the following, https://greasyfork.org/en/scripts/481834-word-and-image-blocker-settings-ui
// @author       def-initive
// @match        *://*/*
// @exclude      *://github.com/*
// @exclude      *://greasyfork.org/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // Function to hide images in element, its parents, and siblings
    function hideImagesInElementAndSiblings(element) {
        // Hide images in the element itself
        var imagesInElement = element.querySelectorAll('img');
        imagesInElement.forEach(function (image) {
            image.style.display = "none";
        });

        // Hide images in siblings
        var siblings = Array.from(element.parentElement.children);
        siblings.forEach(function (sibling) {
            if (sibling !== element) {
                var imagesInSibling = sibling.querySelectorAll('img');
                imagesInSibling.forEach(function (image) {
                    image.style.display = "none";
                });
            }
        });
    }

    // Function to hide direct parent containers and their siblings' images
    // containing occurrences of a phrase on a webpage
    function hideParentAndSiblings(phrase) {
        var textNodes = document.createTreeWalker(
            document.body,
            NodeFilter.SHOW_TEXT,
            null,
            false
        );

        var elementsToRemove = [];

        while (textNodes.nextNode()) {
            var textNode = textNodes.currentNode;
            var match = textNode.nodeValue.match(new RegExp(phrase, 'gi'));

            if (match) {
                var parentContainer = textNode.parentElement;
                elementsToRemove.push(parentContainer);
            }
        }

        elementsToRemove.forEach(function (element) {
            // Check if the element is part of the UI created by "Blocked Phrases Settings"
            if (!element.closest('.blocked-phrases-settings-container')) {
                // Hide parent containers
                element.style.color = "black";
                element.style.backgroundColor = "black";

                // Hide images in siblings
                hideImagesInElementAndSiblings(element);
            }
        });
    }

    // Load target phrases from local storage
    var storedPhrases = localStorage.getItem('blockedPhrases');
    var targetPhrases = storedPhrases ? JSON.parse(storedPhrases) : [];

    // Hide direct parent containers and images in their siblings
    targetPhrases.forEach(function (phrase) {
        hideParentAndSiblings(phrase);
    });

    // Observe changes in the DOM to handle dynamically loaded content
    var observer = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            if (mutation.addedNodes && mutation.addedNodes.length > 0) {
                targetPhrases.forEach(function (phrase) {
                    hideParentAndSiblings(phrase);
                });
            }
        });
    });

    // Configuration of the observer
    var observerConfig = {
        childList: true,
        subtree: true
    };

    // Start observing the DOM
    observer.observe(document.body, observerConfig);

})();