// ==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
// @downloadURL none
// ==/UserScript==
(function () {
'use strict';
// 手動設定不隱藏名單
// 範例:var ForceShowList = ['userId', 'userrrIIIDDD'];
var ForceShowList = [];
// 手動設定隱藏名單
// 範例:var ForceHideList = ['userId', 'userrrIIIDDD'];
var ForceHideList = [];
// 強制顯示優先於強制隱藏,如有重複也沒關係
// 程式開始
// 加入隱藏切換
jQuery('head').append('');
jQuery('.BH-menuE').append('
顯示本頁黑單');
jQuery('.BH-menuE').append('隱藏本頁黑單');
jQuery('body').append('');
//BC頁分開
switch (location.pathname) {
case '/B.php':
filterPost();
break;
case '/C.php':
// 擷取展開按鈕事件:當展開留言按鈕被點擊,執行原生展開留言指令並處理內容
jQuery('body').append('');
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(''); 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_目前沒有任何資料
' ? data.response.split('')[1].replace('
', '').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);
}
});
});
}
})();