ImThatGuy / StickersJVC

// ==UserScript==
// @name         StickersJVC
// @author       ImThatGuy
// @description  StickersJVC vous permet d'utiliser les stickers JVC rapidement.
// @include      http://*.jeuxvideo.com/*
// @include      https://*.jeuxvideo.com/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/tipso/1.0.8/tipso.min.js
// @require      https://raw.githubusercontent.com/avxto/nuContextMenu/master/src/jquery.nu-context-menu.js
// @grant        GM_addStyle
// @grant        GM_getValue
// @grant        GM_setValue
// @version      0.4.3
// @icon         https://i.imgur.com/NHTdDRb.png
// @copyright    2018+, ImThatGuy
// @license      MIT
// ==/UserScript==

/*
StickersJVC. 2018-2020.
Codé par ImThatGuy.

2020+, ImThatGuy.

StickersJVC vous permet d'utiliser les stickers JVC rapidement.

0.1 : Première version.
0.2 : Détecte automatiquement si vous utilisez DarkJVC
      Rajout du sticker lamasticot sasuke
0.3 : Rajout des catégories : Bud & Grukk
0.4 : Amélioration design scrollbar
      Correction bug ajout de stickers en MP (même si ça sert à rien parce que les stickers ne marchent pas en MP :hap:)
	  Ajout d'une catégorie favoris + gestion de favoris
	  Légère amélioration de design avec DarkJVC
	  Suppression de l'espace automatique avant le sticker
	  0.4.1 : Ajout du sticker hap calin
	  0.4.2 : Ajout de stickers bud
	  0.4.3 : correctifs problèmes
*/

