MyAnimeList(MAL) - BBCODE Editor Character Counter

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

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

You will need to install an extension such as Tampermonkey to install this 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         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;
}