Greasy Fork

NGA Filter

troll must die

目前为 2019-08-31 提交的版本。查看 最新版本

// ==UserScript==
// @name      NGA Filter
// @version    0.1
// @author     snyssss
// @description troll must die
// @match        *bbs.nga.cn/thread.php?fid=*
// @match        *bbs.nga.cn/read.php?tid=*
// @match        *ngabbs.com/thread.php?fid=*
// @match        *ngabbs.com/read.php?tid=*
// @grant        none
// @noframes。

// todo 屏蔽模式;直接在页面里添加屏蔽人

// v0.1 简单做了屏蔽逻辑,根据trollArray里的用户ID删除相关帖子或者楼层。
// @namespace https://greasyfork.org/users/263018
// ==/UserScript==


(function () {
    'use strict';

    const trollArray = [35159831];
    const trollMap = Object.assign({}, ...trollArray.map(item => ({ [item]: true })));

    const getUID = function(e) {
        let author = e.getElementsByClassName('author')[0];
        if (author) {
            return author.search.match(/uid=(\S*)/)[1];
        }
    };

    const isTroll = function (uid) {
        uid = ~~uid;
        if (uid) {
            return trollMap[uid];
        }
        return false;
    };

    const observerElements = [
        [
            document.getElementById('topicrows'),
            function (e) {
                let uid = getUID(e);
                if (isTroll(uid)) {
                    e.remove();
                }
            }
        ],
        [
            document.getElementById('m_posts_c'),
            function (e) {
                let uid = getUID(e);
                if (isTroll(uid)) {
                    e.remove();
                }
            }
        ]
    ];

    [].slice.call(observerElements).forEach(function (e) {
        if (!e[0]) return;

        e[0].refilter = function() {
            [].slice.call(e[0].children).forEach(function (c) {
                e[1](c);
            });
        }

        e[0].refilter();

        let observer = new MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                if (mutation.addedNodes.length) {
                    e[1](mutation.addedNodes[0]);
                }
            });
        });

        observer.observe(e[0], {
            childList: true
        });
    });
})();