Word Blocker

Block specific words on webpages

目前為 2024-03-30 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 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);
})();