// ==UserScript==
// @name 巴哈姆特黑名單、關鍵字過濾文章留言
// @namespace https://home.gamer.com.tw/moontai0724
// @version 2.1.1
// @description 在巴哈姆特的B、C頁將黑名單、關鍵字過濾文章留言,在頂端列可以開關過濾器(一次性)
// @author moontai0724
// @match https://forum.gamer.com.tw/*
// @grant GM_xmlhttpRequest
// @connect home.gamer.com.tw
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// @supportURL https://home.gamer.com.tw/moontai0724
// @downloadURL none
// ==/UserScript==
(function () {
'use strict';
// 以下參數注意事項:
// 1. 不分大小寫。
// 2. 可換行
// 3. 請以逗號分隔
// 統一範例:
// 單行範例:var ForceHideList = ['userId', 'TestId'];
// 單行範例:var blockKeywords = ['推', '私信'];
// 換行範例:
// var postBlockKeywords = ['推', '私信',
// '野格炸彈', '斯斯', 'BANG',
// '我的最愛'];
// 手動設定ID不隱藏名單,強制顯示優先於強制隱藏,因此如有重複也沒關係。
var ForceShowList = [];
// 手動設定ID隱藏名單,強制顯示優先於強制隱藏,因此如有重複也沒關係。
var ForceHideList = [];
// 隱藏關鍵字,如果加在這邊代表文章跟留言皆會過濾。
var blockKeywords = [];
// 文章隱藏關鍵字,如果加在這邊代表只有文章內文包含才會被過濾。
var postBlockKeywords = [];
// 留言隱藏關鍵字,如果加在這邊代表只有留言內文包含才會被過濾。
var commentBlockKeywords = [];
// 以下參數請務必完全正確,大小寫相同,true 或 false。
// 文章是否過濾關鍵字? true 為開啟,false 為關閉。
var KeywordPostFliter = true;
// 留言是否過濾關鍵字? true 為開啟,false 為關閉。
var keywordCommentFliter = true;
// 文章是否過濾黑名單? true 為開啟,false 為關閉。
var blacklistPostFliter = true;
// 留言是否過濾黑名單? true 為開啟,false 為關閉。
var blacklistCommentFliter = true;
// 程式開始
// 加入隱藏切換
jQuery('head').append('');
jQuery('.BH-menuE').append('
關閉過濾器');
jQuery('.BH-menuE').append('開啟過濾器');
jQuery('body').append('');
// 將 blockKeywords 加入 postBlockKeywords 和 commentBlockKeywords 中
for (let i = 0; i < blockKeywords.length; i++) postBlockKeywords[postBlockKeywords.length] = commentBlockKeywords[commentBlockKeywords.length] = blockKeywords[i];
postBlockKeywords = postBlockKeywords.map(value => value.toLowerCase());
commentBlockKeywords = commentBlockKeywords.map(value => value.toLowerCase());
//BC頁分開
switch (location.pathname) {
case '/B.php':
// blacklist post fliter
blacklistPostFliter ? getBlackList().then(BlackList => jQuery('.b-list__count__user>a').each((index, value) => BlackList.includes(value.innerHTML.toLowerCase()) ? jQuery(value).parents('.b-list__row').addClass('BlockHide') : void (0))) : void (0);
// keywords post title fliter
KeywordPostFliter ? jQuery('.b-list__main__title').each((index, value) => postBlockKeywords.forEach(data => value.innerText.toLowerCase().includes(data) ? jQuery(value).parents('.b-list__row').addClass('BlockHide') : void (0))) : void (0);
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('#Commendlist_' + postid).each((index, element) => {
blacklistCommentFliter ? getBlackList().then(BlackList => jQuery(element).find('.reply-content__user').each((index, value) => BlackList.includes(value.href.replace('https://home.gamer.com.tw/', '').toLowerCase()) ? jQuery(value).parents('.c-reply__item').addClass('BlockHide') : void (0))) : void (0);
keywordCommentFliter ? jQuery(element).find('.reply-content__article').each((index, value) => commentBlockKeywords.forEach(data => value.innerText.toLowerCase().includes(data) ? jQuery(value).parents('.c-reply__item').addClass('BlockHide') : void (0))) : void (0);
});
} else if (times++ < 50) restartFliter(100);
}, ms);
});
};
// blacklist post fliter
blacklistPostFliter ? getBlackList().then(BlackList => jQuery('.c-post__header__author>.userid').each((index, value) => BlackList.includes(value.innerHTML.toLowerCase()) ? jQuery(value).parents('.c-section').addClass('BlockHide') : void (0))) : void (0);
// blacklist comment fliter
blacklistCommentFliter ? getBlackList().then(BlackList => jQuery('.reply-content__user').each((index, value) => BlackList.includes(value.href.replace('https://home.gamer.com.tw/', '').toLowerCase()) ? jQuery(value).parents('.c-reply__item').addClass('BlockHide') : void (0))) : void (0);
// keywords post fliter
KeywordPostFliter ? jQuery('.c-article__content').each((index, value) => postBlockKeywords.forEach(data => value.innerText.toLowerCase().includes(data) ? jQuery(value).parents('.c-section').addClass('BlockHide') : void (0))) : void (0);
// keywords comment fliter
keywordCommentFliter ? jQuery('.reply-content__article').each((index, value) => commentBlockKeywords.forEach(data => value.innerText.toLowerCase().includes(data) ? jQuery(value).parents('.c-reply__item').addClass('BlockHide') : void (0))) : void (0);
break;
case '/Bo.php':
blacklistPostFliter ? getBlackList().then(BlackList => jQuery('.FM-blist6>a[href*="home.gamer.com.tw"]').each((index, value) => BlackList.includes(value.innerHTML.toLowerCase()) ? jQuery(value).parents('tr').addClass('BlockHide') : void (0))) : void (0);
KeywordPostFliter ? jQuery('.FM-blist3').each((index, value) => postBlockKeywords.forEach(data => value.innerText.toLowerCase().includes(data) ? jQuery(value).parents('tr').addClass('BlockHide') : void (0))) : void (0);
break;
case '/search.php':
KeywordPostFliter ? jQuery('.gsc-table-cell-snippet-close').each((index, value) => postBlockKeywords.forEach(data => value.innerText.toLowerCase().includes(data) ? jQuery(value).parents('.gsc-result').addClass('BlockHide') : void (0))) : void (0);
break;
}
function getBlackList() {
return new Promise(resolve => {
getBHBlackList().then(BlackList => {
let removeNum = [];
ForceHideList.forEach(value => BlackList.includes(value.replace(/\s/g, '').toLowerCase()) ? void (0) : BlackList[BlackList.length] = value.replace(/\s/g, '').toLowerCase());
BlackList.forEach((value, index) => ForceShowList.includes(value.toLowerCase()) ? removeNum[removeNum.length] = index : void (0));
for (let i = removeNum.length; i > 0; i--) BlackList.splice(removeNum[i], 1);
localStorage.setItem('BHBlackList', JSON.stringify({ time: new Date().getTime(), BlackList: BlackList }));
resolve(BlackList);
});
});
}
function getBHBlackList(forceReload) {
return new Promise(resolve => {
if (localStorage.getItem('BHBlackList') && !forceReload) {
let BHBlackList = JSON.parse(localStorage.getItem('BHBlackList')), today = new Date((new Date()).getFullYear(), (new Date()).getMonth(), (new Date()).getDate(), 0, 0, 0, 0);
today.getTime() < BHBlackList.time && BHBlackList.time < today.getTime() + 86400000 ? resolve(BHBlackList.BlackList) : getBlackList(true).then(data => resolve(data));
} else GM_xmlhttpRequest({
method: "GET",
url: "https://home.gamer.com.tw/ajax/friend_getData.php?here=0",
onload: data => {
resolve(data.response.includes('') ? data.response.split('
')[1].replace('
', '').split(',') : []);
}
});
});
}
})();