Greasy Fork

Greasy Fork is available in English.

Bangumi 排行榜增强:隐藏已收藏并按日期区间筛选

隐藏已收藏条目,并按日期区间筛选作品。

目前为 2024-12-27 提交的版本,查看 最新版本

// ==UserScript==
// @name         Bangumi 排行榜增强:隐藏已收藏并按日期区间筛选
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  隐藏已收藏条目,并按日期区间筛选作品。
// @author       KunimiSaya
// @match        https://bgm.tv/*/browser*
// @match        https://bangumi.tv/*/browser*
// @match        https://chii.in/*/browser*
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    let isHidingEnabled = true;
    let startDate = null;
    let endDate = null;

    function extractDate(infoText) {
        const datePatterns = [
            /(\d{4})年(\d{1,2})月/,
            /(\d{4})-(\d{2})-(\d{2})/,
            /(\d{4})-(\d{2})/,
            /(\d{4})/,
        ];

        for (const pattern of datePatterns) {
            const match = infoText.match(pattern);
            if (match) {
                const year = parseInt(match[1], 10);
                const month = match[2] ? parseInt(match[2], 10) : 1;
                return new Date(year, month - 1);
            }
        }
        return null;
    }

    function filterByDate(items) {
        items.forEach(item => {
            const info = item.querySelector('.info');
            if (info) {
                const date = extractDate(info.textContent);
                const isOutOfDateRange =
                    (startDate && date < startDate) || (endDate && date > endDate);
                if (isOutOfDateRange) {
                    item.style.display = 'none';
                }
            }
        });
    }

    function filterCollectedItems(items) {
        items.forEach(item => {
            const isCollected = item.querySelector('.collectModify');
            if (isHidingEnabled && isCollected) {
                item.style.display = 'none';
            } else if (!isHidingEnabled || !isCollected) {
                item.style.display = '';
            }
        });
    }

    function applyFilters() {
        const items = document.querySelectorAll('.item');
        items.forEach(item => (item.style.display = ''));
        filterCollectedItems(items);
        filterByDate(items);
    }

    function addControls() {
        const sideInner = document.querySelector('.sideInner');
        if (!sideInner || document.getElementById('filterControls')) return;

        const controls = document.createElement('div');
        controls.id = 'filterControls';
        controls.style.marginTop = '20px';

        const title = document.createElement('h2');
        title.className = 'subtitle';
        title.textContent = '筛选工具';
        controls.appendChild(title);

        const toggleButton = document.createElement('a');
        toggleButton.className = 'chiiBtn';
        toggleButton.href = 'javascript:void(0);';
        toggleButton.textContent = '显示/隐藏已收藏条目';
        toggleButton.onclick = () => {
            isHidingEnabled = !isHidingEnabled;
            applyFilters();
        };
        controls.appendChild(toggleButton);

        const dateControls = document.createElement('div');
        dateControls.style.marginTop = '10px';
        dateControls.style.display = 'flex';
        dateControls.style.flexWrap = 'wrap';
        dateControls.style.gap = '5px';

        const startDateInput = document.createElement('input');
        startDateInput.type = 'month';
        startDateInput.placeholder = '起始日期';
        startDateInput.style.width = '120px';

        const endDateInput = document.createElement('input');
        endDateInput.type = 'month';
        endDateInput.placeholder = '结束日期';
        endDateInput.style.width = '120px';

        const filterButton = document.createElement('a');
        filterButton.className = 'chiiBtn';
        filterButton.href = 'javascript:void(0);';
        filterButton.textContent = '按时间区间筛选';
        filterButton.onclick = () => {
            startDate = startDateInput.value ? new Date(startDateInput.value) : null;
            endDate = endDateInput.value ? new Date(endDateInput.value) : null;
            applyFilters();
        };

        dateControls.appendChild(startDateInput);
        dateControls.appendChild(endDateInput);
        dateControls.appendChild(filterButton);

        controls.appendChild(dateControls);
        sideInner.appendChild(controls);
    }

    function observePageChanges() {
        const observer = new MutationObserver(() => {
            applyFilters();
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

    function init() {
        addControls();
        applyFilters();
        observePageChanges();
    }

    init();
})();