Greasy Fork

Greasy Fork is available in English.

巴哈姆特黑名單隱藏文章留言

在巴哈姆特的B、C頁將黑名單隱藏文章和留言,在頂端列可以打開該頁被隱藏的名單(一次性)

当前为 2018-04-20 提交的版本,查看 最新版本

// ==UserScript==
// @name         巴哈姆特黑名單隱藏文章留言
// @namespace    https://home.gamer.com.tw/moontai0724
// @version      1.0
// @description  在巴哈姆特的B、C頁將黑名單隱藏文章和留言,在頂端列可以打開該頁被隱藏的名單(一次性)
// @author       moontai0724
// @match        https://forum.gamer.com.tw/*
// @grant        GM_xmlhttpRequest
// @connect      home.gamer.com.tw
// @supportURL   https://home.gamer.com.tw/moontai0724
// ==/UserScript==

(function () {
    'use strict';
    // 手動設定不隱藏名單
    // 範例:var ForceShowList = ['userId', 'userrrIIIDDD'];
    var ForceShowList = [];

    // 手動設定隱藏名單
    // 範例:var ForceHideList = ['userId', 'userrrIIIDDD'];
    var ForceHideList = [];

    // 強制顯示優先於強制隱藏,如有重複也沒關係


    // 程式開始
    // 加入隱藏切換
    jQuery('head').append('<style type="text/css" id="BlockListHideCSS">.BlockListHide { display: none !important; }</style>');
    jQuery('.BH-menuE').append('<li><a id="ShowBlockList" href="javascript:;" style="display: block;" onclick="BlockListDisplay(true);">顯示本頁黑單</a></li>');
    jQuery('.BH-menuE').append('<li><a id="HideBlockList" href="javascript:;" style="display: none;" onclick="BlockListDisplay(false);">隱藏本頁黑單</a></li>');
    jQuery('body').append('<script type="text/javascript">' + function BlockListDisplay(status) {
        document.getElementById('HideBlockList').style.display = status ? 'block' : 'none';
        document.getElementById('ShowBlockList').style.display = status ? 'none' : 'block';
        status ? document.getElementById('BlockListHideCSS').innerHTML = document.getElementById('BlockListHideCSS').innerHTML.replace('BlockListHide', 'noBlockListHide') : document.getElementById('BlockListHideCSS').innerHTML = document.getElementById('BlockListHideCSS').innerHTML.replace('noBlockListHide', 'BlockListHide');
    } + '</script>');

    //BC頁分開
    switch (location.pathname) {
        case '/B.php':
            filterPost();
            break;
        case '/C.php':
            // 擷取展開按鈕事件:當展開留言按鈕被點擊,執行原生展開留言指令並處理內容
            jQuery('body').append('<button id="extendCommentListener" style="display: none;"></button>');
            jQuery('.more-reply').each((index, element) => element.setAttribute("onclick", element.getAttribute("onclick") + " [document.getElementById('extendCommentListener').dataset.bsn, document.getElementById('extendCommentListener').dataset.postid] = [" + element.getAttribute('onclick').replace('extendComment(', '').replace(');', '').split(', ') + "]; jQuery('#Commendlist_" + element.getAttribute('onclick').replace('extendComment(', '').replace(');', '').split(', ')[1] + "').append('<div id=\"extendCommentAreaListener\"></div>'); document.getElementById('extendCommentListener').click();"));
            // 當按鈕點擊就執行
            document.getElementById('extendCommentListener').onclick = function () {
                let [bsn, postid] = [document.getElementById('extendCommentListener').dataset.bsn, document.getElementById('extendCommentListener').dataset.postid], times = 0, ms = 0;
                setTimeout(function restartFliter(ms) {
                    setTimeout(function () {
                        if (!document.getElementById('extendCommentAreaListener')) {
                            jQuery.get("/ajax/moreCommend.php?bsn=" + bsn + "&snB=" + postid + "&sreturnHtml=0", data => getBlockList().then(blocklist => {
                                for (let key in data) {
                                    if (blocklist.includes(data[key].userid)) jQuery('#Commendcontent_' + data[key].sn).addClass('BlockListHide');
                                }
                            }));
                        } else if (times++ < 50) restartFliter(100);
                    }, ms);
                });
            };
            filterPost();
            filterComment();
            break;
    }

    function filterPost() {
        getBlockList().then(blocklist => {
            console.log(blocklist);
            let postAuther = [];
            // get a list of post authers and element id
            switch (location.pathname) {
                case '/B.php':
                    jQuery('.b-list__count__user>a').each((index, value) => postAuther[postAuther.length] = { userId: value.innerHTML, postId: value.parentNode.parentNode.parentNode.getElementsByTagName('a')[0].getAttribute('name') });
                    postAuther.forEach(element => { if (blocklist.includes(element.userId)) jQuery('[name="' + element.postId + '"]').parent().parent().addClass('BlockListHide'); });
                    break;
                case '/C.php':
                    jQuery('.c-post__header__author>.userid').each((index, value) => postAuther[postAuther.length] = { userId: value.innerHTML, postId: value.parentNode.parentNode.parentNode.parentNode.id });
                    postAuther.forEach(element => { if (blocklist.includes(element.userId)) jQuery('#' + element.postId).addClass('BlockListHide'); });
                    break;
            }
        });
    }

    function filterComment(postid) {
        getBlockList().then(blocklist => {
            let commentAuther = [];
            // get a list of comment authers and element id
            jQuery('.reply-content__user').each((index, value) => commentAuther[commentAuther.length] = { userId: value.href.replace('https://home.gamer.com.tw/', ''), commentId: value.parentNode.parentNode.parentNode.id });
            commentAuther.forEach(element => { if (blocklist.includes(element.userId)) jQuery('#' + element.commentId).addClass('BlockListHide'); });
        });
    }

    function getBlockList(forceReload) {
        return new Promise(resolve => {
            if (localStorage.getItem('BHBlockList') && !forceReload) {
                let BHBlockList = JSON.parse(localStorage.getItem('BHBlockList')), today = new Date((new Date()).getFullYear(), (new Date()).getMonth(), (new Date()).getDate(), 0, 0, 0, 0);
                today.getTime() < BHBlockList.time && BHBlockList.time < today.getTime() + 86400000 ? resolve(BHBlockList.blocklist) : getBlockList(true).then(data => resolve(data));
            } else GM_xmlhttpRequest({
                method: "GET",
                url: "https://home.gamer.com.tw/ajax/friend_getData.php?here=0",
                onload: data => {
                    let blocklist = data.response != 'MSG_<p class="MSG-nobox1A">目前沒有任何資料</p>' ? data.response.split('<div id="BMW_nlist" style="display:none;">')[1].replace('</div>', '').split(',') : [];
                    let removeNum = [];
                    ForceHideList.forEach(value => blocklist.includes(value.replace(/\s/g, '')) ? void (0) : blocklist[blocklist.length] = value.replace(/\s/g, ''));
                    blocklist.forEach((value, index) => ForceShowList.includes(value) ? removeNum[removeNum.length] = index : void (0));
                    for (let i = removeNum.length; i > 0; i--) blocklist.splice(removeNum[i], 1);
                    localStorage.setItem('BHBlockList', JSON.stringify({ time: new Date().getTime(), blocklist: blocklist }));
                    resolve(blocklist);
                }
            });
        });
    }
})();