Word Blocker

Block specific words on webpages

当前为 2024-03-30 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Word Blocker
// @namespace    http://tampermonkey.net/
// @version      0.1
// @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', 'Xóchitl', 'Israel', 'Netanyahu', 'israel', 'Xóchitl', 'xochitl', 'Milei', 'Boluarte', 'Israeli', 'Macron', 'macron', 'futbol', 'fútbol', 'Argentina', 'argentina'];

    // 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.toLowerCase();
        for (var i = 0; i < blockedWords.length; i++) {
            if (text.includes(blockedWords[i])) {
                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)) {
                // Add a blur effect before hiding the element
                element.style.transition = 'all 0.5s';
                element.style.filter = 'blur(10px)';
                setTimeout(function() {
                    element.style.display = 'none';
                }, 500);

                // Also hide the closest parent div
                var parent = element.parentElement;
                while (parent) {
                    if (parent.tagName.toLowerCase() === 'div') {
                        (function(parent) {
                            parent.style.transition = 'all 0.5s';
                            parent.style.filter = 'blur(10px)';
                            setTimeout(function() {
                                parent.style.display = 'none';
                            }, 500);
                        })(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) {
                            sibling.style.transition = 'all 0.5s';
                            sibling.style.filter = 'blur(10px)';
                            setTimeout(function() {
                                sibling.style.display = 'none';
                            }, 500);
                        })(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])) {
                    img.style.transition = 'all 0.5s';
                    img.style.filter = 'blur(10px)';
                    setTimeout(function() {
                        img.style.display = 'none';
                    }, 500);
                    break;
                }
            }

            var sibling = img.nextElementSibling;
            while (sibling) {
                if (sibling.tagName.toLowerCase() === 'a' && containsBlockedWord(sibling)) {
                    (function(sibling) {
                        img.style.transition = 'all 0.5s';
                        img.style.filter = 'blur(10px)';
                        setTimeout(function() {
                            img.style.display = 'none';
                        }, 500);
                    })(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);
})();