Advertisement
ShaggyZE1

MyAnimeList(MAL) - Preview BBCODE

Feb 16th, 2025 (edited)
103
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.3
  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.         return;
  42.     }
  43.  
  44.     if (document.location.href.includes("myanimelist.net/blog.php")) {
  45.         initBlogCommentsPreview();
  46.         return;
  47.     }
  48.  
  49.     if (document.location.href.includes("myanimelist.net/myblog.php")) {
  50.         initBlogEntryPreview();
  51.         return;
  52.     }
  53.  
  54.     if (document.location.href.includes("myanimelist.net/editprofile.php")) {
  55.         initProfileAboutMePreview();
  56.         return;
  57.     }
  58.  
  59.     if (document.location.href.includes("myanimelist.net/ownlist/manga")) {
  60.         initEditMangaPreview();
  61.         return;
  62.     }
  63.  
  64.     if (document.location.href.includes("myanimelist.net/ownlist/anime")) {
  65.         initEditAnimePreview();
  66.         return;
  67.     }
  68. }
  69.  
  70. function initClubCommentsPreview() {
  71.     addEditorAndCounter("form.form-club-user-comment textarea");
  72. }
  73.  
  74. function initBlogCommentsPreview() {
  75.     addEditorAndCounter(".blog_detail_comment_wrapper form textarea");
  76. }
  77.  
  78. function initEditMangaPreview() {
  79.     resizeDialog();
  80.     addEditorAndCounter("#add_manga_comments");
  81. }
  82.  
  83. function initEditAnimePreview() {
  84.     resizeDialog();
  85.     addEditorAndCounter("#add_anime_comments");
  86. }
  87.  
  88. function initBlogEntryPreview() {
  89.     resizeDialog();
  90.     addEditorAndCounter("#blogForm textarea[name=\"entry_text\"]");
  91. }
  92.  
  93. function initProfileAboutMePreview() {
  94.     var textarea = document.querySelector("#content form textarea[name=\"profile_aboutme\"]");
  95.     if (textarea) {
  96.         textarea.classList.add("bbcode-message-editor");
  97.         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>");
  98.         addCharacterCount(textarea);
  99.     }
  100. }
  101.  
  102. function resizeDialog() {
  103.     var dialog = document.getElementById("dialog");
  104.     if (dialog) {
  105.         if (document.location.href.includes("hideLayout=1")){
  106.             var clientWidth = document.body.clientWidth;
  107.             dialog.style.width = clientWidth + "px";
  108.         } else {
  109.             dialog.style.width = "804px";
  110.         }
  111.     }
  112. }
  113.  
  114. function addEditorAndCounter(selector) {
  115.     var textarea = document.querySelector(selector);
  116.     if (textarea) {
  117.         textarea.classList.add("bbcode-message-editor");
  118.         textarea.rows = 15;
  119.         addCharacterCount(textarea);
  120.     }
  121. }
  122.  
  123. function addCharacterCount(textarea) {
  124.     var counter = document.createElement("div");
  125.     counter.style.fontSize = "small";
  126.     counter.style.color = "#888";
  127.     counter.textContent = "Characters: " + textarea.value.length;
  128.     textarea.parentNode.insertBefore(counter, textarea.nextSibling);
  129.     textarea.addEventListener("input", function() {
  130.         counter.textContent = "Characters: " + textarea.value.length;
  131.     });
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement