NGA Filter

troll must die

Verze ze dne 01. 09. 2019. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name      NGA Filter
// @namespace https://greasyfork.org/users/263018
// @version    0.3
// @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
// ==/UserScript==

(function () {
    'use strict';

    const key = 'troll';

    const keyword = [
        '拼多多'
    ];

    let trollMap = (function () {
        try {
            return JSON.parse(localStorage.getItem(key));
        } catch (e) {
            localStorage.setItem(key, '{}');
        }
        return {};
    })();

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

    const isMatch = function (text) {
        for (var i in keyword) {
            if (text.search(keyword[i]) >= 0) {
                return true;
            }
        }
        return false;
    };

    const toggleTroll = function (uid) {
        uid = ~~uid;
        if (uid) {
            if (trollMap[uid]) {
                delete trollMap[uid];
            } else {
                trollMap[uid] = true;
            }
            localStorage.setItem(key, JSON.stringify(trollMap));
        }
    };

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

    const getLink = function (e) {
        let ele = e.getElementsByClassName('topic')[0];
        ele.hide = function () {
            ele.style.textDecoration = 'line-through';
        }
        return ele;
    };

    const getToggleButton = function (e) {
        let ele = e.getElementsByClassName('author')[0].nextElementSibling;
        if (ele.nextElementSibling) {
            ele = ele.nextElementSibling;
        }
        let uid = ~~ele.text;
        if (uid) {
            if (isTroll(uid)) {
                ele.style.background = '#CB4042';
            } else {
                ele.style.background = '#AAA';
            }
            return ele;
        }
        else {
            ele.onclick = null;
        }
    };

    const getAvatar = function (e) {
        let ele = e.getElementsByClassName('avatar')[0] || { style: { display: '' } };
        ele.show = function () {
            ele.style.display = '';
        }
        ele.hide = function () {
            ele.style.display = 'none';
        }
        return ele;
    };

    const getContent = function (e) {
        let uid = getUID(e);
        let name = '$troll_' + uid;
        let ele = e.getElementsByClassName('postcontent')[0] || { innerHTML: '' };
        ele.content = ele.content || ele.innerHTML;
        ele.show = function () {
            ele.innerHTML = ele.content;
        }
        ele.hide = function () {
            ele.innerHTML =
                '<div class="lessernuke" style="background: #81C7D4; border-color: #66BAB7;">' +
                '<span class="crimson">Troll must die.</span> ' +
                '<a href="javascript:void(0)" onclick="var x = document.getElementsByName(&quot;' + name + '&quot;);for(var i=0;i<x.length;i++){x[i].style.display=&quot;&quot;}">点击查看</a>' +
                '<div style="display:none" name="' + name + '">' + ele.content + '</div>' +
                '</div>';
        }
        return ele;
    };

    const observerElements = [
        (function () {
            let container = document.getElementById('topicrows');
            let func = function (e) {
                if (e.tagName == 'SCRIPT') return;
                let uid = getUID(e);
                let link = getLink(e);
                if (isTroll(uid) ||
                    isMatch(link.innerHTML)) {
                    link.hide();
                }
            }
            return [
                container,
                func
            ]
        })(),
        (function () {
            let container = document.getElementById('m_posts_c');
            let func = function (e) {
                if (e.tagName == 'SCRIPT') return;
                let uid = getUID(e);
                let toggle = getToggleButton(e);
                if (toggle) {
                    toggle.onclick = function () {
                        toggleTroll(uid);
                        container.refilter();
                    };
                } else {
                    return;
                }
                let avatar = getAvatar(e);
                let content = getContent(e);
                if (isTroll(uid)) {
                    avatar.hide();
                    content.hide();
                }
                else {
                    avatar.show();
                    content.show();
                }
            }
            return [
                container,
                func
            ]
        })()
    ];

    [].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
        });
    });
})();