MAL Date Fix

Script that sets your Start/Finish Dates on MAL

当前为 2021-08-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name MAL Date Fix
  3. // @namespace Fix your dates on MAL
  4. // @version 1.35
  5. // @description Script that sets 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. // @grant GM_deleteValue
  13. // @grant GM_listValues
  14. // @require http://code.jquery.com/jquery-3.5.1.min.js
  15. // ==/UserScript==
  16.  
  17.  
  18. // Variables
  19.  
  20. const $ = window.jQuery;
  21.  
  22. var id = [];
  23. var startDay = [];
  24. var startMonth = [];
  25. var startYear = [];
  26. var endDay = [];
  27. var endMonth = [];
  28. var endYear = [];
  29.  
  30. var csrf_token;
  31.  
  32. var how_much, startDate, endDate;
  33.  
  34. var 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 = [];
  35.  
  36.  
  37. if (GM_getValue('sns_post_type') != undefined) {
  38. storage_type = GM_getValue('storage_type');
  39. storage_value = GM_getValue('storage_value');
  40. is_rewatching = GM_getValue('is_rewatching');
  41. aeps = GM_getValue('aeps');
  42. status = GM_getValue('status');
  43. astatus = GM_getValue('astatus');
  44. num_watched_times = GM_getValue('num_watched_times');
  45. num_watched_episodes = GM_getValue('num_watched_episodes');
  46. comments = GM_getValue('comments');
  47. sns_post_type = GM_getValue('sns_post_type');
  48. score = GM_getValue('score');
  49. rewatch_value = GM_getValue('rewatch_value');
  50. tags = GM_getValue('tags');
  51. priority = GM_getValue('priority');
  52. is_asked_to_discuss = GM_getValue('is_asked_to_discuss');
  53. }
  54.  
  55. if (GM_getValue('endYear') != undefined) {
  56. startDay = GM_getValue('startDay');
  57. startMonth = GM_getValue('startMonth');
  58. startYear = GM_getValue('startYear');
  59. endDay = GM_getValue('endDay');
  60. endMonth = GM_getValue('endMonth');
  61. endYear = GM_getValue('endYear');
  62. }
  63.  
  64.  
  65.  
  66. $(window).ready(async () => {
  67.  
  68. // Function to return requests in orginal order, without making them sync
  69.  
  70. function SendGetRequest(url) {
  71. return new Promise((resolve, reject) => {
  72. GM_xmlhttpRequest({
  73. method: "GET",
  74. url: url,
  75. onload: function (response) {
  76. resolve(response.responseText);
  77. },
  78. onerror: function (error) {
  79. reject(error);
  80. }
  81. });
  82. });
  83. }
  84. if (window.location.pathname.split('/')[2] === 'history,anime') {
  85.  
  86. // Checks the URL, then proceeds to gather ID's of titles that got missing dates
  87.  
  88. $('a.entries-trigger')[0].click();
  89.  
  90. how_much = parseInt($('a.entries-trigger').text());
  91.  
  92. alert("Start");
  93.  
  94. $('#month-unknown ul li a').each(function () {
  95.  
  96. id.push(parseInt($(this).attr('href').slice(30)));
  97. });
  98.  
  99. if (id.length == how_much) {
  100. GM_setValue('id', id);
  101.  
  102. // After getting all of the ID's, redirects You to MAL, to avoid CROSS-ORIGIN problems
  103. document.location = "https://myanimelist.net/ownlist/anime/" + id[0] + "/edit";
  104. }
  105.  
  106. }
  107.  
  108.  
  109.  
  110.  
  111. if (window.location.pathname.split('/')[1] === 'ownlist') {
  112.  
  113. if ($(".g-recaptcha").length > 0) {
  114. $(".g-recaptcha").click();
  115. }
  116. else {
  117.  
  118. id = GM_getValue('id');
  119. if (id == null) alert("No Data, please Start from the Beginning");
  120.  
  121. $("body").html("<label for='p1'>Fetching information</label></br><progress id='p1' value='0' max='" + id.length + "'></progress></br><label for='p2'>Fetching Dates</label></br><progress id='p2' value='0' max='" + id.length + "'></progress></br><label for='p3'>Sending Update Requests</label></br><progress id='p3' value='0' max='" + id.length + "'></progress>");
  122.  
  123. csrf_token = $("meta[name='csrf_token']").attr('content');
  124.  
  125. // 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
  126.  
  127. // in case when you got temporaly block to avoid request spam
  128.  
  129. var i;
  130. if (GM_getValue('i') == null) i = 0;
  131. else i = GM_getValue('i');
  132.  
  133. var f;
  134. if (GM_getValue('f') == null) f = 0;
  135. else { f = GM_getValue('f'); i = id.length - 1; }
  136.  
  137. var e;
  138. if (GM_getValue('e') == null) e = 0;
  139. else { e = GM_getValue('e'); i = id.length - 1; f = id.length - 1; }
  140.  
  141.  
  142.  
  143. for (i; i < id.length; i++) {
  144. $("#p1").attr("value", i + 1);
  145.  
  146. try {
  147. const response = await SendGetRequest("https://myanimelist.net/ownlist/anime/" + id[i] + "/edit");
  148. $(response).find("#main-form :input").each(function (x) {
  149.  
  150. // Gets all the current data except dates, by checking values of the inputs
  151.  
  152. if (x == 1)
  153. aeps.push($(this).val());
  154. if (x == 2)
  155. astatus.push($(this).val());
  156. if (x == 3)
  157. status.push($(this).val());
  158. if (x == 4)
  159. is_rewatching.push($(this).val());
  160. if (x == 5)
  161. num_watched_episodes.push($(this).val());
  162. if (x == 6)
  163. score.push($(this).val());
  164. if (x == 15)
  165. tags.push($(this).val());
  166. if (x == 16)
  167. priority.push($(this).val());
  168. if (x == 17)
  169. storage_type.push($(this).val());
  170. if (x == 18)
  171. storage_value.push($(this).val());
  172. if (x == 19)
  173. num_watched_times.push($(this).val());
  174. if (x == 20)
  175. rewatch_value.push($(this).val());
  176. if (x == 21)
  177. comments.push($(this).val());
  178. if (x == 22)
  179. is_asked_to_discuss.push($(this).val());
  180. if (x == 23)
  181. sns_post_type.push($(this).val());
  182.  
  183. });
  184.  
  185. } catch (error) {
  186. if (error.status != null) {
  187. GM_setValue('i', i); GM_setValue('storage_type', storage_type); GM_setValue('num_watched_episodes', num_watched_episodes); GM_setValue('sns_post_type', sns_post_type);
  188. GM_setValue('aeps', aeps); GM_setValue('storage_value', storage_value); GM_setValue('score', score);
  189. GM_setValue('astatus', astatus); GM_setValue('num_watched_times', num_watched_times); GM_setValue('tags', tags);
  190. GM_setValue('status', status); GM_setValue('rewatch_value', rewatch_value); GM_setValue('priority', priority);
  191. GM_setValue('is_rewatching', is_rewatching); GM_setValue('comments', comments); GM_setValue('is_asked_to_discuss', is_asked_to_discuss);
  192.  
  193. $("body").html("Request failed with error code", error.status, ". Message is ", error.responseText);
  194. break;
  195. }
  196. }
  197.  
  198. }
  199.  
  200. if (aeps.length == id.length) {
  201.  
  202. // After obtaining current data, it proceeds to get Missing Dates
  203.  
  204. for (f; f < id.length; f++) {
  205.  
  206. $("#p2").attr("value", f + 1);
  207.  
  208. try {
  209. const response = await SendGetRequest("https://myanimelist.net/ajaxtb.php?keepThis=true&detailedaid=" + id[f] + "&TB_iframe=true&height=420&width=390");
  210. var length = $(response).find('.spaceit_pad').length;
  211. $($(response).find('.spaceit_pad').get().reverse()).each(function (x) {
  212.  
  213. // 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
  214.  
  215. if (x == 0) {
  216. startDate = $(this).html().match(/\d{2}([\/.-])\d{2}\1\d{4}/g);
  217. startDate = startDate.toString();
  218.  
  219. startDay.push(parseInt(startDate.split('/')[1], 10));
  220. startMonth.push(parseInt(startDate.split('/')[0], 10));
  221. startYear.push(parseInt(startDate.split('/')[2], 10));
  222. }
  223.  
  224. if (x < length && $(this).html().indexOf("Ep " + aeps[f]) == 0) {
  225. endDate = $(this).html().match(/\d{2}([\/.-])\d{2}\1\d{4}/g);
  226. endDate = endDate.toString();
  227.  
  228. endDay.push(parseInt(endDate.split('/')[1], 10));
  229. endMonth.push(parseInt(endDate.split('/')[0], 10));
  230. endYear.push(parseInt(endDate.split('/')[2], 10));
  231.  
  232. return false;
  233. }
  234. x++
  235.  
  236.  
  237. });
  238.  
  239. } catch (error) {
  240.  
  241. if (error.status != null) {
  242.  
  243. $("body").html("Request failed with error code", error.status, ". Message is - ", error.responseText);
  244. GM_setValue('f', f);
  245. GM_setValue('storage_type', storage_type); GM_setValue('num_watched_episodes', num_watched_episodes); GM_setValue('sns_post_type', sns_post_type);
  246. GM_setValue('aeps', aeps); GM_setValue('storage_value', storage_value); GM_setValue('score', score);
  247. GM_setValue('astatus', astatus); GM_setValue('num_watched_times', num_watched_times); GM_setValue('tags', tags);
  248. GM_setValue('status', status); GM_setValue('rewatch_value', rewatch_value); GM_setValue('priority', priority);
  249. GM_setValue('is_rewatching', is_rewatching); GM_setValue('comments', comments); GM_setValue('is_asked_to_discuss', is_asked_to_discuss);
  250. GM_setValue('startDay', startDay);
  251. GM_setValue('startMonth', startMonth);
  252. GM_setValue('startYear', startYear);
  253. GM_setValue('endDay', endDay);
  254. GM_setValue('endMonth', endMonth);
  255. GM_setValue('endYear', endYear);
  256. break;
  257. }
  258. }
  259.  
  260. }
  261. }
  262.  
  263.  
  264. if (endYear.length == id.length) {
  265.  
  266.  
  267. // Last Stage - just sends update requests
  268.  
  269. for (e; e < id.length; e++) {
  270.  
  271.  
  272.  
  273. $.ajax({
  274. method: 'POST',
  275. url: 'https://myanimelist.net/ownlist/anime/' + id[e] + '/edit',
  276. data: {
  277. 'anime_id': id[e],
  278. 'aeps': aeps[e],
  279. 'astatus': astatus[e],
  280. 'add_anime[status]': status[e],
  281. 'add_anime[num_watched_episodes]': num_watched_episodes[e],
  282. 'add_anime[score]': score[e],
  283. 'add_anime[start_date][month]': startMonth[e],
  284. 'add_anime[start_date][day]': startDay[e],
  285. 'add_anime[start_date][year]': startYear[e],
  286. 'add_anime[finish_date][month]': endMonth[e],
  287. 'add_anime[finish_date][day]': endDay[e],
  288. 'add_anime[finish_date][year]': endYear[e],
  289. 'add_anime[tags]': tags[e],
  290. 'add_anime[priority]': priority[e],
  291. 'add_anime[storage_type]': storage_type[e],
  292. 'add_anime[storage_value]': storage_value[e],
  293. 'add_anime[num_watched_times]': num_watched_times[e],
  294. 'add_anime[rewatch_value]': rewatch_value[e],
  295. 'add_anime[comments]': comments[e],
  296. 'add_anime[is_asked_to_discuss]': is_asked_to_discuss[e],
  297. 'add_anime[sns_post_type]': sns_post_type[e],
  298. 'submitIt': 0,
  299. 'csrf_token': csrf_token
  300. },
  301. complete: function () {
  302. $("#p3").attr("value", e + 1);
  303. if (e == id.length) { setTimeout(async function () { $("body").html("<h1>The End</h1>"); let keys = await GM_listValues(); for (let key of keys) { GM_deleteValue(key); } }, 500); }
  304. },
  305. error: function () {
  306. GM_setValue('e', e);
  307. GM_setValue('storage_type', storage_type); GM_setValue('num_watched_episodes', num_watched_episodes); GM_setValue('sns_post_type', sns_post_type);
  308. GM_setValue('aeps', aeps); GM_setValue('storage_value', storage_value); GM_setValue('score', score);
  309. GM_setValue('astatus', astatus); GM_setValue('num_watched_times', num_watched_times); GM_setValue('tags', tags);
  310. GM_setValue('status', status); GM_setValue('rewatch_value', rewatch_value); GM_setValue('priority', priority);
  311. GM_setValue('is_rewatching', is_rewatching); GM_setValue('comments', comments); GM_setValue('is_asked_to_discuss', is_asked_to_discuss);
  312. GM_setValue('startDay', startDay);
  313. GM_setValue('startMonth', startMonth);
  314. GM_setValue('startYear', startYear);
  315. GM_setValue('endDay', endDay);
  316. GM_setValue('endMonth', endMonth);
  317. GM_setValue('endYear', endYear);
  318. setTimeout(function () { location.reload(); }, 500);
  319. }
  320. });
  321.  
  322. }
  323. }
  324.  
  325. }
  326. }
  327. });