Google Block Website Results

Adds the option to hide search results from the google search. A new block icon is visible next to the search result. When clicked you are promted to confirm the blocking of the domain. The amount of search results will not be adjusted, so if you block all websites on page 1, then page 1 will be empty. You can remove websites right now only by editing the script storage,

目前為 2024-12-24 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Google Block Website Results
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  Adds the option to hide search results from the google search. A new block icon is visible next to the search result. When clicked you are promted to confirm the blocking of the domain. The amount of search results will not be adjusted, so if you block all websites on page 1, then page 1 will be empty. You can remove websites right now only by editing the script storage,
// @author       LostFelix
// @license      MIT
// @match        https://www.google.com/search*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function() {
    'use strict';
    let domainsToBlock = [];
    init();
    function init(){
        domainsToBlock = GM_getValue('domainsToBlock',[]);
        nuke();
        setInterval(nuke,1000);
    }
    function nuke(force = false){
        let search = document.querySelector('#search');
        if(!search){ return; }

        if(force){
            search.querySelectorAll('.MjjYud .A6K0A .Ww4FFb a[jsname="UWckNb"][data-nuked]').forEach((el) => {
                delete el.dataset.nuked;
            });
        }
        search.querySelectorAll('.MjjYud .A6K0A .Ww4FFb a[jsname="UWckNb"]:not([data-nuked])').forEach((el) => {
            if(el.classList.length > 0){
                return;
            }
            domainsToBlock.forEach( (domain) => {
                if(el.href.includes(domain)){
                    let parent = getListElParentTop(el);
                    parent.style.opacity = 0.5;
                    parent.style.transform = "scale(0.75)";
                    parent.style.transformOrigin = "top left";
                    parent.style.height = parent.getBoundingClientRect().height +"px";
                    parent.style.display = "none";
                }
            });
            addBlockButton(el);
            el.dataset.nuked = "1";
        });
    }
    function addBlockButton(link){
        const el = getListElParentTop(link).querySelector('.D6lY4c');
        if(el.querySelector('.nuke-button')){
            return;
        };
        el.style.position = "relative";
        let btn = document.createElement('button');
        btn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" style="fill: red; height: 14px; width: 14px; margin: auto;" viewBox="0 0 512 512"><!--!Font Awesome Free 6.7.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M367.2 412.5L99.5 144.8C77.1 176.1 64 214.5 64 256c0 106 86 192 192 192c41.5 0 79.9-13.1 111.2-35.5zm45.3-45.3C434.9 335.9 448 297.5 448 256c0-106-86-192-192-192c-41.5 0-79.9 13.1-111.2 35.5L412.5 367.2zM0 256a256 256 0 1 1 512 0A256 256 0 1 1 0 256z"/></svg>';
        btn.classList.add('nuke-button');
        btn.style.background = "transparent";
        btn.style.border = 0;
        btn.style.cursor = "pointer";
        btn.style.height = "100%";
        btn.style.padding = 0;
        btn.style.margin = "0 3px";
        btn.style.display = "inline-block";
        btn.addEventListener('click',(e) => {
            e.stopPropagation(); e.preventDefault();
            let href = link.href;
            let domain = href.replace(/^http.*\/\//,'').replace(/\/.*/,'');
            if(!domainsToBlock.includes(domain)){
                if(window.confirm("Do you want to block: "+domain)){
                    domainsToBlock.push(domain);
                    GM_setValue('domainsToBlock',domainsToBlock);
                    nuke(true)
                }
            }
        });
        el.appendChild(btn);
        console.log(el);
    }
    function getListElParentTop(el){
        let parent = el.parentElement;
        let target = null
        while(parent){
            if(parent.classList.contains('MjjYud')){
                if(parent.parentElement.classList.contains('hlcw0c')){
                    target = parent.parentElement;
                }else{
                    target = parent;
                }
                parent = null;
            }else{
                parent = parent.parentElement;
            }
        }
        return target;
    }

})();