EmyJV / Custom RainbowDDB

// ==UserScript==
// @name        Custom RainbowDDB 
// @namespace   CrazyJeux/Daring-Do - Modifié par EmyJV
// @author		CrazyJeux/Daring-Do - Modifié par EmyJV
// @match		*://www.jeuxvideo.com/*
// @match		*://m.jeuxvideo.com/*
// @match		*://www.forumjv.com/*
// @description Modif' du script RainbowDDB - Discrétion assurée.
// @license      MIT
// @version		0.1
// @grant       none
// ==/UserScript==

//Topic dédié : http://www.jeuxvideo.com/forums/42-1000021-39882332-1-0-1-0-rainbowddb-reperez-tous-les-messages-signales-meme-les-votres.htm

//Explications : http://image.noelshack.com/fichiers/2015/25/1434754809-explications-final.png

//Ajoutez ou retirez ici les motifs de ban complets qui doivent conduire au masquage automatique du message.
var reasonsToHide = [];

//Enlevez les "//" des trois lignes ci-dessous pour cacher tous les messages signalés dont la raison a pu être déterminée.
//reasonsToHide = reasonsToHide.concat("Pédopornographie", "Incitation à la haine, discrimination", "Mise en danger des personnes", "Diffamation, menaces", "Piratage, non respect des droits d'auteurs", "Apologie de comportements illégaux", "Données personnelles");
//reasonsToHide = reasonsToHide.concat("Pornographie", "Insultes", "Flood de masse", "Raids, mass-dislikes et attaques de sites", "Spoilers");
//reasonsToHide = reasonsToHide.concat("Message inopportun", "Doublons", "Publicité", "Règles spécifiques à chaque forum");

var colors = {
    "byother": "#c11515",
	"byme": "chocolate",
	"ishandledbymod": "forestgreen",
	"ishandledbyadmin": "#3b70ba",
	"isadmin": "royalblue"
};

var borderWidthMessage = "1px";
var borderWidthTopic = "1px";
var topicsToo = true;

var isMobileIframe = false;
var srcSave = null;
var originSave = null;

var DEBUG = false;

function log(str) {
    if (DEBUG) {
        if (isMobileIframe == true)
        {
            srcSave.postMessage({'log': str}, originSave);
        } else {
            console.log(str);
        }
    }
}

