自用广告拦截器

广告拦截器

// ==UserScript==
// @name         自用广告拦截器
// @namespace    http://tampermonkey.net/
// @version      2025-02-23
// @description  广告拦截器
// @author       FlanChan
// @match   https://www.zkk79.com/*
// @match   http://www.zkk79.com/*
// @match   https://kemono.su/*
// @match   http://kemono.su/*
// @icon    https://www.google.com/s2/favicons?sz=64&domain=greasyfork.org
// @grant   none
// @license MIT
// ==/UserScript==
(function () {
    // 记录删除的广告数量
    let deletedAdCount = 0;

    function handleKemomoSuAd() {
        const adContainers = document.getElementsByClassName("ad-container")

        //关闭上下广告容器
        for (let adTag of adContainers) {
                adTag.style.display = "none"
                deletedAdCount++;
        }

        const adCloseBtn = document.getElementsByClassName("close-button--wsOv0")[0]

        if (adCloseBtn) {
            adCloseBtn.click()
            deletedAdCount++
        }

        if (deletedAdCount < 3) {
            setTimeout(() => handleKemomoSuAd(), 500)
        } else {
            deletedAdCount = 0
        }
    }

    function handleZkk79ComAd() {
        //获取广告标签
        const adTag = document.getElementsByTagName("divz")[0]

        //删除广告标签
        if (adTag) {
            adTag.remove()
        }

        if (deletedAdCount === 0) {
            setTimeout(() => handleZkk79ComAd(), 500)
        } else {
            deletedAdCount = 0
        }
    }

    function handleEhentaiOrgAd() {
        //获取广告标签
        const adTag = document.getElementsByClassName("ad-container")[0]

        //删除广告标签
        if (adTag) {
            adTag.remove()
        }

        if (deletedAdCount === 0) {
            setTimeout(() => handleEhentaiOrgAd(), 500)
        } else {
            deletedAdCount = 0
        }
    }

    // 域名对应的处理函数
    const domainFuncMap = {
        'kemono.su': handleKemomoSuAd,
        "www.zkk79.com": handleZkk79ComAd
    }

    function handleAd() {
        const currentDomain = window.location.hostname;
        setTimeout(() => {
            if (domainFuncMap[currentDomain]) {
                domainFuncMap[currentDomain]();
            }
        },500)
    }

    // 监听页面变化执行广告删除函数
    document.addEventListener('click', handleAd);
    window.addEventListener('load', handleAd);
    window.addEventListener('hashchange', handleAd);
    window.addEventListener('pushState', handleAd);
    window.addEventListener('popstate', handleAd);
    window.addEventListener('replaceState', handleAd);

})();