Telegram Unicode First Char Fix

Revives lost first Unicode char + appends full word (without first letter) if Unikey mod char used

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Telegram Unicode First Char Fix
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Revives lost first Unicode char + appends full word (without first letter) if Unikey mod char used
// @match        https://web.telegram.org/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const isUnicode = (char) => char && char.charCodeAt(0) > 127;
    const isWeirdChar = (char) => char === '·' || char === '`';

    let lastChar = '';
    let watching = false;
    let cachedWord = '';
    let hasUnikeyMod = false;

    window.addEventListener('beforeinput', (e) => {
        const input = document.querySelector('div.input-message-input');
        if (!input) return;

        const content = input.textContent || '';

        // ✅ Store cached word when weird Unikey mark is typed
        if (isWeirdChar(e.data)) {
            const words = content.trim().split(/\s+/).filter(Boolean);
            if (words.length === 1) {
                cachedWord = words[0];
                hasUnikeyMod = true;
                //console.log('💾 Cached word:', JSON.stringify(cachedWord));
            }
            return;
        }

        // 🛠️ Only care about first unicode char AND empty box
        if (!e.data || content.length > 0) return;

        if (isUnicode(e.data)) {
            lastChar = e.data;
            //console.log('🔍 First char is unicode:', lastChar);

            if (watching) return;
            watching = true;

            const check = setInterval(() => {
                if (!input.isConnected) {
                    clearInterval(check);
                    watching = false;
                    return;
                }

                if ((input.textContent || '') === '') {
                    let finalText = lastChar;

                    if (hasUnikeyMod && cachedWord.length > 1) {
                        finalText += cachedWord.slice(1); // drop first char
                        //console.log('🧬 Appended from cache:', JSON.stringify(cachedWord.slice(1)));
                    }

                    input.textContent = finalText;
                    input.dispatchEvent(new InputEvent('input', { bubbles: true }));

                    const sel = window.getSelection();
                    sel.collapse(input.lastChild || input, input.textContent.length);

                    // 🔁 Reset flags
                    cachedWord = '';
                    hasUnikeyMod = false;
                    watching = false;
                    clearInterval(check);
                } else {
                    watching = false;
                    clearInterval(check);
                }
            }, 10);
        }
    });
})();