// ==UserScript==
// @name         bili_ads_remove
// @namespace    none
// @version      2025-02-17
// @description 移除烦人的广告,推广,评论区,关闭弹窗,屏蔽各种直播推送,默认启用暗黑样式
// @author       Wos
// @match        https://www.bilibili.com/*
// @match        https://search.bilibili.com/*
// ==/UserScript==

(function() {
    'use strict';

    const adSelectors = [
        '.desktop-download-tip',
        'div[class="bpx-player-top-issue"]',
        'div[class="bpx-player-top-left"]',
        'div[class="activity-m-v1 act-now"]',
        'div[id="slide_ad"]',
        'div[class="video-card-ad-small"]',
        'div[class="video-page-game-card-small"]',
        'div[class*="ad-floor-cover"]',
        'div[class="activity-m-v1 act-end"]',
        '#notice',
        'a[class*="ad-report"]',
        'div[id="commentapp"]',
        'div[class*="bili-live-card"]',
        'div[class="floor-single-card"]',
        'div[class*="recommended-swipe"]',
        'div[id="biliMainFooter"]',
        '#arc_toolbar_report',
        '#v_desc',
        'div[class="video-tag-container"]',
        'div[class="bpx-player-sending-area"]',
        'div[class="header-banner__inner"]',
        'div[class="animated-banner"]',
        'div[class="banner-carousel channel-carousel"]',
    ];

    // 定义一个函数,用于移除广告元素
    function removeAds() {
        adSelectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(elem => {
                if (elem) {
                    elem.style.display = 'none';
                }
            });
        });
        // 可以在这里添加其他针对特定元素的操作,比如隐藏或修改样式
    }

    // 观察目标元素(整个body)变化
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            // 当节点被添加时,尝试移除广告元素
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                removeAds();

                // 额外检测特定元素,隐藏或修改样式
                // 例如隐藏带有广告标识的卡片
                const videoCards = document.querySelectorAll('.bili-video-card, .feed-card');
                videoCards.forEach(card => {
                    if (card.querySelector('.bili-video-card__info--ad') ||
                        card.querySelector('.vui_icon') ||
                        (card.querySelector('.bili-video-card__stats--text') && card.querySelector('.bili-video-card__stats--text').innerText === "广告")) {
                        card.style.display = 'none';
                    }
                });

                // 移除特定的布局元素
                document.querySelectorAll('.col_3.col_xs_1_5.col_md_2.col_xl_1_7.mb_x40').forEach(elem => {
                    if (elem.querySelector('.bili-video-card').style.display === 'none') {
                        // 也可以直接删除
                        elem.remove();
                    }else if(elem.querySelector('.bili-video-card__info--cheese') !== null){
                        elem.remove();
                    }
                });

                // 隐藏banner
                document.querySelectorAll('.bili-grid').forEach(grid => {
                    if (grid.getAttribute('data-report') === 'eva_banner.all') {
                        grid.style.display = 'none';
                    }
                });

                // 隐藏轮播广告
                document.querySelectorAll('.carousel-slide-v2').forEach(carousel => {
                    if (carousel.querySelector('.channel-ad-icon')) {
                        carousel.style.display = 'none';
                    }
                });

                // 隐藏广告举报按钮
                document.querySelectorAll('.ad-report').forEach(el => {
                    el.style.display = 'none';
                });

                // 自动点击关闭按钮
                document.querySelectorAll('.close-btn').forEach(btn => {
                    btn.click();
                });

                if (!document.querySelector('#__css-map__').href.includes('dark.css')){
                    document.querySelector('#__css-map__').href = "https://s1.hdslb.com/bfs/seed/jinkela/short/bili-theme/dark.css";

                }

                // 关闭弹幕(假设有player对象)
                if (typeof player !== 'undefined' && player.danmaku.isOpen()) {
                    player.danmaku.close();
                }
            }
        });
    });

    // 开始观察整个document.body
    observer.observe(document.body, {
        childList: true,
        subtree: true,
    });

    // 页面加载后立即执行一次
    window.addEventListener('load', () => {
        removeAds();
    });

})();