CrazyJeux / TotalBlacklistMobile

// ==UserScript==
// @name        TotalBlacklistMobile
// @namespace   CrazyJeux/Daring-Do
// @author		CrazyJeux/Daring-Do
// @match		*://m.jeuxvideo.com/*
// @description Cache les messages et les topics des forumeurs blacklistés depuis l'installation du script. N'a absolument aucune influence sur la version de bureau, et vice-versa.
// @version		1
// @grant       none
// ==/UserScript==

var askForConfirmation = true;
var loadNextThreads = true;

function getRealType(obj) {
	return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1];
}

function log() {
	return;
	var args = Array.prototype.slice.call(arguments), str = "", type;
	for (var i = 0; i < args.length; i++) {
		type = getRealType(args[i]);
		str += (i > 0 ? "\n" : "") + "(" + type + ") ";
		type = type.toLowerCase();
		switch (type) {
			case "arguments":
			case "array":
			case "object":
				var cache = [];
				str += JSON.stringify(args[i], function (key, value) {
					if (typeof value === 'object' && value !== null) {
						if (cache.indexOf(value) >= 0) {
							return "(Already shown here)";
						}
						cache.push(value);
					}
					return value;
				}, 4);
				break;
			case "undefined":
				str += "undefined";
				break;
			default:
				str += args[i];
		}
	}
	window.console && window.console.log && window.console.log(str);
}

function handleBlacklist() {
	var nickname = this.parentNode.parentNode.querySelector("a.bloc-pseudo-msg");
	if (nickname === null) {
		alert("Erreur : le script TotalBlacklist n'a pas pu enregistrer cet utilisateur.");
		return;
	}
	nickname = nickname.textContent.replace(/[\s\n]/g, "");
	if (askForConfirmation === true) {
    	var r = confirm("Êtes-vous sûr de vouloir blacklister " + nickname + " ?");
    	if (r === false) {
    		return;
    	}
	}
	nickname = nickname.toLowerCase();
	addToBlacklist(nickname);

	var authors = document.querySelectorAll("a.bloc-pseudo-msg");
	for (var i = 0; i < authors.length; i++) {
		var messageAuthor = authors[i].innerHTML.replace(/[\s\n]/g, "").toLowerCase();
		if (messageAuthor === nickname) {
			var msg = authors[i].parentNode;
			while ((' ' + msg.className + ' ').indexOf(" post ") < 0) {
				msg = msg.parentNode;
			}
			msg.style.display = "none";
		}
	}
}

function addToBlacklist(nickname) {
	if (nickname === "auteurblacklisté" || nickname === "#" || nickname === "") {
		return;
	}
	var currentArray = localStorage.getItem("blacklisted");
	if (currentArray === null || currentArray === "") {
		currentArray = [];
	} else {
		currentArray = JSON.parse(currentArray);
	}
	if (currentArray.indexOf(nickname) < 0) {
		currentArray.push(nickname);
		localStorage.setItem("blacklisted", JSON.stringify(currentArray));
	}
}

