小霸王网站广告屏蔽

实时屏蔽小霸王网站的Google广告元素

// ==UserScript==
// @name         小霸王网站广告屏蔽
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  实时屏蔽小霸王网站的Google广告元素
// @author       bbbyqq
// @license      MIT
// @match        *://www.yikm.net/*
// @match        *://yikm.net/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // 定义隐藏广告函数
    const hideAds = () => {
        document.querySelectorAll('.adsbygoogle').forEach(ad => {
            ad.remove()
        });
        document.querySelectorAll('#google-anno-sa').forEach(ad => {
            ad.remove()
        });
        // 删除广告出现时间
        localStorage.removeItem('adsexptime')
    };

    // 初始执行隐藏
    hideAds();

    // 创建MutationObserver监听DOM变化
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.addedNodes.length) {
                hideAds(); // 发现新增节点时执行隐藏
            }
        });
    });

    // 开始监听body及其子元素变化
    observer.observe(document.body, {
        childList: true,
        subtree: true,
        attributes: false,
        characterData: false
    });
})();