Generate a List With The Animes/Mangas Titles that were Re-Watched/Re-Read

This is a tool to easily and quickly generate a list with the titles of what animes/mangas you have ReWatched/ReRead and how many times.

Versión del día 13/08/2020. Echa un vistazo a la versión más reciente.

  1. // ==UserScript==
  2. // @name Generate a List With The Animes/Mangas Titles that were Re-Watched/Re-Read
  3. // @namespace MAL Automatic Anime/Manga List Generator
  4. // @version 0.3
  5. // @description This is a tool to easily and quickly generate a list with the titles of what animes/mangas you have ReWatched/ReRead and how many times.
  6. // @author hacker09 & (Reddit user SapphireFeast)
  7. // @match https://myanimelist.net/animelist/*
  8. // @match https://myanimelist.net/mangalist/*
  9. // @grant none
  10. // @run-at document-end
  11. // ==/UserScript==
  12. (function() {
  13. 'use strict';
  14. var $ = window.jQuery; //Defines That The Symbol $ Is A jQuery
  15.  
  16. var rewatchedlistbtn = document.createElement("a"); // Creates an a element
  17. rewatchedlistbtn.setAttribute("id", "rewatchedlistbtn"); //Adds the id rewatchedlistbtn to the a element
  18. rewatchedlistbtn.setAttribute("style", "margin-left: 10px;transform: scale(1.8);vertical-align: top;margin-top: 7px;", "id", "rewatchedlistbtn"); //Adds the css to the a element
  19. if (window.location.pathname.split('/')[1] === 'animelist') //Check If The URL Is https://myanimelist.net/animelist/ ,And If Yes Make A Button Called "Generate My ReWatched List"
  20. {
  21. rewatchedlistbtn.innerHTML = "Generate ReWatched List";
  22. } // The text that should appear on the "Button"
  23. if (window.location.pathname.split('/')[1] === 'mangalist') //Check If The URL Is https://myanimelist.net/animelist/ ,And If Yes Make A Button Called "Generate My ReRead List"
  24. {
  25. rewatchedlistbtn.innerHTML = "Generate ReRead List"; // The text that should appear on the "Button"
  26. } //Finishes the if condition
  27. if (document.querySelector("#advanced-options-button") === null) //Checks if the Filters button on the modern list style exists,if not then the user is using an old classic list style
  28. {
  29. document.querySelector("a.table_headerLink").parentElement.appendChild(rewatchedlistbtn); //Defines that the 'Generate ReWatched/ReRead List' button should appear close to the 'Anime Title' or 'Manga Title' text on the old classic style list.
  30. document.querySelector("#rewatchedlistbtn").onclick = function() {
  31. scrape();
  32. }; //Shows a message in the console for dev purposes, and run the scrape function.Classic list styles doesn't need to be scrolled down.
  33. document.querySelector("#rewatchedlistbtn").setAttribute("style", "cursor: pointer;"); //Set the css for the button
  34. } //Finishes the if condition
  35. else //If the Filters button on the modern list style exists, then the user is using the modern list style
  36. {
  37. var bottomrewatchedlistbtn = document.createElement("a"); // Creates an a element
  38. bottomrewatchedlistbtn.setAttribute("id", "bottomrewatchedlistbtn"); //Adds the id bottomrewatchedlistbtn to the a element
  39. bottomrewatchedlistbtn.setAttribute("style", "cursor: pointer;margin-left: 10px;transform: scale(1.8);vertical-align: top;margin-top: 7px;font-size: 20px;", "id", "rewatchedlistbtn"); //Adds the css to the a element
  40. bottomrewatchedlistbtn.innerHTML = "Generate ReWatched List"; // The text that should appear on the "Button"
  41. document.querySelector("body > footer").parentElement.appendChild(bottomrewatchedlistbtn); //Defines that the 'Generate ReWatched/ReRead List' button should appear close to the Footer on the modern style list
  42. document.querySelector("#bottomrewatchedlistbtn").onclick = function() //Detects the mouse click on the 'Generate ReWatched/ReRead List' button
  43. {
  44. scrape(); //Start the scrape function
  45. };
  46. document.querySelector("#advanced-options-button").parentElement.appendChild(rewatchedlistbtn); //Defines that the 'Generate ReWatched/ReRead List' button should appear close to the Filter button on the modern style list
  47. document.querySelector("#rewatchedlistbtn").setAttribute("style", "cursor: pointer;"); //Set the css for the button
  48. document.querySelector("#rewatchedlistbtn").onclick = function() // Detects the mouse click on the 'Generate ReWatched/ReRead List' button
  49. {
  50. console.log('Starting To Scroll Down'); //Shows a message in the console for dev purposes
  51. godown(); //Run the godown function
  52. var interval = setInterval(godown, 10000); //Creates a variable named interval that will run the function godown every 10 secs
  53. interval; //Run the variable named interval to start running the godown function and the timer
  54. }; //Finishes the onclick function
  55. } //Finishes the else statement
  56.  
  57. function godown() //Function that automatically "Press the keyboard key End"
  58. {
  59. check(); //Check if the user is on the completed anime/manga page
  60. if (window.innerHeight + window.scrollY >= document.body.scrollHeight === false) //If condition that detects if the website is scrolled 100% at the bottom or not, and calculates how much the page needs to be scrolled to reach the bottom
  61. {
  62. window.scrollTo(0, document.body.scrollHeight); //Scrolls the website till the page reaches the bottom
  63. console.log('Page Bottom Reached, Re-Checking in 10 secs'); //Shows a message in the console for dev purposes
  64. } else //When the page bottom is reached
  65. {
  66. console.log('Full List Loaded! Stopping Scrolling Down Now!'); //Shows a message in the console for dev purposes
  67. //console.log('Stopping the timer that scrolls the page down'); //Shows a message in the console for dev purposes. clearInterval(interval); //Breaks the timer that scrolls the page down every 10 secs
  68. scrape(); //Run the Scrapping Function
  69. return; //Get out of this function
  70. } //Finishes the else condition
  71. } //Finishes the function godown
  72.  
  73. function check() //Function to check if the user is on the completed anime/manga page
  74. {
  75. var username = window.location.pathname.split('/')[2]; //Get the username on the url to use latter
  76.  
  77. // Before running, check if the completed section is opened or not
  78. if (window.location.href.split('/')[4] !== '' + username + '?status=2') //Checks if the user is on https://myanimelist.net/animelist/USERNAMEHERE?status=2 opened or not (the status 2 means that the user is on an completed list)
  79. {
  80. alert("Execute this on the 'Completed' page! \nRedirecting. \nTry again after the page loads."); //Show an error alert message to the user, if the user is not on an completed list
  81. window.location.replace(window.location.href.split('?')[0] + "?status=2"); //Redirects the user to the completed section of the list that the user was in
  82. throw new Error("Redirecting"); //Show an error alert message on the dev console of the user
  83. } //Finishes the if statement
  84. } //Finishes the function check
  85.  
  86. function scrape() //Function that will scrape the page for rewatched/reread values
  87. {
  88. check(); //Check if the user is on the completed anime/manga page
  89. console.log('Starting To Scrape...Please Wait!');
  90. var type = ""; //Null variable that will be used to declare later what is the type that will be scrapped (if it's anime or manga)
  91. var titles_old = document.querySelectorAll('div table tbody tr a.animetitle span'); //Select only the anime title on the old style list
  92. var titles_new = document.querySelectorAll('tbody.list-item tr.list-table-data td.data.title a.link.sort'); //Select only the anime title on the Modern default style list
  93. var titles = []; //Creates a blank node variable to use latter
  94. var old_list = false; //Variable that can be changed latter to the value 'true' if the user used the script on an old classic style list.The value 'false' will be kept if the user used the script on the new modern list style.
  95. var username = window.location.pathname.split('/')[2]; //Get the username on the url to use latter
  96.  
  97. if (window.location.pathname.split('/')[1]) //Detects if the user is on an animelist or not
  98. {
  99. type = "anime"; //Set the type as anime
  100. } //If the user is on an animelist, then the type to be scrapped will be animes
  101. else //Detects if the user is not on an animelist
  102. {
  103. type = "manga"; //The user is on an manga list, then the type to be scrapped will be mangas
  104. } //Finishes the else condition
  105.  
  106. if (titles_old.length > titles_new.length) //Checks if the user list style is the old classic style or the new modern style
  107. {
  108. titles = titles_old; //If the user used the script on an old classic list style, the blank node created later named 'titles' will be changed to an variable named 'titles_old' that will posses the blank nodes value (In dev words var titles_old = []; )
  109. old_list = true; ////Variable old_list will be changed to the value 'true' if the user used the script on an old classic style list
  110. } else //If the user used the script on a new modern list style
  111. {
  112. for (var i = 0; i < titles_new.length; i++) //This for condition is responsible for getting all the anime titles
  113. {
  114. titles[i] = titles_new[i].innerHTML; //This for condition is responsible for getting all the anime titles
  115. } //Finishes the for condition
  116. } //Finishes the if statement
  117.  
  118. var rewatches = []; //Creates a blank node variable to use latter
  119. var moreLinks = document.querySelectorAll('a'); //Defines a variable named 'moreLinks' that will be used to click on all the more buttons on the completed page
  120. var resultArray = []; //Creates a blank node variable to use latter
  121. var result = 'data:text/html;charset=utf-8,<style>html,body{margin: 0;padding: 0;}</style><div style="max-width:650px;font-size: 18px;margin:0px auto;">'; //The HTML and CSS that will be added to the new browser tab when the script is done
  122.  
  123. if (type == "anime") result += "<h1> " + username + " ReWatched Anime List</h1>"; //If the type is anime then add 'Rewatched anime' to The HTML and CSS that will be added to the new browser tab when the script is done
  124. if (type == "manga") result += "<h1> " + username + " ReRead Manga List</h1>"; //If the type is manga then add 'Rewatched manga' to The HTML and CSS that will be added to the new browser tab when the script is done
  125. if (type == "anime") result += "<h3><em>List of Animes that " + username + " has watched and ReWatched:</em></h3>"; //If the type is anime then add 'How many times username has watched and ReWatched' to The HTML and CSS that will be added to the new browser tab when the script is done
  126. if (type == "manga") result += "<h3><em>List of Mangas that " + username + " has read and ReRead:</em></h3>"; //If the type is manga then add ''How many times username has read and ReRead' to The HTML and CSS that will be added to the new browser tab when the script is done
  127.  
  128. if (old_list) //If the script is working on an old classic list style
  129. {
  130. // The 12 lines below Fetches the rewatch count information bypassing the 'More' link on old classic list styles
  131. $("div.hide").each(function(index, value) {
  132. var series_id = $(value).attr('id').split('more')[1];
  133. $.post("/includes/ajax-no-auth.inc.php?t=6", {
  134. color: 1,
  135. id: series_id,
  136. memId: $('#listUserId').val(),
  137. type: $('#listType').val()
  138. }, function(data) {
  139. if (type == "anime") rewatches[index] = $(data.html).find('strong')[0].innerHTML; //If the type is anime start scrapping the anime rewatched values
  140. if (type == "manga") //If the type is anime start scrapping the manga 'Times Read' values
  141. {
  142. var moreSection = $(data.html).find('td').html(); //Opens the more button on old classic style list
  143. var timesReadIndex = moreSection.indexOf("Times Read"); //Detects how many times a manga was read
  144. rewatches[index] = moreSection.charAt(timesReadIndex + 12);
  145. } //Finishes the if condition
  146. }, "json"); //The scrapping isn't done using HTML,it's done by scrapping only the json file that's loaded when the user goes down (loads more animes/mangas) ('XHR Get' Method)
  147. });
  148. } else //If the script was run on a new modern list style
  149. {
  150. console.log('Opening And Scraping All "More" Buttons.Please Wait!'); //Shows a message in the console for dev purposes
  151. // The 6 lines Below Will Click all links labeled 'More' to get the rewatch counts later on the page
  152. for (var i = moreLinks.length; i--;) {
  153. if (moreLinks[i].innerHTML == 'More') {
  154. moreLinks[i].click();
  155. }
  156. } //Finishes the for condition
  157. } //Finishes the else condition
  158.  
  159. // Progress bar
  160. $('body').append('<div id="progress" style="width: 600px;height: 20px;position: fixed;top:30%;left:50%;margin-left:-300px;border: 5px solid #ccc;background-color: #479af9;"></div>') //Creates and show an blue retangular box on the screen
  161. .append($('<div id="bar" style="text-align: left;width: 0px;height: 20px;position: fixed;top:30%;left:50%;margin-left:-295px;margin-top:5px;white-space:pre;color:white;font-weight:bold;font-size:16px"></div>')); //Creates and show the total animes number on the completed list on the blue retangular box on the screen that were processed by the script
  162.  
  163. wait(); // Repeats every 1 seconds until all More-sections are processed
  164.  
  165. function wait() //Creates the wait function
  166. {
  167. setTimeout(function() //Creates the timeout function
  168. {
  169. if (!old_list) rewatches = document.querySelectorAll('tbody.list-item tr.more-info strong'); //If the script was run on an new modern list style then use this command to select all the "more info" buttons
  170. // Log the progress
  171. console.log(rewatches.length + "/" + titles.length + " opened"); //Shows the total number of completed entries that were opened on the dev console.(This should be the same amount of your total completed entries).
  172. $('#bar').text("Total entries processed: " + rewatches.length + "/" + titles.length); //Adds the total number of completed entries that were processed to the HTML DIV ID named 'bar', on the new browser tab output
  173. $('#bar').css('width', rewatches.length / titles.length * 900 + 'px'); //Adds the css for the total number of completed entries that were processed to the HTML DIV ID named 'bar', on the new browser tab output
  174.  
  175. if (rewatches.length != titles.length) // Check if All sections were or not opened
  176. {
  177. wait(); //If All sections were not opened check it again after 1 seconds
  178. } else //If All sections were opened
  179. {
  180. if (old_list) //Check if the script was run in an old classic list style or not
  181. {
  182. for (var i = 0; i < titles.length; i++) {
  183. // Parse rewatched shows into an array of arrays with rewatch count as index and add them to the new browser tab when the script is done
  184. if (rewatches[i] > 0) {
  185. if (resultArray[rewatches[i]]) {
  186. resultArray[rewatches[i]] = resultArray[rewatches[i]].concat("<li>" + titles[i].innerHTML + "</li>");
  187. } else {
  188. resultArray[rewatches[i]] = "<b>" + (parseInt(rewatches[i]) + 1) + " times:</b>"; // +1 shows the total watched times number. -1 shows the total Re-Watched times only.
  189. resultArray[rewatches[i]] = resultArray[rewatches[i]].concat("<ul>");
  190. resultArray[rewatches[i]] = resultArray[rewatches[i]].concat("<li>" + titles[i].innerHTML + "</li>");
  191. } //Finishes the else condition
  192. } //Finishes the if condition
  193. } //Finishes the for condition
  194. } else //If the script was run in on the new default modern list style
  195. {
  196. for (var i = 0; i < titles.length; i++) {
  197. // Parse rewatched shows into an array of arrays with rewatch count as index
  198. if (rewatches[i].innerHTML > 0) {
  199. if (resultArray[rewatches[i].innerHTML]) {
  200. resultArray[rewatches[i].innerHTML] = resultArray[rewatches[i].innerHTML].concat("<li>" + titles[i].trim() + "</li>");
  201. } else {
  202. resultArray[rewatches[i].innerHTML] = "<b>" + (parseInt(rewatches[i].innerHTML) + 1) + " times:</b>"; // +1 shows the Re-Watched/Re-Read and the watched/read total numbers. -1 shows only the total times an anime/manga was Re-Watched/Re-Read.
  203. resultArray[rewatches[i].innerHTML] = resultArray[rewatches[i].innerHTML].concat("<ul>"); //Adds the divisories (div like html tags) between rewatched/reread numbers, and concatenates them
  204. resultArray[rewatches[i].innerHTML] = resultArray[rewatches[i].innerHTML].concat("<li>" + titles[i].trim() + "</li>"); //Adds the rewatched/reread titles inside the tags <li> , and concatenates them
  205. } //Finishes the else condition
  206. }
  207. }
  208. } //Finishes the else condition
  209.  
  210. resultArray.reverse(); //This command makes result on the new browser tab be organized by starting the list with the most rewatched/reread values, if this line is removed, the result on the new browser tab will start the list with animes rewatched once and the last animes on the list will be the most rewatched/reread one's.
  211. resultArray.forEach(function(value, index, array) {
  212. result += value.concat("</ul>");
  213. });
  214.  
  215. console.log('Done! Opening The Results Page!'); //Shows a message in the console for dev purposes
  216. console.log('Redirecting you to the profile page of the owner of this anime/manga list.'); //Shows a message in the console for dev purposes
  217. var link = document.createElement('a'); //Create an a element to hold the results and to be clicked later to download the result
  218. link.download = username + ' ReWatched_ReRead List!'; //Sets the downloaded filename to the user of the scrapped list + ReWatched_ReRead List!
  219. link.href = result; //Adds the scrapped results as a link on the a element
  220. link.click(); //Clicks on the a element to download the scrapped results file
  221. window.location.assign("https://myanimelist.net/profile/" + username); //Redirects the user to the profile page of the owner of the anime/manga list that was scrapped.This is done to get out of the 'interval' variable loop, because when the script finishes and downloads the output, the script won't stop looping if the user leaves the anime/manga completed page opened on his browser.This redirection command avoids this problem.
  222. } //Finishes the else condition
  223. }, 1000); //Finishes the settimeout function.Wait 1 second
  224. } //Finishes the function wait
  225. } // Finishes the scrape function
  226. })(); // Finishes the tampermonkey for condition