Bilibili Live Stream Filter

Live stream filtering with real-time counter

目前为 2025-03-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         Bilibili Live Stream Filter
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Live stream filtering with real-time counter
// @author       Gavin Hon
// @match        https://live.bilibili.com/*
// @grant        none
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const LIVE_FILTER_SELECTOR = '.face.living';
    const TARGET_CONTAINER_SELECTOR = '.single';
    const NAME_SELECTOR = '.name';
    const BLOCKED_NAME = '花芽荔枝';
    const COUNTER_SELECTOR = '.mount';
    const COUNTER_PREFIX = ' (Streaming: ';

    let liveCount = 0;
    let counterElement = null;

    function updateCounter() {
        if (!counterElement) {
            counterElement = document.querySelector(COUNTER_SELECTOR);
            if (!counterElement) return;
        }

        // Remove existing counter
        const currentText = counterElement.textContent;
        const regex = /(.*?)(\s*\(Streaming:\s*\d+\))/;
        const cleanText = currentText.replace(regex, '$1');

        // Update with new count
        counterElement.textContent = `${cleanText}${COUNTER_PREFIX}${liveCount})`;
    }

    function filterLiveStreamers() {
        liveCount = 0; // Reset counter

        document.querySelectorAll(TARGET_CONTAINER_SELECTOR).forEach(container => {
            // Name filtering
            const nameElement = container.querySelector(NAME_SELECTOR);
            if (nameElement?.textContent.includes(BLOCKED_NAME)) {
                container.remove();
                return;
            }

            // Live status check
            const isLive = container.querySelector(LIVE_FILTER_SELECTOR);
            if (!isLive) {
                container.remove();
            } else {
                liveCount++; // Count valid live streamers
            }
        });

        updateCounter();
    }

    // Initial execution
    filterLiveStreamers();

    // Dynamic content handling
    new MutationObserver(mutations => {
        filterLiveStreamers();
    }).observe(document.body, {
        subtree: true,
        childList: true
    });

    // SPA navigation handling
    window.addEventListener('popstate', filterLiveStreamers);
    window.addEventListener('pushstate', filterLiveStreamers);
})();

QingJ © 2025

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