Benissou / JVMorse

// ==UserScript==
// @name        JVMorse
// @author       Benissou
// @description  Encodez/Décodez le morse
// @include      http://www.jeuxvideo.com/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require      https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.8.0/jquery.modal.min.js
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_notification
// @grant        GM_getResourceText
// @grant        GM_getResourceURL
// @version      0.1.5
// @icon         http://image.noelshack.com/fichiers/2017/08/1487683882-morse.jpg
// @copyright    2016+, Benissou
// ==/UserScript==


/*jshint multistr: true */

var alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
var morse = ['·−',    '−···',  '−·−·', '−··', '·',  '··−·',  '−−·',  '····', '··', '·−−−',  '−·−', '·−··', '−−',    '−·',    '−−−',  '·−−·','−−·−',  '·−·',  '···',  '−','··−',   '···−',  '·−−', '−··−', '−·−−',  '−−··',  '  ', '·−−−−', '··−−−', '···−−',  '····−',  '·····',  '−····', '−−···', '−−−··',  '−−−−·', '−−−−−' ];

(function() {
	
    'use strict';
	
	$('.jv-editor-toolbar').append('<div id="morse" ></div>');	
	$('#morse').append('<div class="btn-group"><button class="btn btn-jv-editor-toolbar" id="char_morse" type="button" title=""><span>·−</span></button></div>');
	document.getElementById('char_morse').addEventListener('click', function() {char_morse();});

	function char_morse ()
	{
		var elem = document.getElementById('message_topic');
		var start = elem.selectionStart;
		var end = elem.selectionEnd;
		var len = elem.value.length;
		var sel_txt = elem.value.substring(start,end);
		elem.value = elem.value.substring(0,start) + encodemorse(sel_txt)+ elem.value.substring(end,len);
	}
	
function cleanUpSpecialChars(str)
{
    str = str.replace(/[ÀÁÂÃÄÅàáâãäå]/g,"a");
	str = str.replace(/[ÈÉÊËèéêë]/g,"e");
	str = str.replace(/[ïîÏÎ]/g,"i");
	str = str.replace(/[Çç]/g,"c");
	return str;
}

	function encodemorse(seltxt) 
	{
		var text = "";
		seltxt = seltxt.toLowerCase();
		seltxt = cleanUpSpecialChars(seltxt);
		
		for (var i =0; i< seltxt.length ; i++)
			{
				for (var m = 0; m < alphabet.length;m++)
				{
					 if (alphabet[m] === seltxt[i]) 
					  {
						  text+=  morse[m] + " ";
					  }
				}
			}
		return text;
	}
	
	
    function decodemorse() 
	{
        var allMsg = document.querySelectorAll('.txt-msg.text-enrichi-forum :not(.blockquote-jv)'); // select tous les msg sauf les blockquotes
		
        for(var i = 0; i < allMsg.length; i++)
        {
			var text = "";
			var msf = allMsg[i].innerText;
			if((msf.indexOf('−') !== -1) || (msf.indexOf('·') !== -1))
			{
				var letter =  msf.split(" ");
				for (var l =0; l< letter.length ; l++)
				{
					for (var m = 0; m < morse.length;m++)
					{
						 if (morse[m] === letter[l]) 
						  {
							  text+=  alphabet[m].toUpperCase();
						  }
					}				
				}
			text+= " ";
			}	
		allMsg[i].innerHTML += '<p style="color:#0088aa;"><b>' + text + '</b></p>';
		}									
	}
    decodemorse();
	
	
})();