Google Spam Filter

Clean up Google Search spam links

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Google Spam Filter
// @version      0.1
// @description  Clean up Google Search spam links
// @author       CrazyWolf13
// @icon         https://www.google.com/favicon.ico
// @match        https://www.google.com/search*
// @license      MIT
// @namespace https://greasyfork.org/users/1407887
// ==/UserScript==



(function () {
    'use strict';

    // Sponsored in different languages
    const SPONSORED_KEYWORDS = [
        'sponsored',    // English
        'gesponsert',   // German
        'patrocinado',  // Spanish
        'sponsorizzato',// Italian
        'parrainé',     // French
        'sponsoreret',  // Danish
        'реклама',      // Russian (means "advertisement")
        'sponsorerad',  // Swedish
        '広告',          // Japanese (means "advertisement")
        '赞助',          // Simplified Chinese (means "sponsored")
        'sponsorizat',  // Romanian
        'sponzoriran',  // Croatian
    ];

    // Number of levels to traverse up to find the parent element
    const TRAVERSE_LEVELS = 4;

    // Function to remove elements containing sponsored content
    function removeSponsoredElements() {
        const spans = document.querySelectorAll('span'); // Select all span elements

        spans.forEach(span => {
            const textContent = span.textContent?.toLowerCase().trim(); // Ensure content is lowercase and trimmed
            if (!textContent) return; // Skip empty or undefined content

            // Check if span contains any sponsored keyword
            const isSponsored = SPONSORED_KEYWORDS.some(keyword => textContent.includes(keyword));

            if (isSponsored) {
                let parent = span;

                // Traverse up the DOM tree
                for (let i = 0; i < TRAVERSE_LEVELS; i++) {
                    if (!parent.parentElement) break; // Stop if no parent exists
                    parent = parent.parentElement;
                }

                // Remove the parent element if it exists
                if (parent && parent.nodeType === Node.ELEMENT_NODE) {
                    console.log(`Removed sponsored element:`, parent);
                    parent.remove();
                }
            }
        });
    }

    // Function to handle DOM changes dynamically
    function observeDOMChanges() {
        const observer = new MutationObserver(() => {
            removeSponsoredElements(); // Re-run spam removal on DOM changes
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // Init
    function init() {
        console.log('Remove Sponsored Content initialized.');
        removeSponsoredElements(); 
        observeDOMChanges();       
    }
    window.addEventListener('DOMContentLoaded', init);

})();