function callMe() {
	var attr = document.body.getAttribute("data-totalblacklist");
	if (attr === null || attr === "") {
		document.body.setAttribute("data-totalblacklist", "true");
	} else {
		return;
	}

	//Remove some threads on the forum main page
	var nbHiddenThreads = 0;
	var hrefs = [];
	var topicList = document.querySelector("ul.liste-topics");
	if (topicList !== null) {
		var LIs = topicList.querySelectorAll("li");
		for (var i = 0; i < LIs.length; i++) {
			var href = LIs[i].querySelector(".a-topic").getAttribute("href");
			hrefs.push(href);
		}

		var blacklisted = localStorage.getItem("blacklisted");
		if (blacklisted !== null && blacklisted !== "") {
			blacklisted = JSON.parse(blacklisted);
			if (blacklisted.length > 0) {
				var topicAuthors = topicList.querySelectorAll(".info-post-topic .auteur");
				for (var i = 0; i < topicAuthors.length; i++) {
					var author = topicAuthors[i].textContent.replace(/[\s\n]/g, "").toLowerCase();
					if (blacklisted.indexOf(author) >= 0) {
						var li = topicAuthors[i].parentNode;
						while (li.tagName !== "LI") {
							li = li.parentNode;
						}
						//li.style.display = "none";
						li.remove();
						nbHiddenThreads++;
					}
				}
			}
		}
	}

	//Add a blacklist button to each of the messages
	var opts = document.querySelectorAll(".bloc-opt");
	for (var i = 0; i < opts.length; i++) {
		var reportButton = opts[i].querySelector("a");
		if (reportButton !== null) {
			var spanBlacklist = document.createElement("span");
			spanBlacklist.title = "Blacklister";
			spanBlacklist.style.background = 'rgba(0, 0, 0, 0) url("/skin/skin-jv/img/forum/icones-messages.png") no-repeat scroll -60px 0';
			spanBlacklist.style.cursor = "pointer";
			spanBlacklist.style.marginRight = "8px";
			spanBlacklist.style.display = "inline-block";
			spanBlacklist.style.height = "16px";
			spanBlacklist.style.width = "16px";
			spanBlacklist.style.overflow = "hidden";
			spanBlacklist.innerHTML = "&nbsp;";
			spanBlacklist.addEventListener("click", handleBlacklist, true);
			opts[i].insertBefore(spanBlacklist, reportButton);
			reportButton.style.display = "inline-block";
		}
	}

	//Hide some messages in a thread
	var blacklisted = localStorage.getItem("blacklisted");
	if (blacklisted !== null && blacklisted !== "" && blacklisted !== "[]") {
		blacklisted = JSON.parse(blacklisted);
		var messages = document.querySelectorAll(".post");
		for (var i = 0; i < messages.length; i++) {
			var messageAuthorDiv = messages[i].querySelector("a.bloc-pseudo-msg");
			if (messageAuthorDiv !== null) {
				var messageAuthor = messageAuthorDiv.innerHTML.replace(/[\s\n]/g, "").toLowerCase();
				if (blacklisted.indexOf(messageAuthor) >= 0) {
					messages[i].style.display = "none";
				}
			}
		}
	}

	//Load the next threads to compensate the "holes"
	if (nbHiddenThreads > 0 && loadNextThreads) {
		var nbHiddenThreadsBase = nbHiddenThreads;
		var nextPageButton = document.querySelector(".bloc-pagination-topics a.nav-next");
		if (nextPageButton !== null) {
			var curNbOfCalls = 0;
			var nextUrl = "http:" + nextPageButton.getAttribute("href");
			(function loop(nextUrl, curNbOfCalls, nbHiddenThreadsBase) {
				var xmlhttp = new XMLHttpRequest();
				xmlhttp.onreadystatechange = function () {
					if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
						curNbOfCalls++;

						var nextPageContent = document.createElement('div');
						nextPageContent.innerHTML = xmlhttp.responseText;

						var stop = false;
						var remoteTopicList = nextPageContent.querySelector("ul.liste-topics");
						if (remoteTopicList !== null) {
							var remoteLIs = remoteTopicList.querySelectorAll("li");
							for (var i = 0; i < remoteLIs.length; i++) {
								var remoteHref = remoteLIs[i].querySelector(".a-topic").getAttribute("href");
								if (hrefs.indexOf(remoteHref) < 0) {
									var topicAuthorDiv = remoteLIs[i].querySelector(".info-post-topic .auteur");
									if (topicAuthorDiv !== null) {
										var topicAuthor = topicAuthorDiv.textContent.replace(/[\s\n]/g, "").toLowerCase();
										if (blacklisted.indexOf(topicAuthor) < 0) {
											topicList.appendChild(remoteLIs[i]);
											nbHiddenThreadsBase--;
											if (nbHiddenThreadsBase === 0) {
												stop = true;
												break;
											}
										}
									}
								}
							}
						}
						if (stop === true || curNbOfCalls > 2) {
							return;
						}
						var nextPageButton = nextPageContent.querySelector(".bloc-pagination-topics a.nav-next");
						if (nextPageButton !== null) {
							var nextUrl = "http:" + nextPageButton.getAttribute("href");
							loop(nextUrl, curNbOfCalls, nbHiddenThreadsBase);
						}
					}
				};
				xmlhttp.open("GET", nextUrl, true);
				xmlhttp.send();
			})(nextUrl, curNbOfCalls, nbHiddenThreadsBase);
		}
	}
}

callMe();

//Respeed
addEventListener('instantclick:newpage', callMe);