Greasy Fork

Greasy Fork is available in English.

隐藏B站充电和广告

隐藏B站动态瀑布流中的广告、评论区广告、充电内容以及美化首页

当前为 2025-03-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         隐藏B站充电和广告
// @version      1.21
// @description  隐藏B站动态瀑布流中的广告、评论区广告、充电内容以及美化首页
// @author       chemhunt
// @match        *.bilibili.com/*
// @match        https://t.bilibili.com/*
// @match        https://space.bilibili.com/*
// @match        https://www.bilibili.com/
// @match        https://www.bilibili.com/video/*
// @grant        none
// @license      GPL-3.0 License
// @namespace    http://greasyfork.icu/scripts/511437/
// ==/UserScript==

(function() {
    'use strict';

    // 白名单UP主,可自行修改、增删
    const whiteList = [
        '白名单UP_1', '白名单UP_2', '木鱼水心',
    ];

    const messageDiv = document.createElement('div');
    Object.assign(messageDiv.style, {
        position: 'fixed',
        top: '10px',
        right: '10px',
        padding: '10px',
        backgroundColor: 'rgba(0, 0, 0, 0.7)',
        color: 'white',
        borderRadius: '5px',
        zIndex: '9999',
        display: 'none'
    });

    document.body.appendChild(messageDiv);

    const blockedKeywords = [
        '拼多多', '淘宝', '京东', '天猫', '手淘', '旗舰店','运费',
        '特价', '领券', '下单', '礼包', '补贴', '优惠', '双11','双12', '618', '品牌方',
        '溪木源', '海力生', '萌牙家', '妙界', 'DAWEI', '温眠', '护肝', '护颈','护眼', '护枕', '按摩', '冲牙','牙刷',
        //'中奖', '预告', '抽奖',
    ];

    const blockedLinks = [
        'taobao.com', 'jd.com', 'pinduoduo.com', 'mall.bilibili.com', 'gaoneng.bilibili.com','yangkeduo.com',
    ];

    function hideItem(item) {
        item.style.display = 'none';
    }

    function showMessage(msg) {
        messageDiv.textContent = msg;
        messageDiv.style.display = 'block';
        setTimeout(() => {
            messageDiv.style.display = 'none';
        }, 3000);
    }

    // 检查评论区广告
    function checkCommentAds(item) {
        const commentAds = item.querySelectorAll(
            '.dynamic-card-comment .comment-list.has-limit .list-item.reply-wrap.is-top .con'
        );

        let AdCount = 0;

        commentAds.forEach(comment => {
            const links = comment.querySelectorAll('a');
            let foundAd = false;

            links.forEach(link => {
                const href = link.getAttribute('href');
                if (href && blockedLinks.some(blocked => href.includes(blocked))) {
                    foundAd = true;
                }
            });

            if (foundAd) {
                hideItem(comment);
                AdCount++;
            }
        });

        return AdCount;
    }

    let hiddenAdCount = 0;

    function checkCommentsForAds() {
        const commentsContainer = document.querySelector('#commentapp > bili-comments');
        if (commentsContainer && commentsContainer.shadowRoot) {
            const headerElement = commentsContainer.shadowRoot.querySelector("#header > bili-comments-header-renderer");
            if (headerElement && headerElement.shadowRoot) {
                const noticeElement = headerElement.shadowRoot.querySelector("#notice > bili-comments-notice");
                if (noticeElement && noticeElement.shadowRoot) {
                    const closeElement = noticeElement.shadowRoot.querySelector("#close");
                    if (closeElement) {
                        console.log("找到评论区横条,自动点击关闭按钮");
                        closeElement.click();  // 模拟点击关闭按钮
                    }
                }
            }

            const thread = commentsContainer.shadowRoot.querySelector('bili-comment-thread-renderer');
            if (thread && thread.shadowRoot) {
                const commentRenderer = thread.shadowRoot.querySelector('#comment');
                if (commentRenderer && commentRenderer.shadowRoot) {
                    const richText = commentRenderer.shadowRoot.querySelector('#content > bili-rich-text');
                    if (richText && richText.shadowRoot) {
                        const contentsElement = richText.shadowRoot.querySelector('#contents');
                        if (contentsElement) {
                            let foundAd = false;
                            const links = contentsElement.querySelectorAll('a');
                            links.forEach(link => {
                                const href = link.getAttribute('href');
                                if (href && blockedLinks.some(blocked => href.includes(blocked))) {
                                    foundAd = true;
                                }
                            });

                            if (foundAd) {
                                hideItem(thread);
                                hiddenAdCount++;
                                observer.disconnect();
                                 console.log('observer.disconnect()');
                                let message = `隐藏广告 x ${hiddenAdCount}`;
                                showMessage(message);
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    function checkForContentToHide() {
        let hiddenChargeCount = 0;
        let hiddenAdCount = 0;
        let hiddenFloorCardCount = 0;

        if (window.location.hostname === 't.bilibili.com' || window.location.hostname === 'space.bilibili.com') {
            const items = document.querySelectorAll('.bili-dyn-list__item');
            items.forEach(item => {
                const titleElement = item.querySelector('.bili-dyn-title');
                if (titleElement && whiteList.includes(titleElement.textContent.trim())) {
                    return;
                }

                const blockedmask = item.querySelector('.dyn-blocked-mask');
                const badge = item.querySelector('.bili-dyn-card-video__badge');
                const lotteryTitle = item.querySelector('.dyn-upower-lottery__title');
                const contentDiv = item.querySelector('.bili-dyn-content');
                const goods = item.querySelector('dyn-goods');
                const goods2 = item.querySelector('bili-dyn-card-goods');
                const spans = item.querySelectorAll('span');

                if (goods || goods2) {
                    hideItem(item);
                    hiddenAdCount++;
                } else if (blockedmask) {
                    hideItem(item);
                    hiddenChargeCount++;
                } else if (badge && badge.textContent.includes('专属') ||
                           Array.from(spans).some(span => span.textContent.includes('专属')) ||
                           lotteryTitle && lotteryTitle.textContent.includes('专属')) {
                    hideItem(item);
                    hiddenChargeCount++;
                } else if (contentDiv) {
                    const contentText = contentDiv.textContent;
                    if (blockedKeywords.some(keyword => contentText.includes(keyword))) {
                        hideItem(item);
                        hiddenAdCount++;
                    }
                }

                spans.forEach(span => {
                    const dataUrl = span.getAttribute('data-url');
                    if (dataUrl && blockedLinks.some(blocked => dataUrl.includes(blocked))) {
                        hideItem(item);
                        hiddenAdCount++;
                    }
                });

                hiddenAdCount += checkCommentAds(item);
            });
        }

        if (window.location.hostname === 'www.bilibili.com' && !window.location.pathname.startsWith('/video/')) {
            const floorCards = document.querySelectorAll('.floor-single-card');
            floorCards.forEach(card => {
                hideItem(card);
                hiddenFloorCardCount++;
            });

            const targetElement = document.querySelector('.recommended-swipe.grid-anchor');
            if (targetElement) {
                console.log('隐藏首页大屏');
                hideItem(targetElement);
            }
        }

        if (window.location.pathname.startsWith('/video/')) {
            if (!checkCommentsForAds()) {
                setTimeout(() => {
                    checkCommentsForAds();
                }, 3000);
            }
        }

        let message = '';
        if (hiddenChargeCount > 0) {
            message += `隐藏充电 x ${hiddenChargeCount} `;
        }
        if (hiddenAdCount > 0) {
            message += `隐藏广告 x ${hiddenAdCount} `;
        }

        if (message) {
            showMessage(message.trim());
        }
    }

    const observer = new MutationObserver(checkForContentToHide);
    observer.observe(document.body, {
        childList: true, subtree: true
    });

    checkForContentToHide();
})();