You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用uBlock隐藏包含特定用户名子元素的父LI元素?

用uBlock隐藏特定作者的论坛帖子

背景

论坛站长使用Akismet替代人工审核后不再管理社区,Akismet能有效拦截机器人垃圾帖,但有个别人工垃圾发送者漏网,在论坛发布无关产品链接,给社区成员带来困扰。

论坛DOM结构

论坛帖子采用UL+LI结构,每个LI对应一个帖子,包含作者信息的DOM结构示例如下:

<div>
  <ul>
    <li id="[random-id1]">Good Post</li>
    <li id="[random-id2]">
      <div>
        <a class="title" href="spam-post.html">Spam Post Title</a>
      </div>
      <div id="bad-post-meta">
        <a href="member.php/12345-naughtyspammer" class="username" title="Started by NaughtySpammer on Yesterday 05:41 AM">NaughtySpammer</a>
      </div>
    </li>
    <li id="[random-id3]">Good Post</li>
  </ul>
</div>

需求

希望通过uBlock遍历a.username元素,识别黑名单中的垃圾发送者用户名,并隐藏对应的LI父元素(实现display:none;效果)。此前仅用uBlock处理过简单元素移除,对这种复杂场景不熟悉。

遇到的问题

Adblock Plus的hide-if-contains片段可实现类似功能,但该功能属于EasyList,uBlock不支持;尝试使用forumsite.com#$#hide-if-contains naughtyspammer li语法时,uBlock提示语法无效。


解决方案1:使用CSS :has()伪类(推荐)

uBlock支持现代CSS的:has()伪类,可直接通过选择器定位包含特定用户名的LI元素,添加以下规则到uBlock的「我的规则」中:

forumsite.com##li:has(a.username:contains("NaughtySpammer")) { display: none !important; }
  • 将forumsite.com替换为你的论坛实际域名
  • 如需屏蔽多个用户,可扩展选择器:
forumsite.com##li:has(a.username:contains("NaughtySpammer"), a.username:contains("AnotherSpammer")) { display: none !important; }

解决方案2:使用自定义脚本(兼容旧浏览器)

如果浏览器不支持:has()伪类,可使用uBlock的自定义脚本功能:

  1. 打开uBlock仪表盘,进入「我的规则」标签页
  2. 添加以下规则(单用户):
forumsite.com#%#//scriptlet('hide-element', 'li:has(a.username:contains("NaughtySpammer"))')

或者添加更灵活的多用户脚本:

forumsite.com#%#(function() {
    // 替换为你的黑名单用户名
    const blacklist = ["NaughtySpammer", "AnotherSpammer"];
    
    // 处理初始加载的内容
    const hideSpamPosts = () => {
        document.querySelectorAll('a.username').forEach(link => {
            const username = link.textContent.trim();
            if (blacklist.includes(username)) {
                const targetLi = link.closest('li');
                if (targetLi) targetLi.style.display = 'none';
            }
        });
    };

    // 页面加载完成后执行
    document.addEventListener('DOMContentLoaded', hideSpamPosts);

    // 监听动态加载的帖子(论坛可能滚动加载新内容)
    const observer = new MutationObserver((mutations) => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === Node.ELEMENT_NODE) {
                    node.querySelectorAll('a.username').forEach(link => {
                        const username = link.textContent.trim();
                        if (blacklist.includes(username)) {
                            const targetLi = link.closest('li');
                            if (targetLi) targetLi.style.display = 'none';
                        }
                    });
                }
            });
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();
  • 将forumsite.com替换为论坛实际域名
  • 修改blacklist数组添加需要屏蔽的用户名

内容的提问来源于stack exchange,提问作者Monk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 16:05:19