NGA Filter

troll must die

当前为 2019-08-31 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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
        });
    });
})();