function ajaxReport(link, element, width, type) {
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
			var reportDiv = document.createElement('div');
			reportDiv.innerHTML = xmlhttp.responseText;
			reportDiv.style.display = "none";
			document.body.appendChild(reportDiv);
			var reportTextarea = reportDiv.querySelector("#signalement_commentaire");
			if (reportTextarea === null) {
                log("reportTextarea is null");
                var generic = reportDiv.querySelector(".modal-generic-content");
				if (reportDiv.querySelector(".g-recaptcha") === null &&
                    //The condition below is for moderators (who don't have a captcha)
                   generic.innerHTML.indexOf("Ce contenu a déjà été signalé par un utilisateur") < 0) {
                    log("this message wasn't reported by someone else");
					if (generic === null) {
                        log("generic is null");
						return;
					}
					if (generic.innerHTML.indexOf("Vous avez déjà signalé ce contenu") >= 0) {
						styleElement(element, "byme", width, type);
						return;
					}
					if (generic.innerHTML.indexOf("Cet utilisateur ne peut pas être signalé") >= 0) {
						styleElement(element, "isadmin", width, type);
						return;
					}
					if (generic.innerHTML.indexOf("Ce contenu a déjà été modéré. En cas de problème, vous pouvez contacter un administrateur à moderation@jeuxvideo.com") >= 0) {
						styleElement(element, "ishandledbymod", width, type);
						return;
					}
					if (generic.innerHTML.indexOf("Ce contenu a déjà été modéré par un administrateur") >= 0) {
						styleElement(element, "ishandledbyadmin", width, type);
						return;
					}
				} else {
                    log("this message was reported by someone else");
					styleElement(element, "byother", width, type);
					if (type === "message") {
						var reason = reportDiv.querySelector(".col-md-10");
						if (reason !== null) {
                            reason = reason.innerHTML;
                            log("reason is not null: '"+reason+"'");
							var options = element.querySelector(".bloc-options-msg");
							/*
							var info = document.createElement("div");
							info.innerHTML = reason;
							info.style.float = "left";
							info.style.color = "red";
							info.style.fontWeight = "bold";
							info.style.top = "12px";
							info.style.marginLeft = "15px";
							info.style.position = "relative";
							options.parentNode.insertBefore(info, options);
							*/
							var msg =element.querySelector(".bloc-header .bloc-date-msg");
                            msg.append(" - ");
							var p = Array.prototype.slice.call(msg.querySelectorAll("p"));
							p.forEach(function(el) {
								var klass = el.getAttribute("class");
								if (klass === null) {
									klass = "";
								}
								klass += " maybeToHide";
								el.setAttribute("class", klass);
							});
							var toHide = Array.prototype.slice.call(element.querySelectorAll(".maybeToHide"));
							var wrapper = document.createElement("p");
							var info = document.createElement("span");
							info.innerHTML = reason.charAt(0).toLowerCase() + reason.slice(1);
							info.style.color = "#d81c1c";
							info.style.top = "12px";
							msg.appendChild(wrapper.appendChild(info));

							var shouldHide = false;
							var hideShowMsg = document.createElement("p");
							if (reasonsToHide.indexOf(reason) >= 0) {
								shouldHide = true;
								toHide.forEach(function(el) {
									el.style.display = "none";
								});
								wrapper.appendChild(document.createElement("br"));
								hideShowMsg.innerHTML = "Afficher le contenu du message";
								hideShowMsg.style.marginTop = "15px";
								hideShowMsg.style.cursor = "pointer";
								hideShowMsg.style.textDecoration = "underline";
								hideShowMsg.style.marginBottom = "0px";
								wrapper.appendChild(hideShowMsg);
								wrapper.style.marginBottom = "0px";
							}

							msg.insertBefore(wrapper, msg.firstChild);

							if (shouldHide === true) {
								var isShown = false;

								hideShowMsg.onclick = function() {
									if (!isShown) {
										var r = confirm("Voulez-vous vraiment afficher ce message ?\n\n" +
														"Vous pouvez changer les motifs de signalement conduisant au masquage du message.\n\n" +
														"Pour cela, il faut modifier la variable reasonsToHide au début du code du script.");
										if (r === false) {
											return;
										}
										toHide.forEach(function(el) {
											el.style.display = "initial";
										});
										hideShowMsg.innerHTML = "Cacher le contenu du message";
										wrapper.style.marginBottom = "";
										isShown = true;
									} else {
										toHide.forEach(function(el) {
											el.style.display = "none";
										});
										hideShowMsg.innerHTML = "Afficher le contenu du message";
										wrapper.style.marginBottom = "0px";
										isShown = false;
									}
								};
							}
						}
					}
				}
			}
			reportDiv.remove();
		}
	};
	xmlhttp.open("GET", link, true);
	xmlhttp.send();
}

