MyAnimeList(MAL) - BBCODE Editor Character Counter

This script will add a character counter to the MAL BBCODE Editor.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το 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         MyAnimeList(MAL) - BBCODE Editor Character Counter
// @version      1.0.4
// @description  This script will add a character counter to the MAL BBCODE Editor.
// @author       Cpt_mathix
// @match        https://myanimelist.net/*
// @grant        none
// @run-at document-body
// @namespace    https://greasyfork.org/users/16080
// ==/UserScript==

init();

function init() {
    const observer = new MutationObserver((mutations) => {
        for (const mutation of mutations) {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.ELEMENT_NODE && node.classList.contains('sceditor-outer')) {
                        var tabs = node.querySelector(".sceditor-tabs");
                        var textarea = node.querySelector("textarea");

                        tabs.insertAdjacentHTML("beforeend", `<li class="character-counter" style="margin-left: auto;">Character count: ${getCharCount(textarea)}</li>`);

                        textarea.addEventListener("input", (event) => {
                            var counter = event.target.closest(".sceditor-outer").querySelector(".character-counter");
                            counter.innerHTML = "Character count: " + getCharCount(event.target);
                        });
                    }
                });
            }
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });

    var interestStackTextArea = document.getElementById("stack_description");
    if (interestStackTextArea) {
        interestStackTextArea.previousElementSibling.insertAdjacentHTML("beforeend", '<small class="character-counter" style="float: right;padding-top: 10px;">Character count: 0</small>');
        interestStackTextArea.addEventListener("input", (event) => {
            var counter = event.target.previousElementSibling.querySelector(".character-counter");
            counter.innerHTML = "Character count: " + getCharCount(event.target);
        });
    }
}

function getCharCount(textarea) {
    return textarea.value.length || 0;
}