CrazyJeux / JVCPreview

// ==UserScript==
// @name        JVCPreview
// @namespace   CrazyJeux/Daring-Do
// @author		CrazyJeux/Daring-Do
// @match		*://www.jeuxvideo.com/*
// @description Permet de prévisualiser les messages et topics au survol des liens avec la souris.
// @version		2
// @grant       none
// ==/UserScript==

function isPermalinkOrThread(url) {
	if (url === "#Protocole_inconnu") {
		return false;
	}
	var matchesPermalink = /^https?\:\/\/(www.)?jeuxvideo\.com\/[^\/]+\/forums\/message\/\d+/.test(url);
	if (matchesPermalink === true) {
		return true;
	}
	var matchesThread = /(^https?\:\/\/(www.)?jeuxvideo\.com)?\/forums\/\d+.*\.htm/.test(url);
	return matchesThread;
}

function insertAfter(referenceNode, newNode) {
	referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

function addStyle(rule) {
	var css = document.createElement('style');
	css.type = 'text/css';
	css.innerHTML = rule;
	document.body.appendChild(css);
}

function removeClass(element, classToRemove) {
	if (classToRemove === 'loading') {
		element.className = element.className.replace(/(?:^|\s)loadingIframe(?:\s|$)/gim, '');
	} else { //'waiting'
		element.className = element.className.replace(/(?:^|\s)waitingForHover(?:\s|$)/gim, '');
	}
}

function setIframeDimensions(iframe, iframeBody) {
	var iframeMessages = iframeBody.getElementsByClassName('bloc-message-forum');
	var h, w;
	if (iframeMessages.length === 0) {
		h = iframeBody.getElementsByTagName('b')[0].offsetHeight + 20;
		w = iframeBody.getElementsByTagName('b')[0].offsetWidth + 30;
	} else {
		h = iframeBody.offsetHeight + 25;
		w = iframeBody.offsetWidth + 25;
	}
	iframe.height = h + 'px';
	iframe.width = w + 'px';
}

function loadIframe(div, link) {
	if (div.innerHTML === '' && link.className.indexOf('loadingIframe') < 0) {
		link.className += ' loadingIframe';
		var iframe = document.createElement('iframe');
		iframe.src = link.href;
		var blocs = document.getElementsByClassName("bloc-contenu");
		if (blocs.length === 0) {
			iframe.width = '649px';
		} else {
			var bloc = blocs[blocs.length - 1];
			iframe.width = bloc.offsetWidth + 'px';
		}
		iframe.height = '500px';
		iframe.onload = function () {
			var iframeDocument = this.contentDocument ? this.contentDocument : this.contentWindow.document;
			var iframeBody = iframeDocument.getElementsByTagName('body') [0];
			var iframeMessages = iframeBody.getElementsByClassName('bloc-message-forum');
			if (iframeMessages.length > 0) {
				var wrapper = document.createElement("div");
				for (var i = 0; i < iframeMessages.length; i++) {
					var html = iframeMessages[i].outerHTML;
					wrapper.innerHTML += html;
				}
				iframeBody.innerHTML = '';
				iframeBody.appendChild(wrapper);
			} else {
				var divInfo = document.createElement('div');
				divInfo.innerHTML = '<b>Ce topic/message n\'existe plus.</b>';
				iframeBody.innerHTML = '';
				iframeBody.appendChild(divInfo);
			}
			removeClass(link, 'loading');
			if (div.style.display === 'none') {
				link.className += ' waitingForHover';
			} else {
				setIframeDimensions(iframe, iframeBody);
			}
		};
		div.appendChild(iframe);
	}
}

function showIframe() {
	var div = this.getElementsByTagName("div")[0];
	var link = this.getElementsByTagName("a")[0];
	div.style.display = 'block';
	if (link.className.indexOf('waitingForHover') > 0) {
		var iframe = div.getElementsByTagName('iframe') [0];
		var iframeDocument = iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document;
		var iframeBody = iframeDocument.getElementsByTagName('body') [0];
		setIframeDimensions(iframe, iframeBody);
		removeClass(link, 'waiting');
		return;
	}
	if (loadIframesOnPageLoad === false) {
		loadIframe(div, link);
	}

}

function hideIframe() {
	this.getElementsByTagName("div")[0].style.display = 'none';
}

function callMe() {
	var table = document.getElementById("table-liste-topic-forum");
	if (table !== null) {
		table.style.width = "auto";
	}
	addStyle('.loadingIframe { cursor: wait; }');
	var messages = document.querySelectorAll(".txt-msg, .titre-topic");
	for (var i = 0; i < messages.length; i++) {
		var links = messages[i].getElementsByTagName('a');
		for (var j = 0; j < links.length; j++) {
			var href = links[j].getAttribute("href");
			//console.log("href='" + href + "'");
			var isLink = isPermalinkOrThread(href);
			if (isLink === true) {
				//console.log("Permalink/thread: " + links[j].href);
				var divWrapper = document.createElement('div');
				divWrapper.addEventListener('mouseover', showIframe, true);
				divWrapper.addEventListener('mouseout', hideIframe, true);
				divWrapper.innerHTML = links[j].outerHTML;
				divWrapper.style.display = "inline-block";
				var parent = links[j].parentNode;
				parent.replaceChild(divWrapper, links[j]);
				var div = document.createElement('div');
				div.style.display = 'none';
				div.style.position = 'relative';
				div.style.zIndex = '100';
				divWrapper.appendChild(div);
				if (loadIframesOnPageLoad === true) {
					(function (divWrapper, div) {
						setTimeout(function () {
							var link = divWrapper.getElementsByTagName("a")[0];
							loadIframe(div, link);
						}, 0);
					})(divWrapper, div);
				}
			}
		}
	}
}

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

var loadIframesOnPageLoad = false;

//Prevent very long/infinite loops...
if (inIframe() === false) {
	callMe();
}

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