// ==UserScript== // @name B站直播间弹幕过滤 (带可视化设置) // @namespace http://tampermonkey.net/ // @version 2.0 // @description 悬浮按钮+可视化界面,自动屏蔽没有粉丝牌、或粉丝牌低于设定等级的弹幕 // @author YourName // @match https://live.bilibili.com/* // @icon https://www.bilibili.com/favicon.ico // @grant GM_addStyle // ==/UserScript== (function() { 'use strict'; // ================= 默认配置与存储 ================= const CONFIG_KEY = 'BiliDanmakuFilterConfig'; let config = { minLevel: 5, blockNoMedal: true, onlyCurrentRoom: false }; // 读取本地保存的配置 try { const saved = localStorage.getItem(CONFIG_KEY); if (saved) { Object.assign(config, JSON.parse(saved)); } } catch (e) { console.error("读取配置失败", e); } function saveConfig() { localStorage.setItem(CONFIG_KEY, JSON.stringify(config)); } // ================= 弹幕过滤逻辑 ================= function processDanmaku(node) { if (!node.classList || (!node.classList.contains('chat-item') && !node.classList.contains('danmaku-item'))) { return; } const medalEl = node.querySelector('.fans-medal-item'); // 1. 无粉丝牌 if (!medalEl) { if (config.blockNoMedal) hideNode(node); return; } // 2. 有粉丝牌,检查等级 const medalText = medalEl.innerText || ""; const levelMatch = medalText.match(/\d+/); if (levelMatch) { const level = parseInt(levelMatch[0], 10); if (level < config.minLevel) { hideNode(node); return; } } // 3. 检查是否为当前直播间的牌子 (灰牌检测) if (config.onlyCurrentRoom) { if (medalEl.classList.contains('is-grey') || medalEl.innerHTML.includes('gray')) { hideNode(node); return; } } } function hideNode(node) { node.style.display = 'none'; } function startObserver() { const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { mutation.addedNodes.forEach((node) => { if (node.nodeType === 1) processDanmaku(node); }); }); }); const timer = setInterval(() => { const chatList = document.querySelector('#chat-history-list'); if (chatList) { clearInterval(timer); observer.observe(chatList, { childList: true, subtree: true }); console.log("[B站弹幕过滤] 监控已启动。"); } }, 1000); } // ================= 注入 CSS 样式 ================= GM_addStyle(` /* 悬浮按钮 */ #bili-filter-fab { position: fixed; right: 20px; bottom: 150px; width: 44px; height: 44px; background-color: #fb7299; color: white; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: bold; box-shadow: 0 4px 12px rgba(251, 114, 153, 0.4); cursor: grab; z-index: 999999; user-select: none; transition: transform 0.2s; } #bili-filter-fab:active { cursor: grabbing; transform: scale(0.95); } /* 设置面板 */ #bili-filter-panel { position: fixed; right: 80px; bottom: 150px; width: 240px; background-color: #fff; border-radius: 8px; box-shadow: 0 4px 20px rgba(0,0,0,0.15); padding: 16px; z-index: 999999; display: none; /* 默认隐藏 */ font-family: "Microsoft YaHei", sans-serif; color: #333; border: 1px solid #eee; } #bili-filter-panel h3 { margin: 0 0 12px 0; font-size: 16px; color: #fb7299; display: flex; justify-content: space-between; align-items: center; } #bili-filter-panel .close-btn { cursor: pointer; color: #999; font-weight: normal; } #bili-filter-panel .close-btn:hover { color: #333; } #bili-filter-panel .setting-item { margin-bottom: 12px; font-size: 13px; display: flex; align-items: center; } #bili-filter-panel input[type="number"] { width: 50px; margin-left: 8px; padding: 2px 4px; border: 1px solid #ccc; border-radius: 4px; outline: none; } #bili-filter-panel input[type="checkbox"] { margin-right: 6px; cursor: pointer; } /* 黑夜模式兼容 */ html.dark #bili-filter-panel { background-color: #222; color: #ddd; border-color: #444; } html.dark #bili-filter-panel .close-btn { color: #aaa; } html.dark #bili-filter-panel input[type="number"] { background: #333; color: #fff; border-color: #555;} `); // ================= 构建 UI ================= function initUI() { // 1. 创建悬浮按钮 const fab = document.createElement('div'); fab.id = 'bili-filter-fab'; fab.innerText = '过滤'; document.body.appendChild(fab); // 2. 创建面板 const panel = document.createElement('div'); panel.id = 'bili-filter-panel'; panel.innerHTML = `