油猴脚本:屏蔽特定用户评论

文章摘要
该文章介绍了一个油猴脚本,用于屏蔽特定用户评论。脚本根据设定的UID列表,隐藏相关用户的评论内容,并提供一个“显示此条评论”的按钮,点击后可还原被屏蔽的评论。脚本在页面加载完成后运行,并通过MutationObserver持续监控页面内容变化以执行屏蔽操作。作者表示官方虽已支持屏蔽帖子,但未包括评论,因而开发此脚本作为补充。
— 文章部分摘要由DeepSeek深度思考而成

网站的自定义功能支持屏蔽用户帖子,但评论没有进行屏蔽,懒得去改了,简单弄了个油猴脚本

图片[1]|油猴脚本:屏蔽特定用户评论|不死鸟资源网

脚本内容:

// ==UserScript==
// @name         屏蔽特定用户评论
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  根据设定的UID列表屏蔽特定用户的评论,并提供显示按钮
// @author       大佬论坛
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 设置需要屏蔽的用户UID列表
    const blockedUIDs = ['44', '4884', '1807']; // 支持多个UID

    function blockComments() {
        try {
            console.log('blockComments called'); // 调试信息
            const comments = document.querySelectorAll('.media.post');
            comments.forEach(comment => {
                const uid = comment.getAttribute('data-uid');
                console.log(`Comment UID: ${uid}`); // 调试信息
                if (blockedUIDs.includes(uid)) {
                    console.log(`Blocking comment with UID: ${uid}`); // 调试信息
                    // 获取原始内容
                    const originalContent = comment.cloneNode(true);
                    // 替换内容
                    comment.innerHTML = `
                        <div class="media-body">
                            <div class="d-flex justify-content-between small text-muted">
                                <div>
                                    <span class="username">
                                        <span class="text-muted font-weight-bold" title="(UID: ${uid})">您已屏蔽此用户言论。(<span class="show-comment-btn" style="margin-top: 10px;cursor: pointer;">显示此条评论</span>)</span>
                                    </span>
                                    <br>
                                    <span class="signature-post" style="color: #868e96; font-size: 10.5px;"></span>
                                </div>
                                <div class="text-right text-grey">
                                    <span class="floor-parent">
                                        <span class="floor mr-0">${comment.querySelector('.floor').innerText}</span>楼
                                    </span>
                                </div>
                            </div>


                        </div>
                    `;
                    // 添加显示按钮的点击事件
                    const showButton = comment.querySelector('.show-comment-btn');
                    showButton.addEventListener('click', () => {
                        comment.innerHTML = originalContent.innerHTML;
                    });
                }
            });
        } catch (error) {
            console.error('Error in blockComments:', error);
        }
    }

    // 页面加载完成后执行
    function onDOMContentLoaded() {
        blockComments();
        initMutationObserver();
    }

    // 初始化 MutationObserver
    function initMutationObserver() {
        const observer = new MutationObserver((mutations) => {
            console.log('Mutation observed:', mutations); // 调试信息
            blockComments();
        });

        const targetNode = document.querySelector('.postlist'); // 请根据实际情况修改选择器
        if (targetNode) {
            observer.observe(targetNode, { childList: true, subtree: true });
        } else {
            console.error('Target node .postlist not found'); // 调试信息
        }
    }

    // 注册页面加载事件
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', onDOMContentLoaded);
    } else {
        setTimeout(onDOMContentLoaded, 0);
    }

    // 将 blockComments 函数暴露到全局作用域
    window.blockComments = blockComments;
})();
本站文章部分内容可能来源于网络,仅供大家学习参考,如有侵权,请联系站长📧cutwork@qq.com进行删除处理!
油猴脚本:屏蔽特定用户评论|不死鸟资源网
油猴脚本:屏蔽特定用户评论
此内容为免费阅读,请登录后查看
¥0
限时特惠
¥99
文章采用CC BY-NC-SA 4.0许可协议授权
免费阅读
THE END
点赞12 分享