function checkMessage(href, callback) {
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.onreadystatechange = function () {
		if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
			var reportDiv = document.createElement('div');
			reportDiv.innerHTML = xmlhttp.responseText;
			reportDiv.style.display = "none";
			document.body.appendChild(reportDiv);
			var textarea = reportDiv.querySelector("#signalement_commentaire");
			if (textarea !== null) {
                log("textarea is not null");
				callback('isnotreported');
				return;
			}
			var errorDiv = reportDiv.querySelector(".alert.alert-danger");
			if (errorDiv !== null) {
                log("errorDiv is not null, errorDiv.innerHTML:\n"+errorDiv.innerHTML);
				if (errorDiv.innerHTML.indexOf("Ce contenu a déjà été modéré. En cas de problème, vous pouvez contacter un administrateur à moderation@jeuxvideo.com") >= 0) {
					callback('ishandledbymod');
					return;
				}
				if (errorDiv.innerHTML.indexOf("Ce contenu a déjà été modéré par un administrateur") >= 0) {
					callback('ishandledbyadmin');
					return;
				}
				if (errorDiv.innerHTML.indexOf("Ce contenu a déjà été signalé") >= 0) {
					callback('isreported');
					return;
				}
				if (errorDiv.innerHTML.indexOf("Cet utilisateur ne peut pas être signalé") >= 0) {
					callback('isadmin');
					return;
				}
			}
		}
	};
	xmlhttp.open("GET", href, true);
	xmlhttp.send();
}

function inIframe() {
	try {
		return window.self !== window.top;
	} catch (e) {
		return true;
	}
}

function styleElement(element, info, borderWidth, elementType) {
	//element.style.borderWidth = borderWidth + "!important";

    var el = null;
    var prevStyle = "";
    if (elementType === "topic") {
        el = element.querySelector("a.topic-author");
        prevStyle = el.getAttribute("style");
        if (typeof prevStyle === "undefined" || prevStyle === null) {
            prevStyle = "";
        }
    }

	switch (info) {
		case "ishandledbymod":
            if (elementType === "topic") {
                el.setAttribute("style", prevStyle + ";color: " + colors.ishandledbymod + " !important;");
            } else {
                element.style.cssText += ";box-shadow: 0 0 " + 0 + " " + borderWidth + " " + colors.ishandledbymod + " !important;";
            }
			break;
		case "ishandledbyadmin":
            if (elementType === "topic") {
                el.setAttribute("style", prevStyle + ";color: " + colors.ishandledbyadmin + " !important;");
            } else {
                element.style.cssText += ";box-shadow: 0 0 " + 0 + " " + borderWidth + " " + colors.ishandledbyadmin + " !important;";
            }
			break;
		case "byme":
            if (elementType === "topic") {
                element.style.cssText += ";border: " + borderWidth + " solid " + colors.byme + " !important;";
                el.setAttribute("style", prevStyle + ";color: " + colors.byme + " !important;");
            } else {
                element.style.cssText += ";box-shadow: 0 0 " + 0 + " " + borderWidth + " " + colors.byme + " !important;";
            }
			break;
		case "byother":
            if (elementType === "topic") {
                element.style.cssText += ";border: " + borderWidth + " solid " + colors.byother + " !important;";
                el.setAttribute("style", prevStyle + ";color: " + colors.byother + " !important;");
            } else {
                element.style.cssText += ";box-shadow: 0 0 " + borderWidth + " " + borderWidth + " " + colors.byother + " !important;";
            }
			break;
		case "isadmin":
            if (elementType === "topic") {
                element.style.cssText += ";border: " + borderWidth + " solid " + colors.isadmin + " !important;";
                el.setAttribute("style", prevStyle + ";color: " + colors.isadmin + " !important;");
            } else {
                element.style.cssText += ";box-shadow: 0 0 " + 0 + " " + borderWidth + " " + colors.byother + " !important;";
            }
			break;
		default:
			var err = "Erreur fonction styleElement : info='" + info + "'";
			alert(err);
			throw new Error(err);
	}
}

