Greasy Fork is available in English.

Better Related Anime/Manga Reader

Display the Synonyms/Spin-offs/Alternative versions/Sequels/Summary/Others and Side stories titles in a better readable way on anime and manga pages.

Fra 06.05.2021. Se den seneste versjonen.

// ==UserScript==
// @name         Better Related Anime/Manga Reader
// @namespace    ReadeableSequels
// @version      0.3
// @description  Display the Synonyms/Spin-offs/Alternative versions/Sequels/Summary/Others and Side stories titles in a better readable way on anime and manga pages.
// @author       hacker09
// @include      /^https:\/\/myanimelist\.net\/anime\/[\d]+(\/.*)?/
// @include      /^https:\/\/myanimelist\.net\/manga\/[\d]+(\/.*)?/
// @icon         https://www.google.com/s2/favicons?domain=myanimelist.net
// @run-at       document-end
// @grant        GM_listValues
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
  'use strict';
  if (document.body.innerText.search("Synonyms:") > -1) //If the text "Synonyms:" exists on the page
  { //Starts the if condition
    function findTheSynonymsText() { //Create a function to get the Synonyms: Titles text
      const allInfo = [...[...document.querySelectorAll("h2")].find(h2 => h2.textContent === "Information").parentNode.querySelectorAll("div")]; //Select all divs inside the Information h2 element
      return allInfo.find(info => info.innerText.includes("Synonyms:")); //Return the div that contains the Synonyms text
    } //Find the Synonyms text that's inside the information h2 element

    if (findTheSynonymsText().querySelector("span").nextSibling.textContent.match(', ') !== null) //If there's any commas on the Synonyms titles
    { //Starts the if condition
      var animeid = location.pathname.match(/\d+/)[0]; //Detect the anime id
      var StoredAnimeIdsArray = []; //Creates a new blank array
      GM_listValues().forEach(a => StoredAnimeIdsArray.push('^' + a)); //Add all anime IDs on tampermonkey to the array
      var StoredAnimeIdsRegex = new RegExp(StoredAnimeIdsArray.join('$|')); //Create a new variable and regex containing all the values saved on tampermonkey and replace the , separator with the or $| regex symbols

      async function GetSynonyms() //Creates a function to get the Synonyms titles
      { //Starts the function
        var ID; //Creates a new global variable
        if (window.location.pathname.split('/')[1] === 'anime') //If the opened url is an anime entry page
        { //Starts the if condition
          ID = 'aid'; //Add a value to the variable
        } //Finishes the if condition
        else //If the opened url is an manga entry page
        { //Starts the else condition
          ID = 'mid'; //Add a value to the variable
        } //Finishes the else condition

        const response = await fetch('https://myanimelist.net/dbchanges.php?' + ID + '=' + location.pathname.match(/\d+/)[0] + '&t=alternative_titles'); //Fetch
        const html = await response.text(); //Gets the fetch response
        const newDocument = new DOMParser().parseFromString(html, 'text/html'); //Parses the fetch response
        var SynonymsTitles = newDocument.querySelector("td.dark_text").nextElementSibling.textContent.trim().split(';'); //Creates a variable to hold the Synonyms Titles

        var SynonymsTitlesArray = []; //Create a new global array to store all Synonyms Titles later
        SynonymsTitles.forEach(a => SynonymsTitlesArray.push(a)); //Add a break line between each anime/manga title
        findTheSynonymsText().innerHTML = '<span class="dark_text" style="cursor: pointer;">Synonyms:</span><div>' + SynonymsTitlesArray.join('<br><br>') + '</div>'; //Add the Synonyms Titles with break lines to the page
        GM_setValue(animeid, SynonymsTitlesArray.join('<br><br>')); //Get and save the anime id and  Synonyms Titles with break lines as a variable
      } //Finishes the async function

      if (animeid.match(StoredAnimeIdsRegex) !== null && StoredAnimeIdsRegex.toLocaleString() !== '/(?:)/') //If the current url anime id matches an anime id that is stored on tampermonkey, and if the Regex contains 1 or more anime ids
      { //Starts the if condition

        findTheSynonymsText().querySelector("span").nextSibling.textContent = ''; //Remove the old Synonyms Titles text
        findTheSynonymsText().innerHTML = '<span class="dark_text" style="cursor: pointer;">Synonyms:</span><div>' + GM_getValue(animeid) + '</div>'; //Add the Synonyms Titles text content on the Synonyms: bold text

        findTheSynonymsText().querySelector("span").onclick = function() { //When the bold Synonyms: text is clicked
          GetSynonyms(); //Update the Synonyms Titles
        }; //Finishes the onclick advent listener
        findTheSynonymsText().querySelector("span").style.cursor = 'pointer'; //Make the bold Synonyms: text look like it's clickable

      } //Finishes the if condition
      else //If the current url anime id does NOT match any anime id that is stored on tampermonkey
      { //Starts the else condition
        GetSynonyms(); //Starts the function
      } //Finishes the else condition

    } //Finishes the if condition

  } //Finishes the if condition

  if (document.querySelector("table.anime_detail_related_anime").innerText.match(', ') !== null) //If there are titles separated by commas
  { //Starts the if condition
    document.querySelector("table.anime_detail_related_anime").outerHTML = document.querySelector("table.anime_detail_related_anime").outerHTML.replaceAll(', ', ''); //Remove the now needless commas

    document.querySelector("table.anime_detail_related_anime").querySelectorAll("a[href*='anime'],a[href*='manga']").forEach(a => a.outerHTML = a.outerHTML + '<br>'); //Add a break line between each anime/manga title
  } //Finishes the if condition

})();