CrazyJeux / SmartQuotes

// ==UserScript==
// @name        SmartQuotes
// @namespace   CrazyJeux/Daring-Do
// @author		CrazyJeux/Daring-Do
// @match		*://www.jeuxvideo.com/forums/*
// @match		*://www.forumjv.com/forums/*
// @description Améliore les citations en ne gardant que l'essentiel.
// @version		4
// @grant       none
// ==/UserScript==

//Topic dédié : http://www.jeuxvideo.com/forums/42-1000021-39487177-1-0-1-0-smartquotes-ameliorez-les-citations.htm

var hideNestedQuotes = true;
var maxNumberOfNestedQuotes = 2;
var infoShow = "Cliquez pour afficher plus de citations";
var infoHide = "Cliquez pour afficher moins de citations";

var removeMyNestedQuotes = true;

var onlyQuotePermalink = true;
var quotePermalinkInfo = "Citer le permalien";

var useShortQuotes = true;

function toCall() {
	function handleNestedQuotes(element, nestedNb) {
		var children = element.childNodes;
		var listOfQuotes = [];
		for (var child in children) {
			var tag = children[child].tagName;
			if (tag === "BLOCKQUOTE") {
				listOfQuotes.push(children[child]);
			}
		}
		for (var i = 0; i < listOfQuotes.length; i++) {
			var quote = listOfQuotes[i];
			var nextNumber = nestedNb + 1;
			if (nestedNb > maxNumberOfNestedQuotes) {
				var p = document.createElement("p");
				p.innerHTML = "<b><u>" + infoShow + "</u></b>";
				p.title = infoShow;
				p.style.cursor = "pointer";
				p.addEventListener('click', function (e) {
					e.stopPropagation();
					var quoteToShow = this.nextSibling;
					quoteToShow.style.display = "block";
					this.style.display = "none";
				});
				quote.style.cursor = "pointer";
				quote.title = infoHide;
				quote.addEventListener('click', function (e) {
					e.stopPropagation();
					var spanToShow = this.previousSibling;
					spanToShow.style.display = "block";
					this.style.display = "none";
				});
				quote.parentNode.insertBefore(p, quote);
				quote.style.display = "none";
				nextNumber = maxNumberOfNestedQuotes;
			}
			handleNestedQuotes(quote, nextNumber);
		}
	}

	function handleTextareaQuotes() {
		textarea.value = textarea.value.replace(/> *> *>.*\n*/g, "");
	}

	function handleQuotePermalink(e) {
		e.stopImmediatePropagation();
		var curPos = textarea.selectionStart;
		var permalink = this.parentNode.parentNode.querySelector('.bloc-date-msg a').href;
		var textBeforeCurPos = textarea.value.substring(0, curPos);
		var textAfterCurPos = textarea.value.substring(curPos);
		var carriageReturns = "";
		if (curPos > 0) {
			carriageReturns = "\n\n";
		}
		var add = carriageReturns + "> ''Message cité : " + permalink + " ''\n\n";
		var newValue = textBeforeCurPos + add + textAfterCurPos;
		textarea.value = newValue;
		textarea.focus();
		textarea.selectionStart = textarea.selectionEnd = curPos + add.length;
	}

	function handleShorterQuote() {
		var previousValue = textarea.value;
		var timer = setInterval(function () {
			if (textarea.value !== previousValue) {
				clearInterval(timer);
			} else {
				return;
			}

			var previousResult = "", result = textarea.value;

			while (result !== previousResult) {
				previousResult = result;
				result = result.replace(/((?:> *)+)Le +(\d+) +([^ ]+) +(\d+) +à +(\d\d:\d\d:\d\d) +([^ +]+) +a\s+écrit +:\n(((?:> *)+)[^\n]*)(\n((?:> *)+)[^\n]*)?/g, function (all, firstQuotes, day, month, year, hms, nickname, firstLine, firstLineQuotes, secondLine, secondLineQuotes) {
					/*console.log("all='" + all + "' (length=" + all.length + ")");
					 console.log("firstQuotes='" + firstQuotes + "' (length=" + firstQuotes.length + ")");*/
					/*console.log("day='" + day + "' (length=" + day.length + ")");
					 console.log("month='" + month + "' (length=" + month.length + ")");
					 console.log("year='" + year + "' (length=" + year.length + ")");
					 console.log("hms='" + hms + "' (length=" + hms.length + ")");
					 console.log("nickname='" + nickname + "' (length=" + nickname.length + ")");*/
					/*console.log("firstLine='" + firstLine + "' (length=" + firstLine.length + ")");
					 console.log("firstLineQuotes='" + firstLineQuotes + "' (length=" + firstLineQuotes.length + ")");*/
					var nbOfQuotes0 = (firstQuotes.split(">").length - 1);
					var nbOfQuotes1 = (firstLineQuotes.split(">").length - 1);
					//console.log("nbOfQuotes0=" + nbOfQuotes0 + ", nbOfQuotes1=" + nbOfQuotes1);
					if (nbOfQuotes0 !== nbOfQuotes1) {
						//console.log("Multiple lines (different number of quotes for meta line and first line)");
						return firstQuotes + " '''" + nickname + " :'''\n" + firstLine + secondLine;
					}
					if (typeof secondLine === "undefined" || secondLine === "") {
						//console.log("secondLine is undefined");
					} else {
						/*console.log("secondLine='" + secondLine + "' (length=" + secondLine.length + ")");
						 console.log("secondLineQuotes='" + secondLineQuotes + "' (length=" + secondLineQuotes.length + ")");*/
					}
					if (typeof secondLine !== "undefined" && secondLine !== "") {
						var nbOfQuotes2 = (secondLineQuotes.split(">").length - 1);
						//console.log("nbOfQuotes1=" + nbOfQuotes1 + ", nbOfQuotes2=" + nbOfQuotes2);
						if (nbOfQuotes1 !== nbOfQuotes2) {
							//console.log("Single line (quote 1) and second line (quote 2): different number of quotes for first line and second line");
							return firstQuotes + " '''" + nickname + " :''' " + firstLine.replace(/>/g, "") + secondLine;
						} else {
							//console.log("Multiple lines for the same quote: same number of quotes for first line and second line");
							return firstQuotes + " '''" + nickname + " :'''\n" + firstLine + secondLine;
						}
					} else {
						//console.log("Single line, no second line found");
						return firstQuotes + " '''" + nickname + " :''' " + firstLine.replace(/>/g, "");
					}
				});
			}

			textarea.value = result;
			textarea.focus();
			textarea.selectionStart = textarea.selectionEnd = textarea.value.length;
			
			
	if (removeMyNestedQuotes === true) {
	    handleTextareaQuotes();
	}
		}, 50);
	}

	var body = document.getElementsByTagName("body")[0];
	var attr = body.getAttribute("data-shortquotes");
	if (attr === null || attr === "") {
		body.setAttribute("data-shortquotes", "true");
	} else {
		return;
	}

	var textarea = document.getElementById('message_topic');

	if (hideNestedQuotes === true) {
		var messages = document.getElementsByClassName("txt-msg");
		for (var i = 0; i < messages.length; i++) {
			handleNestedQuotes(messages[i], 1);
		}
	}

	if (removeMyNestedQuotes === true) {
		var sendButton = document.getElementsByClassName("btn-poster-msg")[0];
		sendButton.addEventListener('click', handleTextareaQuotes, true);
		textarea.addEventListener('keyup', handleTextareaQuotes, true);
	}

	if (onlyQuotePermalink === true) {
		//Prevent infinite loop
		var quoteButtons = Array.prototype.slice.call(document.getElementsByClassName('picto-msg-quote'));
		for (var i = 0; i < quoteButtons.length; i++) {
			var element = quoteButtons[i];
			var span1 = document.createElement("span");
			span1.className = "picto-msg-quote";
			span1.title = quotePermalinkInfo;
			span1.style.opacity = "1.0";
			//Dat pyramid
			span1.style.background = 'rgba(0, 0, 0, 0) url("data:image/png;base64,'
				+ 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAABGdBTUEAAK/INwWK'
				+ '6QAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAO'
				+ 'pgAABdwnLpRPAAAABJQTFRF////teYd////4+PjsbGxysrKr9YbKAAAAAF0Uk5TAED'
				+ 'm2GYAAAABYktHRACIBR1IAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAOElEQVQI12NgEA'
				+ 'QDBgZGCEMAKgAUQmIoKQopEWIIKgoqgxlCikKKEClHpUAIAwhRDYQy4JbCnAEAwXsJ38'
				+ 'NAIVQAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTUtMDUtMTRUMDE6MDY6MzQrMDI6MDBcrOD'
				+ 'IAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE1LTA1LTE0VDAxOjA2OjM0KzAyOjAwLfFYdAAAA'
				+ 'Ep0RVh0c2lnbmF0dXJlAGE0NWQyMmVkOGJlNjljMTMzNWQyNjBjMTEzNGZkMzdmMzJhMWE3'
				+ 'OThmMmRmNDhkYTI0Y2E3NzllYTFjMmM2YWJ6WDGHAAAAAElFTkSuQmCC") no-repeat scroll';
			var span2 = document.createElement("span");
			span2.innerHTML = quotePermalinkInfo;
			span1.appendChild(span2);
			span1.addEventListener('click', handleQuotePermalink, true);
			element.parentNode.insertBefore(span1, element);

			if (useShortQuotes === true) {
				element.addEventListener('click', handleShorterQuote, true);
			}
		}
	}
}

toCall();

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