softkhey / Blacklist topic

// ==UserScript==
// @author pleinsdennuis
// @name Blacklist topic
// @namespace Blacklist
// @license MIT
// @description Cache les topics des forumeurs blacklistés via ce script
// @include *://*.jeuxvideo.com/*
// @version 1.0.1
// @grant none
// ==/UserScript==

(()=>{
	function add_el(el, name, parentName){
		el.id = name;
		el.setAttribute('class', name);
		document.getElementById(parentName).appendChild(el);
	}

	function printBLpseudos(pseudo) {
		const li = document.createElement('li');
		li.setAttribute('id', pseudo);
		li.style.cursor = 'pointer';
		li.innerHTML = pseudo+' [x]';
		pseudoList.append(li);
	}

	function printInfoMessage(el, color, message){
		el.style.color = color;
		el.innerText = message;
	}

	function getCookie(name) {
		const v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
		return v ? v[2] : null;
	}

	const NAME_PLUGIN = 'Blacklist Topic';
	const VERSION = '1.0.1';
	const LIMIT_BL = 300;
	const topicBL = [];

	let BLACKLIST_USERS = [];
	if (getCookie('pseudo')) {
		BLACKLIST_USERS = (getCookie('pseudo')).split(',');
	}

	function blacklistTopic() {
		for (let i = 1; i < 27; i++) {
			if (i != 13 && TOPICS) {
				const result = /^(.+?)\n\t(.+)\t(.+)\t(.+)\t$/.exec(TOPICS[i].innerText);
				for (let j = 0; j < BLACKLIST_USERS.length; j++){
					if (result && result[2] == BLACKLIST_USERS[j]) {
						topicBL.push(TOPICS[i]);
						console.log(TOPICS[i]);
						TOPICS[i].style.background = 'grey';
						const ent = (TOPICS[i]).children[0];
						const ent1 = ent.children[1];
						ent1.style.color = 'grey';

						const paz = (TOPICS[i]).children[1];
						paz.style.color = 'grey'
					}
				}
			}
		}
	}
	const CONTENEUR_TOPICS = document.getElementsByClassName('topic-list topic-list-admin');
	let TOPICS;
	if (CONTENEUR_TOPICS.length > 0) {
		TOPICS = CONTENEUR_TOPICS[0].children;
	}
	const blocPagiDefault = document.getElementsByClassName('bloc-pagi-default'),
	displayTopic = document.createElement('div'),
	
	newPanel = document.createElement('div'), //panel entier (titre+corps)
	panelHeading = document.createElement('div'), //titre du panel
	panelBody = document.createElement('div'), //corps du panel
	
	desc = document.createElement('p'),
	err = document.createElement('p'),
	formBlacklist = document.createElement('form'),
	inputPseudo = document.createElement('input'),
	submitBtn = document.createElement('button'),

	pseudoList = document.createElement('ul'),
	titreList = document.createElement('p'), //Liste des pseudo blacklistés (0) :

	formDel = document.createElement('form'),
	inputDelPseudo = document.createElement('input'),
	delBlacklistBtn = document.createElement('button'),

	relaodPageBtn = document.createElement('button'),

	formReset = document.createElement('form'),
	resetBlacklistBtn = document.createElement('button');

	pseudoList.style.overflowY = scroll;
 
	add_el(newPanel, 'panel panel-jv-forum', 'forum-right-col');
	add_el(panelHeading, 'panel-heading', 'panel panel-jv-forum');
	add_el(panelBody, 'panel-body', 'panel panel-jv-forum');
	
	//ajout du titre du panel
	panelHeading.appendChild(document.createTextNode(NAME_PLUGIN+' v'+VERSION));

	add_el(desc, 'desc', 'panel-body');
	desc.appendChild(document.createTextNode('Utilisation: \n\n\
		Entrez dans le champ de saisi le pseudo que vous voulez blackliser puis cliquez sur "VALIDER".\n\
		La blacklist est limitée à '+LIMIT_BL+' pseudos. Faites vous plaisir :)\n (Si le script bug, supprimez les cookies de jvc)'));
	add_el(displayTopic, 'displayTopic', 'panel-body');
	
	displayTopic.style.cursor = 'pointer';

	add_el(err, 'err', 'panel-body');
	
	add_el(formBlacklist, 'formBlacklist', 'panel-body');
	add_el(inputPseudo, 'inputPseudo', 'formBlacklist');
	inputPseudo.placeholder = 'Pseudo à blacklister';
	inputPseudo.autocomplete = 'off';
	inputPseudo.style.color = 'black';
	add_el(submitBtn, 'submitBtn', 'formBlacklist');
	submitBtn.appendChild(document.createTextNode('BLACKLISTER'));
	submitBtn.style.color = 'black';
	
	//ajoute le bouton RESET LA BLACKLIST
	add_el(resetBlacklistBtn, 'resetBlacklistBtn', 'panel-body');
	resetBlacklistBtn.appendChild(document.createTextNode('RESET LA BLACKLIST'));
	resetBlacklistBtn.style.color = 'black';

	add_el(formReset, 'formReset', 'panel-body');
	add_el(resetBlacklistBtn, 'resetBlacklistBtn', 'formReset');
	add_el(titreList, 'titreList', 'panel-body');
	titreList.appendChild(document.createTextNode('Liste des pseudos blacklistés ('+BLACKLIST_USERS.length+') : '));
	add_el(pseudoList, 'pseudoList', 'panel-body');

	titreList.style.color = 'green';

	err.appendChild(document.createTextNode(''));

	blacklistTopic();

	displayTopic.appendChild(document.createTextNode('Afficher/Masquer les topics blacklistés ('+topicBL.length+')'));
	formBlacklist.addEventListener('submit', event => {
		event.preventDefault();
		const URL = 'https://www.jeuxvideo.com/sso/ajax_suggest_pseudo.php?pseudo='+inputPseudo.value;
		fetch(URL).then(response=>{
			if(response.ok){
				response.json().then(json=>{
					const alias = json.alias;
					if (alias.length == 0) {
						printInfoMessage(err, 'red', '[ERREUR] Le pseudo n\'existe pas ou est banni');
					}
					else if((BLACKLIST_USERS.indexOf(alias[0].pseudo)) > 0){
						printInfoMessage(err, 'red', '[Erreur] Le pseudo est déjà blacklisté !');

					}
					else if(BLACKLIST_USERS.length >= LIMIT_BL){
						printInfoMessage(err, 'red', '[Erreur] La blacklist ne peut pas contenir plus de '+LIMIT_BL+' pseudos !');
					}
					else{
						printInfoMessage(err, 'green', '[Info] Pseudo blacklisté !');
						BLACKLIST_USERS.push(alias[0].pseudo);
						document.cookie = 'pseudo='+BLACKLIST_USERS+'; expires=Fri, 31 Dec 9999 23:59:59 GMT';
						BLACKLIST_USERS = (getCookie('pseudo')).split(',');
						printBLpseudos(alias[0].pseudo);
						blacklistTopic();
					}
				});
			}
			inputPseudo.value = '';
		});
	});

	displayTopic.addEventListener('click', event => {
		event.preventDefault();
		for (let i = 0; i < topicBL.length; i++) {
			console.log(topicBL[i].style.backgroundColor);
			console.log(topicBL[i]);
			topicBL[i].style.backgroundColor = topicBL[i].style.backgroundColor == 'grey' ? 'white' : 'grey';
			const ent = (topicBL[i]).children[0];
			const ent1 = ent.children[1];
			ent1.style.color = ent1.style.color == 'grey' ? '#006bd7' : 'grey';

			const paz = (topicBL[i]).children[1];
			paz.style.color = paz.style.color == 'grey' ? '#000' : 'grey';
		}
	});


	formReset.addEventListener('submit', event => {
		event.preventDefault();
		document.cookie = "pseudo=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
		printInfoMessage(err, 'violet', '[Info] Blacklist réinitialisée ! (Rechargez la page)');
	});

	for (let i = 0; i < BLACKLIST_USERS.length; i++) {
		const pseudos = BLACKLIST_USERS[i]
		printBLpseudos(pseudos);
		
		const test = document.getElementById(pseudos);
		test.addEventListener('click', event => {
			event.preventDefault();
			BLACKLIST_USERS.splice(BLACKLIST_USERS.indexOf(pseudos), 1);
			document.cookie = 'pseudo='+BLACKLIST_USERS+'; expires=Fri, 31 Dec 9999 23:59:59 GMT';
			printInfoMessage(err, 'green', '[Info] '+pseudos+' supprimé de la blacklist ! Le pseudo est banni. (Rechargez la page)');
		
		});
	}
})();