MAL Date Fix

Script that fixes your Start/Finish Dates on MAL

当前为 2021-05-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MAL Date Fix
  3. // @namespace Fix your dates on MAL
  4. // @version 1.235
  5. // @description Script that fixes your Start/Finish Dates on MAL
  6. // @author NurarihyonMaou
  7. // @match https://anime.plus/*/history,anime
  8. // @match https://myanimelist.net/ownlist/*
  9. // @grant GM_xmlhttpRequest
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @require http://code.jquery.com/jquery-3.5.1.min.js
  13. // ==/UserScript==
  14.  
  15.  
  16. // Variables
  17.  
  18. var $ = window.jQuery;
  19.  
  20. var id = [];
  21. var startDay = [];
  22. var startMonth = [];
  23. var startYear = [];
  24. var endDay = [];
  25. var endMonth = [];
  26. var endYear = [];
  27.  
  28. var csrf_token;
  29.  
  30. var how_much, startDate, endDate;
  31. let aeps = [], astatus = [], status = [], num_watched_episodes = [], score = [], tags = [], priority = [], storage_type = [], storage_value = [], num_watched_times = [], rewatch_value = [], comments = [], is_asked_to_discuss = [], sns_post_type = [], is_rewatching = [], submitIt = [];
  32.  
  33.  
  34.  
  35. $(window).ready(async () => {
  36.  
  37. // Function to return requests in orginal order, without making them sync
  38.  
  39. function SendGetRequest(url) {
  40. return new Promise((resolve, reject) => {
  41. GM_xmlhttpRequest({
  42. method: "GET",
  43. url: url,
  44. onload: function(response) {
  45. resolve(response.responseText);
  46. },
  47. onerror: function(error) {
  48. reject(error);
  49. }
  50. });
  51. });
  52. }
  53. if (window.location.pathname.split('/')[2] === 'history,anime') {
  54.  
  55. // Checks the URL, then proceeds to gather ID's of titles that got missing dates
  56.  
  57. $('a.entries-trigger')[0].click();
  58.  
  59. how_much = parseInt($('a.entries-trigger').text());
  60.  
  61. alert("Start");
  62.  
  63. $('#month-unknown ul li a').each(function(){
  64.  
  65. id.push(parseInt($(this).attr('href').slice(30)));
  66. });
  67.  
  68. if(id.length == how_much){ GM_setValue('id', id);
  69.  
  70. // After getting all of the ID's, redirects You to MAL, to avoid CROSS-ORIGIN problems
  71. document.location = "https://myanimelist.net/ownlist/anime/"+id[0]+"/edit"; }
  72.  
  73. }
  74.  
  75.  
  76.  
  77.  
  78. if (window.location.pathname.split('/')[1] === 'ownlist'){
  79.  
  80. id = GM_getValue('id');
  81.  
  82.  
  83.  
  84.  
  85. csrf_token = $("meta[name='csrf_token']").attr('content');
  86.  
  87.  
  88. // After redirecting fetches data saved in the TamperMonkey, then gets csrf_token to allow sending update requests, and starts requesting current data of all the titles using previously obtained ID's
  89.  
  90. for (let i = 0; i < id.length; i++) {
  91. $("body").html("Getting info about " + (i + 1) + " Title");
  92. try {
  93. const response = await SendGetRequest("https://myanimelist.net/ownlist/anime/"+id[i]+"/edit");
  94. $(response).find("#main-form :input").each(function(x) {
  95.  
  96. // Gets all the current data except dates, by checking values of the inputs
  97.  
  98. if (x == 1)
  99. aeps.push($(this).val());
  100. if (x == 2)
  101. astatus.push($(this).val());
  102. if (x == 3)
  103. status.push($(this).val());
  104. if (x == 4)
  105. is_rewatching.push($(this).val());
  106. if (x == 5)
  107. num_watched_episodes.push($(this).val());
  108. if (x == 6)
  109. score.push($(this).val());
  110. if (x == 15)
  111. tags.push($(this).val());
  112. if (x == 16)
  113. priority.push($(this).val());
  114. if (x == 17)
  115. storage_type.push($(this).val());
  116. if (x == 18)
  117. storage_value.push($(this).val());
  118. if (x == 19)
  119. num_watched_times.push($(this).val());
  120. if (x == 20)
  121. rewatch_value.push($(this).val());
  122. if (x == 21)
  123. comments.push($(this).val());
  124. if (x == 22)
  125. is_asked_to_discuss.push($(this).val());
  126. if (x == 23)
  127. sns_post_type.push($(this).val());
  128.  
  129. });
  130. } catch (error) {
  131. if(error.status != null) $("body").html("Request failed with error code", error.status, ". Message is ", error.responseText);
  132. }
  133.  
  134. }
  135. if(aeps.length == id.length){
  136.  
  137. // After obtaining current data, it proceeds to get Missing Dates
  138.  
  139. for(let f = 0; f<id.length; f++){
  140. $("body").html("Getting date of " + (f + 1) + " Title");
  141.  
  142. try {
  143. const response = await SendGetRequest("https://myanimelist.net/ajaxtb.php?keepThis=true&detailedaid=" +id[f] +"&TB_iframe=true&height=420&width=390");
  144. var length = $(response).find('.spaceit_pad').length;
  145. $($(response).find('.spaceit_pad').get().reverse()).each(function (x) {
  146.  
  147. // Takes first record as the start date, and first one (to get first watch dates, not the rewatch ones) that got the number of the last ep as the Finish Date
  148.  
  149. if (x == 0){
  150. startDate = $(this).html().match(/\d{2}([\/.-])\d{2}\1\d{4}/g);
  151. startDate = startDate.toString();
  152.  
  153. startDay.push(parseInt(startDate.split('/')[1],10));
  154. startMonth.push(parseInt(startDate.split('/')[0],10));
  155. startYear.push(parseInt(startDate.split('/')[2],10));
  156. }
  157.  
  158. if (x<length && $(this).html().indexOf("Ep "+aeps[f]) == 0){
  159. endDate = $(this).html().match(/\d{2}([\/.-])\d{2}\1\d{4}/g);
  160. endDate = endDate.toString();
  161.  
  162. endDay.push(parseInt(endDate.split('/')[1],10));
  163. endMonth.push(parseInt(endDate.split('/')[0],10));
  164. endYear.push(parseInt(endDate.split('/')[2],10));
  165.  
  166. return false;
  167. }
  168. x++
  169.  
  170.  
  171. });
  172. } catch (error) {
  173. if(error.status != null) $("body").html("Request failed with error code", error.status, ". Message is - ", error.responseText);
  174. }
  175.  
  176. }
  177. }
  178.  
  179. if(endYear.length == id.length){
  180. $("body").html("<h1>Last Stage</h1>");
  181.  
  182. // Last Stage - just sends update requests
  183.  
  184. for(let e = 0; e<id.length; e++){
  185. $.ajax({
  186. method: 'POST',
  187. url: 'https://myanimelist.net/ownlist/anime/'+id[e]+'/edit',
  188. data: {
  189. 'anime_id': id[e],
  190. 'aeps': aeps[e],
  191. 'astatus': astatus[e],
  192. 'add_anime[status]': status[e],
  193. 'add_anime[num_watched_episodes]': num_watched_episodes[e],
  194. 'add_anime[score]': score[e],
  195. 'add_anime[start_date][month]': startMonth[e],
  196. 'add_anime[start_date][day]': startDay[e],
  197. 'add_anime[start_date][year]': startYear[e],
  198. 'add_anime[finish_date][month]': endMonth[e],
  199. 'add_anime[finish_date][day]': endDay[e],
  200. 'add_anime[finish_date][year]': endYear[e],
  201. 'add_anime[tags]': tags[e],
  202. 'add_anime[priority]': priority[e],
  203. 'add_anime[storage_type]': storage_type[e],
  204. 'add_anime[storage_value]': storage_value[e],
  205. 'add_anime[num_watched_times]': num_watched_times[e],
  206. 'add_anime[rewatch_value]': rewatch_value[e],
  207. 'add_anime[comments]': comments[e],
  208. 'add_anime[is_asked_to_discuss]': is_asked_to_discuss[e],
  209. 'add_anime[sns_post_type]': sns_post_type[e],
  210. 'submitIt': 0,
  211. 'csrf_token': csrf_token
  212. },
  213. complete: function(){
  214. $("body").append("Successfully updated "+(e+1)+" Title</br>");
  215. if(e == id.length-1) setTimeout(function(){ $("body").append("<h1>The End</h1>");}, 500);
  216. },
  217. error: function(){
  218. $("body").append("<h2>Something went wrong with this ID - <a href='https://myanimelist.net/anime/"+id[e]+"'>Link</a></h2>");
  219. }
  220. });
  221.  
  222. }
  223. }
  224.  
  225. }
  226.  
  227.  
  228. });