MyAnimeList(MAL) - BBCODE Editor Character Counter

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

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

(I already have a user script manager, let me install it!)

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.

(I already have a user style manager, let me install it!)

// ==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;
}