/*jshint multistr: true */
(function() {
    'use strict';

	const version = "0.4.3";
	console.log("[StickersJVC version "+version+"]");

	// IMPORT CSS
	$('head').append('<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/tipso/1.0.8/tipso.min.css"/>');
	$('head').append('<link rel="stylesheet" type="text/css" href="https://epmd.000webhostapp.com/files/nu-context-menu.css"/>');

	// Current div
	var currentDiv = "fav";
	// LISTS
	var _stickers_hap = [
		"http://image.jeuxvideo.com/stickers/p/1kki",
		"http://image.jeuxvideo.com/stickers/p/1kkn",
		"http://image.jeuxvideo.com/stickers/p/1lmk",
		"http://image.jeuxvideo.com/stickers/p/1kkl",
		"http://image.jeuxvideo.com/stickers/p/1lmh",
		"http://image.jeuxvideo.com/stickers/p/1ljr",
		"http://image.jeuxvideo.com/stickers/p/1ljj",
		"http://image.jeuxvideo.com/stickers/p/1kkg",
		"http://image.jeuxvideo.com/stickers/p/1kkh",
		"http://image.jeuxvideo.com/stickers/p/1kkk",
		"http://image.jeuxvideo.com/stickers/p/1ljn",
		"http://image.jeuxvideo.com/stickers/p/1mr0",
		"http://image.jeuxvideo.com/stickers/p/1ljl",
		"http://image.jeuxvideo.com/stickers/p/1nua",
		"http://image.jeuxvideo.com/stickers/p/1ljm",
		"http://image.jeuxvideo.com/stickers/p/1ljq",
		"http://image.jeuxvideo.com/stickers/p/1lmi",
		"http://image.jeuxvideo.com/stickers/p/1lml",
		"http://image.jeuxvideo.com/stickers/p/1lmj",
		"http://image.jeuxvideo.com/stickers/p/1lmm",
		"http://image.jeuxvideo.com/stickers/p/1lmn",
		"http://image.jeuxvideo.com/stickers/p/1lmo",
		"http://image.jeuxvideo.com/stickers/p/1lmp",
		"http://image.jeuxvideo.com/stickers/p/1mqv",
		"http://image.jeuxvideo.com/stickers/p/1kkm",
		"http://image.jeuxvideo.com/stickers/p/1kkj",
		"http://image.jeuxvideo.com/stickers/p/1mr1",
		"http://image.jeuxvideo.com/stickers/p/1mqy",
		"http://image.jeuxvideo.com/stickers/p/1kgx"
	];
	var _stickers_noel = [
		"http://image.jeuxvideo.com/stickers/p/1kkr",
		"http://image.jeuxvideo.com/stickers/p/1kko",
		"http://image.jeuxvideo.com/stickers/p/1kkp",
		"http://image.jeuxvideo.com/stickers/p/1kkq",
		"http://image.jeuxvideo.com/stickers/p/1kks",
		"http://image.jeuxvideo.com/stickers/p/1ljo",
		"http://image.jeuxvideo.com/stickers/p/1ljp",
		"http://image.jeuxvideo.com/stickers/p/1kkt",
		"http://image.jeuxvideo.com/stickers/p/1kku",
		"http://image.jeuxvideo.com/stickers/p/1kkv",
		"http://image.jeuxvideo.com/stickers/p/1mqw",
		"http://image.jeuxvideo.com/stickers/p/1mqz"
	];
	var _stickers_brid = [
		"http://image.jeuxvideo.com/stickers/p/1jnd",
		"http://image.jeuxvideo.com/stickers/p/1jnc",
		"http://image.jeuxvideo.com/stickers/p/1jne",
		"http://image.jeuxvideo.com/stickers/p/1jnf",
		"http://image.jeuxvideo.com/stickers/p/1jng",
		"http://image.jeuxvideo.com/stickers/p/1jnh",
		"http://image.jeuxvideo.com/stickers/p/1jni",
		"http://image.jeuxvideo.com/stickers/p/1jnj"
	];
	var _stickers_rex = [
		"http://image.jeuxvideo.com/stickers/p/1lm9",
		"http://image.jeuxvideo.com/stickers/p/1lma",
		"http://image.jeuxvideo.com/stickers/p/1lmb",
		"http://image.jeuxvideo.com/stickers/p/1lmc",
		"http://image.jeuxvideo.com/stickers/p/1lmd",
		"http://image.jeuxvideo.com/stickers/p/1lme",
		"http://image.jeuxvideo.com/stickers/p/1lmf",
		"http://image.jeuxvideo.com/stickers/p/1lmg"
	];
	var _stickers_grukk = [
		"http://image.jeuxvideo.com/stickers/p/1lgg",
		"http://image.jeuxvideo.com/stickers/p/1lgb",
		"http://image.jeuxvideo.com/stickers/p/1lga",
		"http://image.jeuxvideo.com/stickers/p/1lgc",
		"http://image.jeuxvideo.com/stickers/p/1lgd",
		"http://image.jeuxvideo.com/stickers/p/1lge",
		"http://image.jeuxvideo.com/stickers/p/1lgf",
		"http://image.jeuxvideo.com/stickers/p/1lgh"
	];
	var _stickers_bud = [
		"http://image.jeuxvideo.com/stickers/p/zuc",
		"http://image.jeuxvideo.com/stickers/p/zu2",
		"http://image.jeuxvideo.com/stickers/p/zu6",
		"http://image.jeuxvideo.com/stickers/p/zu7",
		"http://image.jeuxvideo.com/stickers/p/zu8",
		"http://image.jeuxvideo.com/stickers/p/zu9",
		"http://image.jeuxvideo.com/stickers/p/zua",
		"http://image.jeuxvideo.com/stickers/p/zub",
		"http://image.jeuxvideo.com/stickers/p/1f88",
		"http://image.jeuxvideo.com/stickers/p/1f89",
		"http://image.jeuxvideo.com/stickers/p/1f8a",
		"http://image.jeuxvideo.com/stickers/p/1f8b",
		"http://image.jeuxvideo.com/stickers/p/1f8d",
		"http://image.jeuxvideo.com/stickers/p/1f8e",
		"http://image.jeuxvideo.com/stickers/p/1f8f"
	];

	// Favoris
	//GM_setValue("st-fav", "");
	var _stickers_fav = [];
	if (GM_getValue("st-fav")) {
		_stickers_fav = JSON.parse(GM_getValue("st-fav"));
	}

	// FUNCTIONS
	function getCode(element) {
		return element.attr("src").split('/')[5];
	}

	function darkJVC() {
		if ( $("body").css("background-color") == "rgb(18, 18, 18)" ) {
			return true;
		} else {
			return false;
		}
	}

	function idTextarea() {
		if (window.location.href.indexOf("messages-prives/message.php?") > -1) {
			return "#message";
		} else {
			return "#message_topic";
		}
	}

	// MAIN APPEND
	var newStickers = '<div style="position: relative">\
                        <div id="fav" class="new-stickers"></div>\
                        <div id="hap" class="new-stickers"></div>\
                        <div id="noel" class="new-stickers"></div>\
                        <div id="brid" class="new-stickers"></div>\
                        <div id="rex" class="new-stickers"></div>\
                        <div id="grukk" class="new-stickers"></div>\
                        <div id="bud" class="new-stickers"></div>\
                      </div>';
	$(newStickers).insertAfter('.jv-editor-toolbar');

	if (_stickers_fav.length < 1) {
		$("#fav").append('<p id="aucunFav">Vous n\'avez aucun sticker dans vos favoris.</p>');
		currentDiv = "hap";
	} // Check s'il y a des fav

	// HIDES
	$(".new-stickers").each(function() {
		if ( $(this).attr("id") != currentDiv ) {
			$(this).hide();
		}
	});

	// APPENDS
	$('<div class="pull-right jv-editor-option-on-off"><span class="script-title">StickersJVC</span> <button id="active-script" type="button" class="btn-on-off active" autocomplete="off"></button></div>').insertAfter(".option-previsu");
	$(".new-stickers").append('<div code="fav" id="fav-css" title="Favoris" class="cat-stickers"></div>\
                               <div code="hap" id="hap-css" title="Hap & autres" class="cat-stickers"></div>\
                               <div code="noel" id="noel-css" title="Noel" class="cat-stickers"></div>\
                               <div code="brid" id="brid-css" title="Bridgely" class="cat-stickers"></div>\
                               <div code="rex" id="rex-css" title="Rex ryder" class="cat-stickers"></div>\
                               <div code="grukk" id="grukk-css" title="Grukk" class="cat-stickers"></div>\
                               <div code="bud" id="bud-css" title="Bud" class="cat-stickers"></div>');

	// AJOUT STICKERS
	for (var i = 0; i < _stickers_hap.length; i++) {
		$(".new-stickers#hap").append( '<img src="'+_stickers_hap[i]+'" class="stickers-script">' );
	}
	for (var k = 0; k < _stickers_noel.length; k++) {
		$(".new-stickers#noel").append( '<img src="'+_stickers_noel[k]+'" class="stickers-script">' );
	}
	for (var n = 0; n < _stickers_brid.length; n++) {
		$(".new-stickers#brid").append( '<img src="'+_stickers_brid[n]+'" class="stickers-script">' );
	}
	for (var j = 0; j < _stickers_rex.length; j++) {
		$(".new-stickers#rex").append( '<img src="'+_stickers_rex[j]+'" class="stickers-script">' );
	}
	for (var gr = 0; gr < _stickers_grukk.length; gr++) {
		$(".new-stickers#grukk").append( '<img src="'+_stickers_grukk[gr]+'" class="stickers-script">' );
	}
	for (var bd = 0; bd < _stickers_bud.length; bd++) {
		$(".new-stickers#bud").append( '<img src="'+_stickers_bud[bd]+'" class="stickers-script">' );
	}
	for (var fv = 0; fv < _stickers_fav.length; fv++) {
		$(".new-stickers#fav").append( '<img src="'+_stickers_fav[fv]+'" class="stickers-script sticker-fav">' );
	}


	// Context menu
	var context = $('.new-stickers').nuContextMenu({

		hideAfterClick: true,

		items: '.stickers-script.sticker-fav',

		callback: function(key, element) {
			if ( key == 'del' ) {
				$(element).remove();
				var index = _stickers_fav.indexOf( $(element).attr("src") );
				if (index > -1) {
					_stickers_fav.splice(index, 1);
					if (_stickers_fav.length < 1) {
						$("#fav").append('<p id="aucunFav">Vous n\'avez aucun sticker dans vos favoris.</p>');
					}
					GM_setValue("st-fav", JSON.stringify(_stickers_fav));
				}
			}
		},

		menu: [
			{
				name: 'del',
				title: 'Supprimer',
				icon: 'remove',
			},
		]

	});

	var context2 = $('body').nuContextMenu({

		hideAfterClick: true,

		items: '.stickers-script:not(.sticker-fav)',

		callback: function(key, element) {
			if ( key == 'add' ) {
				var n = $(element).attr("src");
				if (_stickers_fav.indexOf(n) > -1) {
					// Le sticker est déjà en fav
				} else {
					if ($("#aucunFav").length > 0) { $("#aucunFav").remove(); }
					$("#fav").append('<img src="'+n+'" class="stickers-script sticker-fav">');
					$(".stickers-script").off("click"); // Destroy click event stickers
					stickersEvent();
					_stickers_fav.push(n);
					// Store fav
					GM_setValue("st-fav", JSON.stringify(_stickers_fav));
				}
			}
		},

		menu: [
			{
				name: 'add',
				title: 'Ajouter aux favoris',
				icon: 'plus',
			},
		]

	});

	// LISTENERS
	function stickersEvent() {
		$(".stickers-script").click(function() {
			// Get sticker code
			var code = getCode( $(this) );
			//$("#message_topic").val($("#message_topic").val() + " [[sticker:p/"+code+"]]");
			var $textarea = jQuery(idTextarea());
			var caretPos = $textarea[0].selectionStart;
			var textAreaTxt = $textarea.val();

			var sticker = "[[sticker:p/"+code+"]] ";

			$textarea.val(textAreaTxt.substring(0, caretPos) + sticker + textAreaTxt.substring(caretPos) );
			$textarea.focus();
		});
	}
	stickersEvent();

	$(".cat-stickers").click(function() {
		var id = $(this).attr("code");
		$(".new-stickers").each(function() {
			if ( $(this).attr("id") == id ) {
				$(this).show();
				if (currentDiv != id) {
					$("#"+currentDiv).hide();
					currentDiv = id;
				}
			}
		});
	});

	// NICE SCROLL
	var lastScrollTop = 0;
	var ft_up         = false;
	var ft_down       = false;
	$(".new-stickers").scroll(function() {
		//console.log($(this).scrollTop());
		var st = $(this).scrollTop();
		//console.log(st);
		if (st > lastScrollTop) {
			if (!ft_up) {
				if (st < 50) {
					ft_up = true;
					$(this).scrollTop(50);
				}
			}
		} else {
			if (!ft_down) {
				if (st > 50) {
					ft_down = true;
					$(this).scrollTop(50);
				}
			}
		}
		lastScrollTop = st;

		// Taille div
		if ($(this).scrollTop() != 0) { // Pas en haut de la div
			$(".cat-stickers").hide(55);
		} else {
			 // En haut de la div
			$(".cat-stickers").show(55);
			ft_up   = false;
			ft_down = false;
		}
		if ( $(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight ) {
			// En bas de la div
			ft_up   = false;
			ft_down = false;
		}
	});

	// Active script
	$("#active-script").click(function() {
		if ( $(".new-stickers").is(":visible") ) {
			$(this).removeClass("active");
			$(".new-stickers#"+currentDiv).hide(80);
		} else {
			$(this).addClass("active");
			$(".new-stickers#"+currentDiv).show(80);
			$(".new-stickers").css("overflow", "auto");
		}
	});

	// TOOLTIPS
	$(document).ready(function() {
		$(".cat-stickers").each(function() {
			$(this).tipso({
				delay: 0,
				speed: 120,
				background: "rgba(0, 0, 0, 0.7)",
				size: 'tiny',
				content: '<b>'+$(this).attr("title")+'</b>',
				width: null,
				maxWidth: "150px"
			});
		});
	});

	// CSS
	//GM_addStyle(".new-stickers:hover { background-color: #f9f9f9; }");
	// Scrollbar
	if (!$.browser.mozilla) {
		GM_addStyle(".new-stickers::-webkit-scrollbar-track { -webkit-box-shadow: inset 0 0 3px rgba(0,0,0,0.2); background-color: #f7f7f7; }\
                     .new-stickers::-webkit-scrollbar { width: 9px; background-color: #F5F5F5; }\
                     .new-stickers::-webkit-scrollbar-thumb { background-color: #ccc; }");
	}

	GM_addStyle(".stickers-script { height: 50px; width: 50px; cursor: pointer; padding: 2px; }");

	GM_addStyle(".cat-stickers:hover { border: none; ");
	GM_addStyle(".cat-stickers#fav-css { left: 5px; background-image: url('https://i.imgur.com/sS6w0hg.png'); }\
				 .cat-stickers#hap-css { left: 30px; background-image: url('https://i.imgur.com/QiaEFm9.png'); }\
                 .cat-stickers#noel-css { left: 55px; background-image: url('https://i.imgur.com/xrdqIkA.png'); }\
                 .cat-stickers#brid-css { left: 80px; background-image: url('https://i.imgur.com/Y2X5sc9.png'); }\
                 .cat-stickers#rex-css { left: 105px; background-image: url('https://i.imgur.com/hc1pY1o.png'); }\
                 .cat-stickers#grukk-css { left: 130px; background-image: url('https://i.imgur.com/raQso5Z.png'); }\
                 .cat-stickers#bud-css { left: 155px; background-image: url('https://i.imgur.com/bYjXcMU.png'); }");
	if (darkJVC()) {
		GM_addStyle(".input-on-off:checked ~ .btn-on-off, .btn-on-off.active, .btn-on-off.label-support.active-mach-version { background: url('https://jvcpremium.000webhostapp.com/files/on-off-dark.svg') 26px top!important; }");
		// Script title
		GM_addStyle(".script-title { font-family: 'robotoboldcondensed' ,Arial,Helvetica,sans-serif; text-transform: uppercase; font-size: 0.75rem; color: #d4d4d4 }");
		// Stickers panel
		GM_addStyle(".new-stickers { border-top: 1px solid #555; padding: 2px; margin-top: 8px; height: 75px; transition: background-color 0.1s; background-color: #333; overflow: auto; text-align: center; padding-top: 10px; scroll-behavior: smooth; }");
		// Catégories
		GM_addStyle(".cat-stickers { position: absolute; top:-8px; background-color: #555; box-shadow: 0px 2px 2px #333 ; border: 1px solid #333; border-radius: 50px; height: 16px; width: 16px; cursor: pointer; background-size: cover; background-repeat: no-repeat; background-position: center center; }");
		// Hover stickers
		GM_addStyle(".stickers-script:hover { background-color: #2d2d2d; border-radius: 3px; }");
		// Hover cat
		GM_addStyle(".cat-stickers:hover { box-shadow: 0px 2px 8px #222; }");
	} else {
		// Script title
		GM_addStyle(".script-title { font-family: 'robotoboldcondensed' ,Arial,Helvetica,sans-serif; text-transform: uppercase; font-size: 0.75rem; color: #656574 }");
		// Stickers panel
		GM_addStyle(".new-stickers { border-top: 1px solid #ccc; padding: 2px; margin-top: 8px; height: 75px; transition: background-color 0.1s; background-color: #f7f7f7; overflow: auto; text-align: center; padding-top: 10px; scroll-behavior: smooth; }");
		// Catégories
		GM_addStyle(".cat-stickers { position: absolute; top:-8px; background-color: white; box-shadow: 0px 2px 2px #e0e0e0 ; border: 1px solid #ccc; border-radius: 50px; height: 16px; width: 16px; cursor: pointer; background-size: cover; background-repeat: no-repeat; background-position: center center; }");
		// Hover stickers
		GM_addStyle(".stickers-script:hover { background-color: #efefef; border-radius: 3px; }");
		// Hover cat
		GM_addStyle(".cat-stickers:hover { box-shadow: 0px 2px 8px #b5b5b5; }");
	}

})();