function toCall() {
	//Our own messages
	if (window.location.hostname === "www.jeuxvideo.com") {
		var deleteButtons = document.querySelectorAll(".picto-msg-croix");
        log("deleteButtons.length: "+deleteButtons.length);
		if (deleteButtons.length > 0) {
            var requestsForMyMessages = function(origin) {
				var i = 0;
				(function loop(i) {
                    log("my message  #"+i+"/"+deleteButtons.length);
					if (i >= deleteButtons.length) {
						return;
					}
					var message = deleteButtons[i].parentNode;
					while (message.getAttribute("class").indexOf("bloc-message-forum") < 0) {
						message = message.parentNode;
					}
                    var messAttr = message.getAttribute("data-handledbyrainbowddb");
                    var isHandled = (messAttr !== null && messAttr === "true");
                    if (!isHandled) {
                        message.setAttribute("data-handledbyrainbowddb", "true");
                        log("before send message to iframe; message id: " + message.getAttribute("data-id"));
                        iframeMessages.contentWindow.postMessage({'messageid': message.getAttribute("data-id")}, origin);
                    }

					setTimeout(function () {
						loop(i + 1);
					}, 5);
				})(i);
            };

			var handleResponseFromMobileJVCForMessages = function (e) {
                var origin = e.origin || e.originalEvent.origin;
				if (origin !== 'http://m.jeuxvideo.com') {
					return;
				}
				if (e.data.hasOwnProperty("log")) {
                    log("[Mobile iframe] " + e.data.log);
                    return;
                }
				if (e.data.hasOwnProperty("started")) {
                    if (e.data.started === true) {
                        log("mobile iframe is ready, start checking my messages");
                        requestsForMyMessages(origin);
                    }
                    return;
                }
                log("got a response from mobile JVC, e.data:\n" + JSON.stringify(e.data, null, 4));
				if (e.data.hasOwnProperty("messageid")) {
					var message = document.querySelector("[data-id='" + e.data.messageid + "']");
                    log("e.data.messageid: " + e.data.messageid + ", message found ? " + (message !== null));

                    if (message !== null) {
                        if (e.data.hasOwnProperty("isnotreported")) {
                            return;
                        }
                        if (e.data.hasOwnProperty("ishandledbymod")) {
                            styleElement(message, "ishandledbymod", borderWidthMessage, "message");
                            return;
                        }
                        if (e.data.hasOwnProperty("ishandledbyadmin")) {
                            styleElement(message, "ishandledbyadmin", borderWidthMessage, "message");
                            return;
                        }
                        if (e.data.hasOwnProperty("isreported")) {
                            styleElement(message, "byother", borderWidthMessage, "message");
                            return;
                        }
                        if (e.data.hasOwnProperty("isadmin")) {
                            styleElement(message, "isadmin", borderWidthMessage, "message");
                            return;
                        }
                    }
				}
			};

			window.addEventListener("message", handleResponseFromMobileJVCForMessages, false);

			var mobileURL;
			var index = window.location.pathname.substring(1).indexOf("/");
			var firstDirectory = window.location.pathname.substring(1, index + 1);
			//Direct message
			if (firstDirectory !== "forums") {
				mobileURL = document.querySelector(".btn.btn-actu-new-list-forum").parentNode.href.replace("www.jeuxvideo.com", "m.jeuxvideo.com");
			} else { //Message in a topic
				var desktopURL = document.location.href;
				mobileURL = desktopURL.replace("www.jeuxvideo.com", "m.jeuxvideo.com");
			}

			var iframeMessages = document.createElement("iframe");
			iframeMessages.style.display = "none";
			document.body.appendChild(iframeMessages);
			iframeMessages.src = mobileURL;
		}
	}

	//Other people's messages
	var reportButtons = document.getElementsByClassName("picto-msg-exclam");
	var i = 0;
	(function loop(i) {
        log("reportButton #"+i+"/"+reportButtons.length);
		if (i >= reportButtons.length) {
			return;
		}
		var link = reportButtons[i].getAttribute("data-selector");
		var message = reportButtons[i].parentNode;
		while (message.className.indexOf("bloc-message-forum") < 0) {
			message = message.parentNode;
		}
        var messAttr = message.getAttribute("data-handledbyrainbowddb");
        var isHandled = (messAttr !== null && messAttr === "true");
        log("is link ("+link+") already handled? "+isHandled);
        if (!isHandled) {
            message.setAttribute("data-handledbyrainbowddb", "true");

            log("before ajaxReport for link '"+link+"'");
			ajaxReport(link, message, borderWidthMessage, "message");
        }

        setTimeout(function () {
            loop(i + 1);
        }, 5);
	})(i);

	//Topics
	if (topicsToo === true) {
		var table = document.querySelector(".topic-list");
		if (table !== null) {
			var topicRows = table.querySelectorAll("li[data-id]");

			var topicRequests = [];

			var handleResponseFromMobileJVCForTopics = function (e) {
				var origin = e.origin || e.originalEvent.origin;
				if (origin !== 'http://m.jeuxvideo.com') {
					return;
				}
				log("result in listener: "+JSON.stringify(e.data, null, 4));
				if (e.data.hasOwnProperty("started")) {
					if (e.data.started === true) {
						var resFilter = topicRequests.filter(function(obj) {
							return obj.url === e.data.url;
						});
						if (resFilter.length > 0) {
							resFilter[0].func(origin);
						}
					}
					return;
				}
				if (e.data.hasOwnProperty("topicid")) {
					var row = null;
					for (var j = 0; j < topicRows.length; j++) {
						var curDataId = topicRows[j].getAttribute("data-id");
						if (curDataId === e.data.topicid) {
							row = topicRows[j];
							break;
						}
					}
					if (row !== null) {
						if (e.data.hasOwnProperty("isnotreported")) {
							return;
						}
						if (e.data.hasOwnProperty("ishandled")) {
							styleElement(row, "ishandled", borderWidthTopic, "topic");
							return;
						}
						if (e.data.hasOwnProperty("isreported")) {
							styleElement(row, "byother", borderWidthTopic, "topic");
							return;
						}
						if (e.data.hasOwnProperty("isadmin")) {
							styleElement(row, "isadmin", borderWidthTopic, "topic");
							return;
						}
					}
				}
			};

			window.addEventListener("message", handleResponseFromMobileJVCForTopics, false);

			for (var i = 0; i < topicRows.length; i++) {
				var link = topicRows[i].querySelector("a");
				if (link === null) {
					continue;
				}
				var href = link.href;

				var topicAuthorNickname = null;
				var topicAuthorArea = topicRows[i].querySelector(".topic-author");
				if (topicAuthorArea !== null) {
					topicAuthorNickname = topicAuthorArea.textContent.trim();
				}
				var currentAuthorNickname = null;
				var currentAuthorArea = document.querySelector(".account-pseudo");
				if (currentAuthorArea !== null) {
					currentAuthorNickname = currentAuthorArea.textContent.trim();
				}
				var myTopic = false;
				if (topicAuthorNickname !== null && currentAuthorNickname !== null) {
					if (topicAuthorNickname.toLowerCase() === currentAuthorNickname.toLowerCase()) {
						myTopic = true;
					}
				}
				if (myTopic === false) {
					(function(href, topic) {
						var xmlhttp = new XMLHttpRequest();
						xmlhttp.onreadystatechange = function () {
							if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
								var topicDiv = document.createElement('div');
								topicDiv.innerHTML = xmlhttp.responseText;
								var mess = topicDiv.querySelector(".bloc-message-forum");
								if (mess !== null) {
									topicDiv.innerHTML = mess.outerHTML;
									var reportBtn = topicDiv.querySelector(".picto-msg-exclam");
									if (reportBtn !== null) {
										var link = reportBtn.getAttribute("data-selector");
										ajaxReport(link, topic, borderWidthTopic, "topic");
									}
								}
							}
						};
						xmlhttp.open("GET", href, true);
						xmlhttp.send();
					})(href, topicRows[i]);
				} else { //One of my topics
					var topicID = topicRows[i].getAttribute("data-id");
					var iframeTopic = document.createElement("iframe");
					(function (iframeTopic, topicID, href, link) {
						var mobileURL = href.replace("www", "m");
						log("my topic: '"+mobileURL+"', topicID='"+topicID+"'");
						var requestForATopic = function (origin) {
							log("requestForATopic for my topic: '"+mobileURL+"', topicID='"+topicID+"'");
							iframeTopic.contentWindow.postMessage({'topicid': topicID, 'topicname': link.textContent.trim()}, origin);
						};
						topicRequests.push({"url": mobileURL, "func": requestForATopic});
						iframeTopic.style.display = "none";
						document.body.appendChild(iframeTopic);
						iframeTopic.src = mobileURL;
					})(iframeTopic, topicID, href, link);
				}
			}
		}
	}
}


