Word Blocker

Block specific words on webpages

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Word Blocker
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Block specific words on webpages
// @match        *://*/*
// @grant        none
// @author       Edu Altamirano
// @website      https://www.cocoalopez.com/blog
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // List of words to block
        var blockedWords = ['boluarte', 'milei', 'salinas', 'xochitl', 'netanyahu', 'israel', 'boluarte', 'ecuador', 'noboa', 'piqué', 'sunak' ];

    // List of websites to filter
    var websitesToFilter = ['example.com', 'another-example.com'];

    // List of websites to exclude from filtering
    var excludedWebsites = ['excluded-example.com', 'another-excluded-example.com'];

    // User choice: 'all', 'list', or 'none'
    var userChoice = 'all'; // Change this to your preference

    // Function to check if an element contains a blocked word
    function containsBlockedWord(element) {
        var text = element.textContent.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
        for (var i = 0; i < blockedWords.length; i++) {
            var blockedWord = blockedWords[i].normalize("NFD").replace(/[\u0300-\u036f]/g, "");
            if (text.includes(blockedWord)) {
                return true;
            }
        }
        return false;
    }


    // Function to hide elements containing blocked words
    function hideBlockedElements() {
        var elements = document.querySelectorAll('p, h1, h2, h3, h4, h5, h6, a, span');
        elements.forEach(function(element) {
            if (containsBlockedWord(element)) {
                // Cross out and color red the blocked words
                element.innerHTML = element.innerHTML.replace(new RegExp(blockedWords.join("|"), "gi"), function(matched){
                    return '<span style="color: red; text-decoration: line-through;">' + matched + '</span>';
                });

                // Wait a second, then add a blur effect before hiding the element
                setTimeout(function() {
                    element.style.transition = 'all 0.5s';
                    element.style.filter = 'blur(10px)';
                    setTimeout(function() {
                        element.style.display = 'none';
                    }, 500);
                }, 1000);

                // Also hide the closest parent div
                var parent = element.parentElement;
                while (parent) {
                    if (parent.tagName.toLowerCase() === 'div') {
                        (function(parent) {
                            setTimeout(function() {
                                parent.style.transition = 'all 0.5s';
                                parent.style.filter = 'blur(10px)';
                                setTimeout(function() {
                                    parent.style.display = 'none';
                                }, 500);
                            }, 1000);
                        })(parent);
                        break;
                    }
                    parent = parent.parentElement;
                }

                // Also hide any preceding img elements within the same parent element
                var sibling = element.previousElementSibling;
                while (sibling) {
                    if (sibling.tagName.toLowerCase() === 'img') {
                        (function(sibling) {
                            setTimeout(function() {
                                sibling.style.transition = 'all 0.5s';
                                sibling.style.filter = 'blur(10px)';
                                setTimeout(function() {
                                    sibling.style.display = 'none';
                                }, 500);
                            }, 1000);
                        })(sibling);
                    }
                    sibling = sibling.previousElementSibling;
                }
            }
        });

        // Hide img elements with alt text or sibling a element text containing blocked words
        var images = document.querySelectorAll('img');
        images.forEach(function(img) {
            var altText = img.alt.toLowerCase();
            for (var i = 0; i < blockedWords.length; i++) {
                if (altText.includes(blockedWords[i])) {
                    setTimeout(function() {
                        img.style.transition = 'all 0.5s';
                        img.style.filter = 'blur(10px)';
                        setTimeout(function() {
                            img.style.display = 'none';
                        }, 500);
                    }, 1000);
                    break;
                }
            }

            var sibling = img.nextElementSibling;
            while (sibling) {
                if (sibling.tagName.toLowerCase() === 'a' && containsBlockedWord(sibling)) {
                    (function(sibling) {
                        setTimeout(function() {
                            img.style.transition = 'all 0.5s';
                            img.style.filter = 'blur(10px)';
                            setTimeout(function() {
                                img.style.display = 'none';
                            }, 500);
                        }, 1000);
                    })(sibling);
                    break;
                }
                sibling = sibling.nextElementSibling;
            }
        });
    }

    // Check if the current website should be filtered
    function shouldFilterWebsite() {
        var currentWebsite = window.location.hostname;
        if (excludedWebsites.includes(currentWebsite)) {
            return false;
        } else if (userChoice === 'all' || (userChoice === 'list' && websitesToFilter.includes(currentWebsite))) {
            return true;
        } else {
            return false;
        }
    }

    // Run the script after the DOM is fully loaded
    window.addEventListener('load', function() {
        if (shouldFilterWebsite()) {
            hideBlockedElements();
        }
    }, false);
})();