Iwara ID 黑名单

在 iwara.tv 上根据作者 Profile ID 进行屏蔽 (作品和评论区),并默认屏蔽指定ID。

// ==UserScript==
// @name         Iwara ID 黑名单
// @namespace    http://tampermonkey.net/
// @version      3.2
// @license      MIT
// @description  在 iwara.tv 上根据作者 Profile ID 进行屏蔽 (作品和评论区),并默认屏蔽指定ID。
// @author       Gemini
// @match        https://*.iwara.tv/*
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // --- 使用统一的存储键来保存基于ID的黑名单 ---
    const BLACKLIST_KEY = 'iwara_id_blacklist';

    /**
     * 获取黑名单
     * @returns {Set<string>}
     */
    const getBlacklist = () => {
        // --- 修改点 ---
        // 如果存储中没有值,默认返回一个包含 'blacklane' 的数组字符串
        const defaultValue = '["blacklane"]';
        return new Set(JSON.parse(GM_getValue(BLACKLIST_KEY, defaultValue)));
    };

    /**
     * 保存黑名单
     * @param {Set<string>} blacklistSet
     */
    const saveBlacklist = (blacklistSet) => {
        GM_setValue(BLACKLIST_KEY, JSON.stringify(Array.from(blacklistSet)));
    };


    /**
     * 根据 Profile ID 隐藏作品列表中的项目
     */
    const hideBlacklistedWorks = () => {
        const blacklist = getBlacklist();
        if (blacklist.size === 0) return;

        const works = document.querySelectorAll('.page-videoList__item:not([data-id-checked])');
        for (const work of works) {
            work.dataset.idChecked = 'true';

            const authorLink = work.querySelector('a.username');
            if (authorLink) {
                const href = authorLink.getAttribute('href');
                const profileId = href.split('/profile/')[1];
                if (profileId && blacklist.has(profileId)) {
                    console.log(`[Iwara ID 黑名单] 已隐藏 ID: ${profileId} (作者: ${authorLink.title}) 的作品`);
                    work.style.display = 'none';
                }
            }
        }
    };

    /**
     * 根据 Profile ID 隐藏评论区中的评论
     */
    const hideBlacklistedComments = () => {
        const blacklist = getBlacklist();
        if (blacklist.size === 0) return;

        const comments = document.querySelectorAll('.comment:not([data-id-checked])');
        for (const comment of comments) {
            comment.dataset.idChecked = 'true';

            const authorLink = comment.querySelector('a.username');
            if (authorLink) {
                const href = authorLink.getAttribute('href');
                const profileId = href.split('/profile/')[1];
                if (profileId && blacklist.has(profileId)) {
                    const commentContainer = comment.closest('.col-12');
                    if (commentContainer) {
                         console.log(`[Iwara ID 黑名单] 已隐藏 ID: ${profileId} (作者: ${authorLink.title}) 的评论`);
                         commentContainer.style.display = 'none';
                    }
                }
            }
        }
    };


    /**
     * 在作者主页根据 Profile ID 添加“拉黑ID”按钮
     */
    const addBlockButton = () => {
        if (!window.location.pathname.startsWith('/profile/')) return;

        const container = document.querySelector('.page-profile__header__middle .d-flex.align-items-center');
        if (!container || document.querySelector('#author-id-block-btn')) return;

        const currentProfileId = window.location.pathname.split('/profile/')[1];
        if (!currentProfileId) return;

        const blockButton = document.createElement('div');
        blockButton.id = 'author-id-block-btn';
        Object.assign(blockButton.style, {
            marginLeft: '16px', padding: '4px 10px', border: '1px solid #ccc',
            borderRadius: '5px', cursor: 'pointer', fontSize: '14px',
            fontWeight: 'bold', userSelect: 'none', transition: 'all 0.2s ease'
        });

        const updateButtonState = (isBlocked) => {
            if (isBlocked) {
                blockButton.textContent = `✓ 已拉黑此ID (移除)`;
                blockButton.style.borderColor = '#e91e63';
                blockButton.style.color = '#e91e63';
                blockButton.style.backgroundColor = '#fce4ec';
            } else {
                blockButton.textContent = `🚫 拉黑此ID`;
                blockButton.style.borderColor = '#ccc';
                blockButton.style.color = '#555';
                blockButton.style.backgroundColor = '#f0f0f0';
            }
        };

        let blacklist = getBlacklist();
        updateButtonState(blacklist.has(currentProfileId));

        blockButton.addEventListener('mouseenter', () => { blockButton.style.opacity = '0.8'; });
        blockButton.addEventListener('mouseleave', () => { blockButton.style.opacity = '1'; });

        blockButton.addEventListener('click', (e) => {
            e.stopPropagation();
            let currentBlacklist = getBlacklist();
            if (currentBlacklist.has(currentProfileId)) {
                currentBlacklist.delete(currentProfileId);
                updateButtonState(false);
            } else {
                currentBlacklist.add(currentProfileId);
                updateButtonState(true);
            }
            saveBlacklist(currentBlacklist);
        });

        container.appendChild(blockButton);
    };

    // --- 脚本启动与动态内容监控 ---
    const observer = new MutationObserver(() => {
        hideBlacklistedWorks();
        hideBlacklistedComments();
        addBlockButton();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();