if (window.location.hostname.indexOf("forumjv") < 0) {
    document.domain = "jeuxvideo.com";
}
if (window.location.hostname !== "m.jeuxvideo.com") {

	toCall();

	addEventListener('instantclick:newpage', toCall);

    (function btnLoop() {
        var moreCommentsButtons = document.querySelectorAll(".link-plus-de-comm");
        for (var i=0; i<moreCommentsButtons.length; i++) {
            var btnAttr = moreCommentsButtons[i].getAttribute("data-handledbyrainbowddb");
            var isHandled = (btnAttr !== null && btnAttr === "true");
            if (!isHandled) {
                moreCommentsButtons[i].setAttribute("data-handledbyrainbowddb", "true");

                moreCommentsButtons[i].onclick = function() {
                    (function loop() {
                        var newElements = document.querySelectorAll(".bloc-message-forum:not([data-handledbyrainbowddb])");
                        if (newElements.length === 0) {
                            setTimeout(loop, 50);
                        } else {
                            btnLoop();
                            toCall();
                        }
                    })();
                };
            }
        }
    })();
} else {
	if (inIframe()) {
        isMobileIframe = true;

		var handleResponseFromDesktopJVC = function (e) {
            var origin = e.origin || e.originalEvent.origin;
			if (origin !== 'http://www.jeuxvideo.com') {
				return;
			}
            srcSave = e.source;
            originSave = origin;
			log("e.data in iframe: "+JSON.stringify(e.data, null, 4), srcSave, originSave);
			//Message
			if (e.data.hasOwnProperty("messageid")) {
                var realId = "#post_" + e.data.messageid;
				var message = document.querySelector(realId);
                if (message === null) {
                    log("message " + e.data.messageid + " not found...", srcSave, originSave);
                    return;
                }
				var alertButton = message.querySelector(".picto-msg-exclam");
                if (alertButton === null) {
                    log("alertButton is null", srcSave, originSave);
                    return;
                }
				var href = alertButton.parentNode.getAttribute("href");
				checkMessage(href, function (ret) {
					var result = {'messageid': e.data.messageid};
					result[ret] = true;
                    log("message " + e.data.messageid + " checked: " + ret, srcSave, originSave);
					e.source.postMessage(result, origin);
				});
			}

			//Topic
			if (e.data.hasOwnProperty("topicid")) {
				var message = document.querySelector(".post");
                if (message !== null) {
                    var alertButton = message.querySelector(".picto-msg-exclam");
                    if (alertButton !== null) {
                        var href = alertButton.parentNode.getAttribute("href");
                        checkMessage(href, function (ret) {
                            var result = {'topicid': e.data.topicid, 'topicname': e.data.topicname};
                            result[ret] = true;
							log2("result in iframe: "+JSON.stringify(result, null, 4));
                            e.source.postMessage(result, origin);
                        });
                    } else {
						log2("alertButton is null, message html:\n"+message.outerHTML);
					}
                } else {
					log2("message is null");
				}
			}
		};

		window.addEventListener("message", handleResponseFromDesktopJVC, false);

        window.parent.postMessage({"started": true, "url": window.location.href}, "*");
	}
}