MyAnimeList(MAL) - BBCODE Editor Character Counter

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

目前为 2025-02-20 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name MyAnimeList(MAL) - BBCODE Editor Character Counter
  3. // @version 1.0.3
  4. // @description This script will add a character counter to the MAL BBCODE Editor.
  5. // @author Cpt_mathix
  6. // @match https://myanimelist.net/*
  7. // @grant none
  8. // @run-at document-body
  9. // @namespace https://greasyfork.org/users/16080
  10. // ==/UserScript==
  11.  
  12. init();
  13.  
  14. function init() {
  15. const observer = new MutationObserver((mutations) => {
  16. for (const mutation of mutations) {
  17. if (mutation.type === 'childList') {
  18. mutation.addedNodes.forEach(node => {
  19. if (node.nodeType === Node.ELEMENT_NODE && node.classList.contains('sceditor-outer')) {
  20. var tabs = node.querySelector(".sceditor-tabs");
  21. var textarea = node.querySelector("textarea");
  22.  
  23. tabs.insertAdjacentHTML("beforeend", `<li class="character-counter" style="margin-left: auto;">Character count: ${getCharCount(textarea)}</li>`);
  24.  
  25. textarea.addEventListener("input", (event) => {
  26. var counter = event.target.closest(".sceditor-outer").querySelector(".character-counter");
  27. counter.innerHTML = "Character count: " + getCharCount(event.target);
  28. });
  29. }
  30. });
  31. }
  32. }
  33. });
  34.  
  35. observer.observe(document.body, { childList: true, subtree: true });
  36.  
  37. var interestStackTextArea = document.getElementById("stack_description");
  38. interestStackTextArea.previousElementSibling.insertAdjacentHTML("beforeend", '<small class="character-counter" style="float: right;padding-top: 10px;">Character count: 0</small>');
  39. interestStackTextArea.addEventListener("input", (event) => {
  40. var counter = event.target.previousElementSibling.querySelector(".character-counter");
  41. counter.innerHTML = "Character count: " + getCharCount(event.target);
  42. });
  43. }
  44.  
  45. function getCharCount(textarea) {
  46. return textarea.value.length || 0;
  47. }