[ Content contribution] 一个论坛评论区屏蔽名单的油猴脚本
Tofloor
poster avatar
fallingstar-ten
deepin
2024-06-24 11:16
Author

书接上回,我们在屏蔽/黑名单功能上的争执没有得到解决,然后我当时使用了爬虫+blog作为解决方案,这样解决了在首页里面进行用户过滤,但是没有在帖子本身内及评论区做工作。

现在,我们提供了一个方便快捷的油猴脚本,当你在浏览器中访问论坛时,你可以根据DIY黑名单对某些用户的回帖进行屏蔽。初步的代码如下,目前测试相当丝滑,欢迎大佬指出错误并添加更多功能/给予建议:

  • tips :用户名单在const blockedUserIds = ['user123', 'user456'] 字段,需要输入用户昵称进行。
// ==UserScript==
// @name         ADblocker4DeepinBBS
// @namespace    http://tampermonkey.net/
// @version      2024-06-24
// @description  block AD-like Users in DeepinBBS
// @author       fallingstar10
// @match        https://bbs.deepin.org/post/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=deepin.org
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 要屏蔽的用户ID列表
    const blockedUserIds = ['user123', 'user456']; // 根据需要添加用户ID

    // 定位到每个回帖的容器
    const postContainerSelector = 'div.post_pc';

    // 定位到用户ID的span
    const userNameSelector = 'span.post_name';

    // 检查并隐藏帖子内容
    function hidePosts() {
        const postContainers = document.querySelectorAll(postContainerSelector);
        postContainers.forEach(postContainer => {
            // 找到用户ID的span元素
            const userNameSpan = postContainer.querySelector(userNameSelector);
            if (userNameSpan) {
                const userId = userNameSpan.textContent.trim();
                if (blockedUserIds.includes(userId)) {
                    // 隐藏整个回帖容器
                    postContainer.style.display = 'none';
                }
            }
        });
    }

    // 监听DOM变化
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'childList') {
                hidePosts();
            }
        });
    });

    // 配置observer,使其知道需要观察哪个节点,以及需要观察哪些变化
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 页面加载完成后执行一次
    document.addEventListener('DOMContentLoaded', hidePosts);
})();

--------------------更新

本代码将在dpbbs-rss仓库存储,主要每日爬取黑名单内ID及对应的昵称,然后自动生成js文件更新到GreasyFork。

GitHub地址:https://github.com/rainoffallingstar/dpbbs-rss/blob/master/R/ADblocker4DeepinBBSv1.js

Greasyfork地址:https://greasyfork.org/en/scripts/498942-adblocker4deepinbbsv1

------------------更新

非常简单的加入了首页信息流的屏蔽功能(按发帖人)


可能换一个地方玩linux会更幸福吧。

什么时候有空了再回来看看吧

Reply Favorite View the author
All Replies
1 / 2
To page
蔡EEPIN
deepin
2024-06-24 11:19
#1

agree

Reply View the author
hanzn-zzx
deepin
2024-06-24 11:21
#2

like

Reply View the author
deepin小助手
Super Moderator
OM
2024-06-24 11:22
#3

好牛

学到了,感谢分享

Reply View the author
131******66
deepin
2024-06-24 11:25
#4

向大佬致敬

Reply View the author
阿尼樱奈奈
Moderator
2024-06-24 11:26
#5

牛啊like

Reply View the author
berners
deepin
Backbone of ecological co-construction group
2024-06-24 12:02
#6

是id号还是昵称😂 他们一换昵称岂不是失效了

Reply View the author
fallingstar-ten
deepin
2024-06-24 12:10
#7
回答楼上,应该是可以ID的,但是今天突然忘记ID是哪个选择器了,貌似贴子网页不会带ID,只有昵称来着,就还没解决这个问题
Reply View the author
f@deepin
deepin
2024-06-24 12:25
#8

like

Reply View the author
DebuggerX
deepin
2024-06-24 12:48
#9
fallingstar-ten
回答楼上,应该是可以ID的,但是今天突然忘记ID是哪个选择器了,貌似贴子网页不会带ID,只有昵称来着,就还没解决这个问题

可以从页面的 ng-state脚本里获取,比如我的id是16934,用下面的代码就可以找出所有我发的post:

JSON.parse(document.querySelector('#ng-state').innerText)['post-detail'].posts.data.filter((ele) => ele.post_user_id === 16934)

image.png

拿到的数据里评论id、用户名、昵称,啥都有~

曾经我也写脚本屏蔽,后来干IT久了也算是见多识广,心性磨练的差不多了,现在基本都能做到视而不见、一笑而过了😂

Reply View the author
fallingstar-ten
deepin
2024-06-24 13:01
#10
厉害了,原来还能这样操作
Reply View the author
fallingstar-ten
deepin
2024-06-24 13:04
#11
我其实脾气挺好的,这次属于是跟某绿一天杠到晚杠生气了
Reply View the author
神末shenmo
deepin
Spark-App
2024-06-24 13:23
#12
fallingstar-ten
回答楼上,应该是可以ID的,但是今天突然忘记ID是哪个选择器了,貌似贴子网页不会带ID,只有昵称来着,就还没解决这个问题

太需要了

Reply View the author
出售星辰之书的书商
Moderator
2024-06-24 13:48
#13
刚需 感谢楼主 applaud
Reply View the author
流浪的加菲
deepin
2024-06-24 13:49
#14

技术活啊like

果然大佬们泡的论坛和我们普通小白是不一样的,哈哈

Reply View the author
👿Deepin大护法👿
deepin
2024-06-24 14:02
#15

换个地方玩 linux,可能幸福指数会提升

Reply View the author
zijinyise
deepin
2024-06-24 14:49
#16

按照DebuggerX大佬的方法来获取查询到id后,可直接删除div.post_pc[id="post_"+id]

post_id的id就是

JSON.parse(document.querySelector('#ng-state').innerText)['post-detail'].posts.data.filter((ele) => ele.id === 1612293)

image.png

Reply View the author
fallingstar-ten
deepin
2024-06-24 16:06
#17
agree
Reply View the author
fallingstar-ten
deepin
2024-06-25 16:16
#18
👿Deepin大护法👿

换个地方玩 linux,可能幸福指数会提升

是的,忙完这阵是时候考虑一下了

Reply View the author
乾豫恒益
deepin
2024-06-26 16:52
#19

yeah 实用啊。。。

Reply View the author
fallingstar-ten
deepin
2024-06-26 17:03
#20
乾豫恒益

yeah 实用啊。。。

哈哈,可以去看看github的版本,那个连首页的都能直接过滤,就是这个版本昵称得自己设定,哪天他们换名字了就会又跑出来

Reply View the author
1 / 2
To page