Advertisement
ShaggyZE1

MyAnimeList(MAL) - Preview BBCODE with Character Count

Feb 16th, 2025 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         MyAnimeList(MAL) - Preview BBCODE with Character Count
  3. // @version      1.0.4
  4. // @description  This script adds the MAL BBCODE Editor and character count.
  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.     var fancyboxInitObserver = new MutationObserver(function(mutations) {
  16.         mutations.forEach(function(mutation) {
  17.             var fancybox = document.getElementById("fancybox-wrap");
  18.             var fancyboxInner = document.getElementById("fancybox-inner");
  19.             if (fancybox) {
  20.                 fancyboxInitObserver.disconnect();
  21.  
  22.                 var fancyboxObserver = new MutationObserver(function(mutations) {
  23.                     mutations.forEach(function(mutation) {
  24.                         if (fancybox.style.width !== "1000px") {
  25.                             fancybox.style.width = "1000px";
  26.                             fancyboxInner.style.width = "980px";
  27.                         }
  28.                         window.dispatchEvent(new Event('resize'));
  29.                     });
  30.                 });
  31.  
  32.                 fancyboxObserver.observe(fancybox, { attributes: true });
  33.             }
  34.         });
  35.     });
  36.  
  37.     fancyboxInitObserver.observe(document.body, { childList: true });
  38.  
  39.     if (document.location.href.includes("myanimelist.net/clubs")) {
  40.         initClubCommentsPreview();
  41.     }
  42.  
  43.     if (document.location.href.includes("myanimelist.net/blog.php")) {
  44.         initBlogCommentsPreview();
  45.     }
  46.  
  47.     if (document.location.href.includes("myanimelist.net/myblog.php")) {
  48.         initBlogEntryPreview();
  49.     }
  50.  
  51.     if (document.location.href.includes("myanimelist.net/editprofile.php")) {
  52.         initProfileAboutMePreview();
  53.     }
  54.  
  55.     if (document.location.href.includes("myanimelist.net/ownlist/manga")) {
  56.         initEditMangaPreview();
  57.     }
  58.  
  59.     if (document.location.href.includes("myanimelist.net/ownlist/anime")) {
  60.         initEditAnimePreview();
  61.     }
  62.  
  63.     observeBBCodeTextareas();
  64. }
  65.  
  66. function initClubCommentsPreview() {
  67.     addEditor("form.form-club-user-comment textarea");
  68. }
  69.  
  70. function initBlogCommentsPreview() {
  71.     addEditor(".blog_detail_comment_wrapper form textarea");
  72. }
  73.  
  74. function initEditMangaPreview() {
  75.     resizeDialog();
  76.     addEditor("#add_manga_comments");
  77. }
  78.  
  79. function initEditAnimePreview() {
  80.     resizeDialog();
  81.     addEditor("#add_anime_comments");
  82. }
  83.  
  84. function initBlogEntryPreview() {
  85.     resizeDialog();
  86.     addEditor("#blogForm textarea[name=\"entry_text\"]");
  87. }
  88.  
  89. function initProfileAboutMePreview() {
  90.     var textarea = document.querySelector("#content form textarea[name=\"profile_aboutme\"]");
  91.     if (textarea) {
  92.         textarea.classList.add("bbcode-message-editor");
  93.         textarea.insertAdjacentHTML("afterend", "<small><b>You can also preview bbcode with: <a href='https://cptmathix.github.io/MyAnimeList-BBCODE2HTML/'>https://cptmathix.github.io/MyAnimeList-BBCODE2HTML/</a><b></small>");
  94.     }
  95. }
  96.  
  97. function resizeDialog() {
  98.     var dialog = document.getElementById("dialog");
  99.     if (dialog) {
  100.         if (document.location.href.includes("hideLayout=1")){
  101.             var clientWidth = document.body.clientWidth;
  102.             dialog.style.width = clientWidth + "px";
  103.         } else {
  104.             dialog.style.width = "804px";
  105.         }
  106.     }
  107. }
  108.  
  109. function addEditor(selector) {
  110.     var textarea = document.querySelector(selector);
  111.     if (textarea) {
  112.         textarea.classList.add("bbcode-message-editor");
  113.         textarea.rows = 15;
  114.     }
  115. }
  116.  
  117. function addCharacterCount(textarea) {
  118.     var counter = document.createElement("div");
  119.     counter.style.fontSize = "small";
  120.     counter.style.color = "#888";
  121.     counter.textContent = "Characters: " + textarea.value.length;
  122.     counter.classList.add("character-count-display");
  123.     textarea.parentNode.insertBefore(counter, textarea.nextSibling);
  124.     textarea.addEventListener("input", function() {
  125.         counter.textContent = "Characters: " + textarea.value.length;
  126.     });
  127. }
  128.  
  129. function observeBBCodeTextareas() {
  130.     var sceditorInitObserver = new MutationObserver(function(mutations) {
  131.         mutations.forEach(function(mutation) {
  132.             mutation.addedNodes.forEach(function(node) {
  133.                 if (node.nodeType === Node.ELEMENT_NODE) {
  134.                     var textareas = node.querySelectorAll(".sceditor-container textarea");
  135.                     textareas.forEach(function(textarea) {
  136.                         if (!textarea.nextElementSibling || !textarea.nextElementSibling.classList.contains('character-count-display')) {
  137.                             addCharacterCount(textarea);
  138.                         }
  139.                     });
  140.                 }
  141.             });
  142.         });
  143.     });
  144.  
  145.     sceditorInitObserver.observe(document.body, { childList: true, subtree: true });
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement