Telegram markup extension

presented below

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Telegram markup extension
// @namespace    https://gist.github.com/W-1/
// @version      0.2
// @description  presented below
// @author       W-1
// @match        *://web.telegram.org/
// @grant        none
// ==/UserScript==

/*
*
*    [_text_]   => t̲e̲x̲t̲
*    [-text-]   => ̶t̶e̶x̶t̶
*  [_[-text-]_] => ̶̶̲t̶̶̲e̶̶̲x̶̶̲t̶̲
*    [^text^]   => ᴛᴇxᴛ
*    [@text@]   => ????
*    [|text|]   => ????
*    [$text$]   => ????
*    [=text=]   => ????
*    [%text%]   => text
*
*/

document.querySelector('.composer_rich_textarea').onkeydown = e => {
  if(e.keyCode!==13)
    return;
  
  const abc = ['abcdefghijklmnopqrstuvwxyz'],
        abcI = e => abc.join('').indexOf(e),
        mapjoin = (s,f) => [...s].map(l=>f(l,abcI(l))).join('');
  abc.push(abc[0].toUpperCase());

  const cmd = [
    ['_','̲'],
    ['-','̶'],
    ['^','ᴀʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ'],
    ['@','??????????????????????????'],
    ['|','????????????????????????????ℂ????ℍ?????ℕ?ℙℚℝ???????ℤ'],
    ['$','????????????????????????????????????????????????????'],
    ['=','????????????????????????????????????????????????????'],
    ['%','abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ']
  ];
  
  let text = e.target.innerText;
  
  for (let [r,s] of cmd){
    if(!(new RegExp(`\\[\\${r}.*\\${r}\\]`).test(text)))
      continue;
    
    let t = text.split(new RegExp(`\\[\\${r}|\\${r}\\]`));
    
    text = t[0];
    
    switch(r){
      case '^': text += mapjoin(t[1], (l,i) => i!==-1&&i<26?s[i]:l);
        break;
      case '@': text += mapjoin(t[1], (l,i) => i!==-1?[...s][i<26?i:i-26]:l);
        break;
      case '-':
      case '_': text += mapjoin(t[1], l => ['[',']',...cmd.map(c=>c[0])].indexOf(l)!==-1?l:s+l)+s;
        break;
      default: text += mapjoin(t[1], (l,i) => i!==-1?[...s][i]:l);
    }
    
    text += t[2];
  }
  
  e.target.innerText=text;
  return true;
}