IYF Ad Blocker

Filter ads on iyf.tv

目前為 2025-08-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         IYF Ad Blocker
// @name:zh-CN   IYF Ad Blocker on iyf.tv
// @namespace    http://tampermonkey.net/
// @version      0.1.19
// @description  Filter ads on iyf.tv
// @description:zh-CN  过滤广告 on iyf.tv
// @author       Dylan Zhang
// @include      https://www.iyf.tv/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=iyf.tv
// @license      MIT
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // variables
    const win = unsafeWindow
    let commonStyle
    let homePageStyle
    let playPageStyle
    let listPageStyle
    let discoveryPageStyle

    const TIME_OUT = 5000

    /* utilities */
    function $(selector) {
        return document.querySelector(selector)
    }

    function getCurrentPath() {
        const path = win.location.pathname.split('/')[1]
        return path ? `/${path}` : ''
    }

    function addEvent(el, type, handler) {
        if (typeof el === 'string') el = $(el)
        el.addEventListener(type, handler, false)
    }

    function ensureDOM(selector) {
        let timer
        let timeout
        let clearTimer = () => {
            if (timer) clearInterval(timer)
            if (timeout) clearTimeout(timeout)
        }

        const p = new Promise((resolve, reject) => {
            const find = () => {
                const el = $(selector)
                if (el) resolve(el)
            }
            const notFound = () => reject()
            timer = setInterval(find, 1000)
            timeout = setTimeout(notFound, TIME_OUT)
        })

        p.finally(() => clearTimer())

        return p
    }

    /* meta modification */
    const history = win.history
    const originalPushState = history.pushState
    const originalReplaceState = history.replaceState
    const pushstateEvent = new Event('pushstate')
    const replacestate = new Event('replacestate')

    // override pushState
    history.pushState = function() {
        const result = originalPushState.apply(this, arguments)
        win.dispatchEvent(pushstateEvent)
        return result
    };

    // override replaceState
    history.replaceState = function() {
        const result = originalReplaceState.apply(this, arguments)
        win.dispatchEvent(replacestate)
        return result
    }

    ;['popstate', 'pushstate', 'replaceState'].forEach(eventName => {
        addEvent(win, eventName, main)
    })

    /* common ads */
    function filterCommonAds() {
        if (!commonStyle) {
            commonStyle = GM_addStyle(`
                app-dn-user-menu-item:has(span.iconVIP),
                .login-inner-box,
                .gg-dl,
                #sticky-block .inner {
                   display: none!important;
                }
                .my-card.none-user,
                .none-user-content {
                    height: auto!important;
                }
                .login-frame-container .login-frame-box.heighter,
                .login-frame-container .inner {
                    width: auto!important;
                    margin-left: 0px!important;
                }
            `)
        }
    }

    /* home page */
    function filterHomePage() {
        if (!homePageStyle) {
            homePageStyle = GM_addStyle(`
                app-recommended-news:nth-of-type(2),
                app-classified-top-videos:nth-of-type(1) > app-home-collection,
                div:has(> app-discovery-in-home),
                .new-list,
                .sec4,
                .sec4 + .separater {
                   display: none!important;
                }
            `)
        }
    }

    /* play/watch page */
    let playerEl

    function filterPlayPage() {
        if (!playPageStyle) {
            playPageStyle = GM_addStyle(`
                div.ps > div.bl,
                .video-player > div:last-child,
                vg-pause-f,
                .overlay-logo,
                .caption {
                   display: none!important;
                }

                .player-title {
                    margin-left: 0!important;
                }
            `)
        }

        if (!$('#video_player')) {
            filterPlayer()
        }
    }

    function filterPlayer() {
        init()

        function init() {
            ensureDOM('#video_player').then(el => {
                playerEl = el
                setPlayer()
            })
        }

        function setPlayer(){
            addEvent(playerEl, 'pause', onPause)
            closeDanmu()
        }

        function closeDanmu() {
            const btn = $('.action-pannel i.iconfont')
            const openedName = 'icondanmukai'
            if (btn?.classList.contains(openedName)) {
                btn.click()
            }
        }

        function onPause(){
            let pauseOverlayEl = $('.publicbox')
            if (pauseOverlayEl) {
                pauseOverlayEl.remove()
                pauseOverlayEl = null
                if (playerEl.paused) {
                    playerEl.play()
                }
            }
        }
    }

    /* list page */
    function filterListPage() {
        if (!listPageStyle) {
            listPageStyle = GM_addStyle(`
                #filterDiv {
                    width: 100%;
                }
                div.ss-ctn:has(img[alt="广告"]) {
                    display: none!important;
                }
            `)
        }
    }

    /* discovery page */
    function filterDiscoveryPage() {
        if (!discoveryPageStyle) {
            discoveryPageStyle = GM_addStyle(`
                .block-top {
                    display: none!important;
                }
            `)
        }
    }

    // main
    function main() {
        filterCommonAds()

        const path = getCurrentPath()
        switch (path) {
            case '':
                filterHomePage()
                break
            case '/watch':
            case '/play':
                filterPlayPage()
                break
            case '/list':
                filterListPage()
                break
            case '/discovery':
                filterDiscoveryPage()
                break
        }
    }

    main()
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址