Bangmumi排行榜条目屏蔽器

在页面右上角添加按钮,可开启屏蔽模式或查看已屏蔽条目并恢复,支持本地持久保存和动态控制显示状态。

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

// ==UserScript==
// @name         Bangmumi排行榜条目屏蔽器
// @namespace    https://bgm.tv/dev/app/4220
// @version      1.0
// @description  在页面右上角添加按钮,可开启屏蔽模式或查看已屏蔽条目并恢复,支持本地持久保存和动态控制显示状态。
// @author       forary
// @license      MIT
// @match        https://bgm.tv/*/browser*sort=rank*
// @match        https://bangumi.tv/*/browser*sort=rank*
// @match        https://chii.in/*/browser*sort=rank*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';
    // 按榜单类别设置分组 key
    const category = (function () {
        const path = location.pathname;
        if (path.startsWith('/anime/')) return 'anime';
        if (path.startsWith('/book/')) return 'book';
        if (path.startsWith('/music/')) return 'music';
        if (path.startsWith('/game/')) return 'game';
        if (path.startsWith('/real/')) return 'real';
        return 'common';
    })();
    const STORAGE_KEY = `bangumi_rank_blocked_items_${category}`; // 分组存储

    function getBlockedMap() {
        const raw = localStorage.getItem(STORAGE_KEY);
        return raw ? JSON.parse(raw) : {};
    }

    function setBlockedMap(map) {
        localStorage.setItem(STORAGE_KEY, JSON.stringify(map));
    }

    function addToBlockedMap(id, name) {
        const map = getBlockedMap();
        if (!(id in map)) {
            map[id] = name;
            setBlockedMap(map);
        }
    }

    function removeFromBlockedMap(id) {
        const map = getBlockedMap();
        if (id in map) {
            delete map[id];
            setBlockedMap(map);
        }
    }

    // 隐藏当前页面中的屏蔽条目
    function blockExistingItems() {
        const blockedMap = getBlockedMap();
        const items = document.querySelectorAll('li[id^="item_"]'); // 页面中存在的条目列表
        items.forEach(item => {
            const idMatch = item.id.match(/^item_(\d+)$/); // 提取出数值ID
            if (!idMatch) return; // 失败则跳过
            const id = idMatch[1];
            if (id in blockedMap) {
                item.remove();
            }
        });
    }
    // 显示每个条目的 “屏蔽” 按钮
    function showBlockButtons() {
        const items = document.querySelectorAll('li[id^="item_"]');
        items.forEach(item => {
            const idMatch = item.id.match(/^item_(\d+)$/);
            if (!idMatch) return;
            const id = idMatch[1];

            const btn = document.createElement('button');
            btn.textContent = '×';
            btn.className = 'bangumi-block-btn';
            Object.assign(btn.style, {
                position: 'absolute',
                bottom: '8px',
                right: '10px',
                width: '18px',
                height: '18px',
                display: 'flex',
                alignItems: 'center',
                justifyContent: 'center',
                fontSize: '16px',
                padding: '0',
                background: '#f66',
                color: '#fff',
                border: 'none',
                borderRadius: '4px',
                cursor: 'pointer',
            });
            btn.onclick = () => {
                const nameElem = item.querySelector('.inner h3 a');
                const name = nameElem?.textContent?.trim() || '未知标题';
                addToBlockedMap(id, name);
                item.remove();
            };
            item.style.position = 'relative';
            item.appendChild(btn);
        });
    }
    // 隐藏每个条目的 “屏蔽” 按钮
    function hideBlockButtons() {
        const buttons = document.querySelectorAll('.bangumi-block-btn');
        buttons.forEach(btn => {
            btn.parentElement?.removeChild(btn);
        });
    }

    function createControlPanel() {
        const toggleBtn = document.createElement('button');
        toggleBtn.textContent = '添加屏蔽';
        Object.assign(toggleBtn.style, {
            position: 'absolute',
            right: '90px',
            padding: '8px 8px',
            background: '#F09199',
            color: '#fff',
            border: 'none',
            borderRadius: '12px',
            cursor: 'pointer',
            fontSize: '12px',
            lineHeight: '1',
        });
        toggleBtn.onclick = () => {
            if (toggleBtn.textContent === '添加屏蔽') {
                showBlockButtons();
                toggleBtn.textContent = '退出';
            } else {
                hideBlockButtons();
                toggleBtn.textContent = '添加屏蔽';
            }
        };

        const showListBtn = document.createElement('button');
        showListBtn.textContent = '管理屏蔽';
        Object.assign(showListBtn.style, {
            position: 'absolute',
            right: '20px',
            padding: '8px 8px',
            background: '#F09199',
            color: '#fff',
            border: 'none',
            borderRadius: '12px',
            cursor: 'pointer',
            fontSize: '12px',
            lineHeight: '1',
        });
        showListBtn.onclick = showBlockedList;

        const header = document.querySelector('#header');
        if (header) {
            header.style.position = 'relative';
            header.appendChild(toggleBtn);
            header.appendChild(showListBtn);
        }
    }

    function showBlockedList() {
        const blocked = getBlockedMap();
        const overlay = document.createElement('div');
        Object.assign(overlay.style, {
            position: 'fixed',
            top: 0, left: 0, right: 0, bottom: 0,
            background: 'rgba(0,0,0,0.5)',
            zIndex: 10000,
            display: 'flex',
            justifyContent: 'center',
            alignItems: 'center',
        });

        const modal = document.createElement('div');
        Object.assign(modal.style, {
            background: '#F09199',
            padding: '20px',
            borderRadius: '10px',
            width: '300px',
            maxHeight: '400px',
            overflowY: 'auto',
        });

        const title = document.createElement('h3');
        title.textContent = `已屏蔽${category}条目`; // 提示当前类别
        title.style.marginBottom = '10px';

        const list = document.createElement('ul');
        for (const [id, name] of Object.entries(blocked)) {
            const li = document.createElement('li');
            Object.assign(li.style, {
                display: 'flex',
                alignItems: 'center',
                overflow: 'hidden',
                textOverflow: 'ellipsis',
                whiteSpace: 'nowrap',
                width: '100%',
                marginBottom: '5px',
            });
            // 创建文本节点(会自动省略过长文本)
            const textSpan = document.createElement('span');
            textSpan.textContent = `${id} - ${name}`;
            Object.assign(textSpan.style, {
                overflow: 'hidden',
                textOverflow: 'ellipsis',
                whiteSpace: 'nowrap',
                flexGrow: 1,
            });
            const unhideBtn = document.createElement('button');
            unhideBtn.textContent = '取消屏蔽';
            Object.assign(unhideBtn.style, {
                marginRight: '8px',
                padding: '2px 6px',
                fontSize: '12px',
                background: '#4CAF50',
                color: '#fff',
                border: 'none',
                borderRadius: '4px',
                cursor: 'pointer',
            });
            unhideBtn.onclick = () => {
                removeFromBlockedMap(id);
                li.remove();
            };
            li.appendChild(unhideBtn);
            li.appendChild(textSpan);
            list.appendChild(li);
        }
        
        const closeBtn = document.createElement('button');
        closeBtn.textContent = '关闭';
        Object.assign(closeBtn.style, {
            marginTop: '10px',
            padding: '6px 12px',
            background: '#888',
            color: '#fff',
            border: 'none',
            borderRadius: '4px',
            cursor: 'pointer',
        });
        closeBtn.onclick = () => overlay.remove();

        modal.appendChild(title);
        modal.appendChild(list);
        modal.appendChild(closeBtn);
        overlay.appendChild(modal);
        document.body.appendChild(overlay);
    }

    function init() {
        blockExistingItems();
        createControlPanel();
    }
    window.addEventListener('load', init);
    const observer = new MutationObserver(() => {
        blockExistingItems();
    });
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();

QingJ © 2025

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