// ==UserScript==
// @name North-Plus Block
// @namespace https://github.com/sssssssl/NP-Scripts
// @version 0.2.1
// @description 为北+提供关键词屏蔽功能
// @author sl
// @match https://*.summer-plus.net/thread.php?fid-*
// @match https://*.summer-plus.net/thread.php?fid-*
// @match https://*.level-plus.net/thread.php?fid-*
// @match https://*.white-plus.net/thread.php?fid-*
// @match https://*.south-plus.net/thread.php?fid-*
// @match https://*.imoutolove.me/thread.php?fid-*
// @grant GM_setValue
// @grant GM_getValue
// @downloadURL https://update.greasyfork.icu/scripts/369914/North-Plus%20Block.user.js
// @updateURL https://update.greasyfork.icu/scripts/369914/North-Plus%20Block.meta.js
// ==/UserScript==
(function() {
'use strict';
const STORE_BLOCK_MODE_KEY = 'blockEnabled';
const STORE_BLOCK_WORDS_KEY = 'blockWords';
const ID_BTN_BLOCK_SWITCH = 'np-block-switch';
const ID_BTN_SHOW_LIST = 'np-block-btn-show-list';
const ID_NP_BLOCK_LIST_CONTAINER = 'np-block-list-container';
const ID_NP_BLOCK_LIST = 'np-block-list';
const ID_BTN_ADD_WORD = 'np-block-list-add-btn';
const ID_INPUT_WORD = 'np-block-list-add-input';
const CLS_BTN_DEL_WORD = 'btn-delete-block-word';
const CLS_MODE_DISABLED = 'np-block-disabled';
const CLS_MODE_ENABLED = 'np-block-enabled';
const CLS_LIST_UNFOLD = 'np-list-unfold';
const CLS_LIST_FOLD = 'np-list-fold';
const CLS_LIST_ITEM = 'block-list-item';
const CLS_FADEIN = 'np-fadein';
const CLS_FADEOUT = 'np-fadeout';
const TIME_FADE = 1;
const CLS_THREAD = '.tr3.t_one';
const CLS_THREAD_TITLE = 'h3 a';
const NP_BLOCK_HTML = `
`;
const NP_BLOCK_STYLE = `
`;
// 2. CODE ENTRYPOINT.
let blockModeEnabled = GM_getValue(STORE_BLOCK_MODE_KEY, false);
let blockWords = GM_getValue(STORE_BLOCK_WORDS_KEY);
if(!blockWords) {
blockWords = [];
}
let hiddenThreads = [];
// add panel & register callbacks.
document.head.insertAdjacentHTML('beforeend', NP_BLOCK_STYLE);
document.body.insertAdjacentHTML('beforeend', NP_BLOCK_HTML);
let btnBlockSwitch = document.getElementById(ID_BTN_BLOCK_SWITCH)
let btnShowlist = document.getElementById(ID_BTN_SHOW_LIST);
let btnAddWord = document.getElementById(ID_BTN_ADD_WORD);
let inputWord = document.getElementById(ID_INPUT_WORD);
let npBlockListContainer = document.getElementById(ID_NP_BLOCK_LIST_CONTAINER);
let npBlockList = document.getElementById(ID_NP_BLOCK_LIST);
btnShowlist.onclick = () => {
btnShowlist.classList.toggle(CLS_MODE_DISABLED);
btnShowlist.classList.toggle(CLS_MODE_ENABLED);
npBlockListContainer.classList.toggle(CLS_LIST_FOLD);
npBlockListContainer.classList.toggle(CLS_LIST_UNFOLD);
};
btnBlockSwitch.onclick = () => {
btnBlockSwitch.classList.toggle(CLS_MODE_DISABLED);
btnBlockSwitch.classList.toggle(CLS_MODE_ENABLED);
blockModeEnabled = !blockModeEnabled;
btnBlockSwitch.textContent = blockModeEnabled ? '开' : '关';
GM_setValue(STORE_BLOCK_MODE_KEY, blockModeEnabled);
if(blockModeEnabled) {
parseThreadTitle(blockWords);
}
else {
let i = hiddenThreads.length;
while (i > 0) {
let elem = hiddenThreads.pop();
elem.style.display = 'table-row';
elem.classList.remove(CLS_FADEOUT);
elem.classList.add(CLS_FADEIN);
i -= 1;
}
}
};
btnAddWord.onclick = () => {
let word = inputWord.value;
if(word.length == 0) {
return;
}
inputWord.value = '';
blockWords.push(word);
GM_setValue(STORE_BLOCK_WORDS_KEY, blockWords);
renderBlockWord(word);
if(blockModeEnabled) {
parseThreadTitle([word]);
}
}
btnBlockSwitch.textContent = blockModeEnabled ? '开' : '关';
if(blockModeEnabled && btnBlockSwitch.classList.contains(CLS_MODE_DISABLED)) {
btnBlockSwitch.classList.toggle(CLS_MODE_DISABLED);
btnBlockSwitch.classList.toggle(CLS_MODE_ENABLED);
}
blockWords.forEach(renderBlockWord);
if(blockModeEnabled) {
parseThreadTitle(blockWords);
}
function parseThreadTitle(wordList) {
let titleList = [];
let threads = document.querySelectorAll(CLS_THREAD);
threads.forEach((elem) => {
let title = elem.querySelector(CLS_THREAD_TITLE).textContent;
wordList.forEach(word => {
if(title.includes(word)) {
hiddenThreads.push(elem);
elem.classList.remove(CLS_FADEIN);
elem.classList.add(CLS_FADEOUT);
setTimeout(() => {
elem.style.display = 'none';
}, TIME_FADE*1000);
}
});
});
}
function addDeleteWordCallback(btn) {
btn.onclick = () => {
let word = btn.previousElementSibling.textContent;
let idx = blockWords.indexOf(word);
if(idx >= 0) {
blockWords.splice(idx, 1);
GM_setValue(STORE_BLOCK_WORDS_KEY, blockWords);
btn.parentElement.remove();
}
else {
console.log(`${word}, ${idx}`);
}
};
}
function renderBlockWord(word) {
let wordHTML = `
${word}
删
`;
npBlockList.insertAdjacentHTML('afterbegin', wordHTML);
let newDelBtn = npBlockList.querySelector(`.${CLS_BTN_DEL_WORD}`);
addDeleteWordCallback(newDelBtn);
}
})();