CrazyJeux / SpawnBadges

// ==UserScript==
// @name         SpawnBadges
// @namespace    CrazyJeux/Daring-Do
// @version      9
// @description  Affiche les badges dans les profils selon le nombre de messages actuel
// @author       CrazyJeux/Daring-Do
// @match        *://*.jeuxvideo.com/*
// @grant        none
// ==/UserScript==

var isCalled = false;

function unique() {
	function updateBadgesScript() {
        var badges = [
            {
                name: "carton",
                points: 0
            },
            {
                name: "bronze",
                points: 50
            },
            {
                name: "argent",
                points: 200
            },
            {
                name: "or",
                points: 1000
            },
            {
                name: "rubis",
                points: 10000
            },
            {
                name: "saphir",
                points: 30000
            },
            {
                name: "emeraude",
                displayedName: "Émeraude",
                points: 75000
            },
            {
                name: "diamant",
                points: 150000
            }
        ];

        var url = document.location.href;
        var isDesktopVersionAndOnProfilePage = (url.indexOf("www.jeuxvideo.com/profil/") >= 0 && url.indexOf("?mode=infos") > 0);
        var isMobileVersionAndOnProfilePage = (url.indexOf("m.jeuxvideo.com/profil/") >= 0 && url.indexOf(".html") > 0);
        if (!isDesktopVersionAndOnProfilePage && !isMobileVersionAndOnProfilePage) {
            return;
        }

        var currentUserNicknameArea = null;
        if (isDesktopVersionAndOnProfilePage) {
            currentUserNicknameArea = document.querySelector(".account-pseudo");
        } else {
            currentUserNicknameArea = document.querySelector(".pseudo");
        }
        if (currentUserNicknameArea !== null) {
            var currentUserNickname = currentUserNicknameArea.textContent.trim().toLowerCase();

            var profileNicknameArea = null;
            if (isDesktopVersionAndOnProfilePage) {
                profileNicknameArea = document.querySelector(".infos-pseudo h1");
            } else {
                profileNicknameArea = document.querySelector(".titre-pseudo .titre");
            }
            if (profileNicknameArea !== null) {
                var profileNickname = profileNicknameArea.textContent.trim().toLowerCase();

                if (currentUserNickname === profileNickname) {
                    var el = document.querySelector("ul.dropdown-menu.menu-compte-head a[href*='alerte.php']");
                    if (el !== null) {
                        var modoBadge = document.createElement("img");
                        modoBadge.setAttribute("src", "//static.jvc.gg/1.57.7/img/profils/badges/64px/badge-moderateur.png");
                        modoBadge.setAttribute("alt", "Modérateur");
                        modoBadge.setAttribute("title", "Modérateur");
                        listeHautsFaits.insertBefore(modoBadge, listeHautsFaits.firstChild);
                    }
                }
            }
        }

        var nbMsgsArea = null;
        if (isDesktopVersionAndOnProfilePage) {
            var msgsForumText = document.querySelectorAll(".info-lib");
            msgsForumText = [].slice.call(msgsForumText, 0);
            msgsForumText = msgsForumText.filter(function(el) {
                return el.textContent.indexOf("Messages Forums :") >= 0;
            });
            if (msgsForumText.length !== 0) {
                msgsForumText = msgsForumText[0];

                nbMsgsArea = msgsForumText.nextSibling;
            }
        } else {
            nbMsgsArea = document.querySelector(".cdv-nbpost span");
        }
        if (nbMsgsArea === null) {
            return;
        }

        var nbMsgs = nbMsgsArea.textContent.replace(/( messages?)|\./g, "");
        nbMsgs = parseInt(nbMsgs, 10);
        //console.log("nbMsgs=" + nbMsgs);

        var listeHautsFaits = null;
        if (isDesktopVersionAndOnProfilePage) {
            listeHautsFaits = document.querySelector(".liste-hauts-faits");
            if (listeHautsFaits === null) {
                var parent = document.querySelector("#page-profil > div.container.container-content > div.row:nth-child(2) > div.col-md-6:nth-child(1)");
                if (parent !== null) {
                    var blocDefaultProfil = document.createElement("div");
                    blocDefaultProfil.className = "bloc-default-profil";
                    blocDefaultProfil.innerHTML = '<div class="header">'+
                        '<h2>Badges JeuxVideo.com</h2>'+
                        '</div>'+
                        '<div class="body hauts-faits">'+
                        '<div class="liste-hauts-faits"></div>'+
                        '</div>';
                    parent.appendChild(blocDefaultProfil);
                    listeHautsFaits = document.querySelector(".liste-hauts-faits");
                }
            }
        } else {
            var newListHighFeats = document.createElement("div");
            newListHighFeats.className = "bloc-cdv cdv-nbpost";
            newListHighFeats.innerHTML = '<h2 class="titre">Badges JeuxVideo.com :'+
                '<div class="liste-hauts-faits"></div>'+
                '</h2>';

            var nbMsgsBlock = nbMsgsArea.parentNode.parentNode;
            nbMsgsBlock.parentNode.insertBefore(newListHighFeats, nbMsgsBlock.nextSibling);
            listeHautsFaits = document.querySelector(".liste-hauts-faits");
        }

        //Remove all the already displayed badges from the "badges" array
        var imgs = listeHautsFaits.querySelectorAll("img");
        for (var i=0; i<imgs.length; i++) {
            var img = imgs[i];
            var src = img.getAttribute("src");
            //console.log("src='"+src+"'");
            if (src.indexOf("badge-rang") >= 0) {
                //http://static.jvc.gg/1.57.7/img/profils/badges/64px/badge-rang-diamant.png
                var rang = src.replace("//static.jvc.gg/1.57.7/img/profils/badges/64px/badge-rang-", "").replace(".png", "");
                var index = null;
                badges.every(function(el, i) {
                    if (el.name === rang) {
                        index = i;
                        return false;
                    }
                    return true;
                });
                if (index !== null) {
                    badges.splice(index, 1);
                }
            }
        }

        for (var j=0; j<badges.length; j++) {
            var b = badges[j];
            if (nbMsgs >= b.points) {
                var displayedName;
                if (b.hasOwnProperty("displayedName")) {
                    displayedName = b.displayedName;
                } else {
                    displayedName = b.name.charAt(0).toUpperCase() + b.name.slice(1);
                }
                var newBadge = document.createElement("img");
                newBadge.src = '//static.jvc.gg/1.57.7/img/profils/badges/64px/badge-rang-'+b.name+'.png';
                newBadge.setAttribute("alt", "Rang "+displayedName);
                newBadge.setAttribute("title", "Rang "+displayedName);
                newBadge.setAttribute("style", "margin: 15px 0 0 10px;");
                if (displayedName === "Carton") {
                    var bronze = listeHautsFaits.querySelector("img[alt='Rang Bronze']");
                    if (bronze !== null) {
                        bronze.parentNode.insertBefore(newBadge, bronze);
                    } else {
                        listeHautsFaits.appendChild(newBadge);
                    }
                    continue;
                }
                listeHautsFaits.appendChild(newBadge);
            }
        }
    }

	function callMe() {
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.innerHTML = "(function(){ " + updateBadgesScript.toString() + " updateBadgesScript();})();";
		document.body.appendChild(script);
	}

    if (isCalled) {
        return;
    }
    isCalled = true;

	callMe();

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

document.addEventListener('DOMContentLoaded', unique, false);
window.addEventListener('load', unique, false);