ADblocker4DeepinBBS

block AD-like Users in DeepinBBS

目前為 2024-06-26 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ADblocker4DeepinBBS
// @namespace    http://tampermonkey.net/
// @version      2024-06-26
// @description  block AD-like Users in DeepinBBS
// @author       fallingstar10
// @match        https://bbs.deepin.org/post/*
// @match        https://bbs.deepin.org/zh/post/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=deepin.org
// @license      MIT
// @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(); // 首次隐藏帖子
    });
})();