[ Content contribution] ADblocker4DeepinBBS v0.02更新
Tofloor
poster avatar
fallingstar-ten
deepin
2024-06-25 22:19
Author

书接上回

目前ADblocker4DeepinBBS v0.02实现了通过用户固定ID来获取用户的回帖情况。

Github代码:https://github.com/rainoffallingstar/ADblocker4DeepinBBS

Greasyfork地址:https://greasyfork.org/en/scripts/498936-adblocker4deepinbbs

核心的实现来自@DebuggerX

可以从页面的 ng-state脚本里获取

已知bug:当回帖量过多时,可能需要多次刷新网页才能完成屏蔽。

欢迎大家指出错误及提出建议~

// ==UserScript==
// @name         ADblocker4DeepinBBS
// @namespace    http://tampermonkey.net/
// @version      2024-06-25
// @description  block AD-like Users in DeepinBBS
// @author       fallingstar10
// @match        https://bbs.deepin.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=deepin.org
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const userIds = [312791,312017,286301,312105,19468,312784,19468,260299]; // 根据需要添加用户ID
    let blockedPostIds = [];

    function fetchUserPosts(userid) {
        const ngState = JSON.parse(document.querySelector('#ng-state').innerText);
        const userPosts = ngState['post-detail'].posts.data.filter((ele) => ele.post_user_id === userid);
        const postIds = userPosts.map(post => post.id);
        const formattedPostIds = postIds.map(id => `post_${id}`);
        blockedPostIds.push(...new Set([...blockedPostIds, ...formattedPostIds]));
        formattedPostIds.forEach(hidePosts); // 隐藏当前用户的所有帖子
        return formattedPostIds;
    }

    function hidePosts() {
        blockedPostIds.forEach(postId => {
            let postContainer = document.getElementById(postId);
            if (postContainer) {
                postContainer.style.display = 'none';
            }
        });
    }

    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.type === 'childList' && mutation.addedNodes.length) {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE && node.matches('div.post_pc')) {
                        hidePosts(); // 重新检查所有帖子
                    }
                });
            }
        });
    });

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

    // 屏蔽执行
    window.addEventListener('load', () => {
        userIds.forEach(fetchUserPosts); // 预先获取所有应被屏蔽的帖子ID
        hidePosts(); // 隐藏帖子
    });
})();

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

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

Reply Favorite View the author
All Replies
晚秋(lateautumn)
Moderator
2024-06-25 22:22
#1

能编程的都是高手。like

Reply View the author
阿尼樱奈奈
Moderator
2024-06-26 06:14
#2
like
Reply View the author
131******66
deepin
2024-06-26 09:06
#3

like

大佬

Reply View the author
fallingstar-ten
deepin
2024-06-26 10:13
#4
It has been deleted!
神末shenmo
deepin
Spark-App
2024-06-27 22:48
#5

改进建议:bbs.deepin.org.cn也要匹配

Reply View the author
fallingstar-ten
deepin
2024-06-28 07:52
#6
神末shenmo

改进建议:bbs.deepin.org.cn也要匹配

这个好办,再加一行match就好,回头更新一下

Reply View the author
神末shenmo
deepin
Spark-App
2024-06-28 09:02
#7
fallingstar-ten

这个好办,再加一行match就好,回头更新一下

如果能在通知也能屏蔽就好了

可以考虑放到gitee或者gh上面允许公开提交贡献,充实广告列表

能提交到uBlock或者ADBlock上游就更好了

Reply View the author