MAL Date Fix

Script that fixes your Start/Finish Dates on MAL

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

// ==UserScript==
// @name         MAL Date Fix
// @namespace    Fix your dates on MAL
// @version      1.235
// @description  Script that fixes your Start/Finish Dates on MAL
// @author       NurarihyonMaou
// @match        https://anime.plus/*/history,anime
// @match        https://myanimelist.net/ownlist/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @require      http://code.jquery.com/jquery-3.5.1.min.js
// ==/UserScript==


// Variables

var $ = window.jQuery;

var id = [];
var startDay = [];
var startMonth = [];
var startYear = [];
var endDay = [];
var endMonth = [];
var endYear = [];

var csrf_token;

var how_much, startDate, endDate;
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 = [];



$(window).ready(async () => {

    // Function to return requests in orginal order, without making them sync

  function SendGetRequest(url) {
    return new Promise((resolve, reject) => {
      GM_xmlhttpRequest({
        method: "GET",
        url: url,
        onload: function(response) {
          resolve(response.responseText);
        },
        onerror: function(error) {
          reject(error);
        }
      });
    });
  }
         if (window.location.pathname.split('/')[2] === 'history,anime') {

             // Checks the URL, then proceeds to gather ID's of titles that got missing dates

             $('a.entries-trigger')[0].click();

             how_much = parseInt($('a.entries-trigger').text());

             alert("Start");

             $('#month-unknown ul li a').each(function(){

                id.push(parseInt($(this).attr('href').slice(30)));
             });

              if(id.length == how_much){ GM_setValue('id', id);

                  // After getting all of the ID's, redirects You to MAL, to avoid CROSS-ORIGIN problems
                        document.location = "https://myanimelist.net/ownlist/anime/"+id[0]+"/edit"; }

              }




         if (window.location.pathname.split('/')[1] === 'ownlist'){

         id = GM_getValue('id');




 csrf_token = $("meta[name='csrf_token']").attr('content');


             // 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

for (let i = 0; i < id.length; i++) {
  $("body").html("Getting info about " + (i + 1) + " Title");
  try {
    const response = await SendGetRequest("https://myanimelist.net/ownlist/anime/"+id[i]+"/edit");
    $(response).find("#main-form :input").each(function(x) {

        // Gets all the current data except dates, by checking values of the inputs

                         if (x == 1)
                             aeps.push($(this).val());
                         if (x == 2)
                             astatus.push($(this).val());
                         if (x == 3)
                             status.push($(this).val());
                         if (x == 4)
                             is_rewatching.push($(this).val());
                         if (x == 5)
                             num_watched_episodes.push($(this).val());
                         if (x == 6)
                             score.push($(this).val());
                         if (x == 15)
                             tags.push($(this).val());
                         if (x == 16)
                             priority.push($(this).val());
                         if (x == 17)
                             storage_type.push($(this).val());
                         if (x == 18)
                             storage_value.push($(this).val());
                         if (x == 19)
                             num_watched_times.push($(this).val());
                         if (x == 20)
                             rewatch_value.push($(this).val());
                         if (x == 21)
                             comments.push($(this).val());
                         if (x == 22)
                             is_asked_to_discuss.push($(this).val());
                         if (x == 23)
                             sns_post_type.push($(this).val());

    });
  } catch (error) {
      if(error.status != null) $("body").html("Request failed with error code", error.status, ". Message is ", error.responseText);
  }

}
if(aeps.length == id.length){

    // After obtaining current data, it proceeds to get Missing Dates

                               for(let f = 0; f<id.length; f++){
                  $("body").html("Getting date of " + (f + 1) + " Title");

                  try {
                    const response = await SendGetRequest("https://myanimelist.net/ajaxtb.php?keepThis=true&detailedaid=" +id[f] +"&TB_iframe=true&height=420&width=390");
                    var length = $(response).find('.spaceit_pad').length;
                    $($(response).find('.spaceit_pad').get().reverse()).each(function (x) {

                        // 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

                        if (x == 0){
                            startDate = $(this).html().match(/\d{2}([\/.-])\d{2}\1\d{4}/g);
                            startDate  = startDate.toString();

                            startDay.push(parseInt(startDate.split('/')[1],10));
                            startMonth.push(parseInt(startDate.split('/')[0],10));
                            startYear.push(parseInt(startDate.split('/')[2],10));
                        }

                      if (x<length && $(this).html().indexOf("Ep "+aeps[f]) == 0){
                            endDate = $(this).html().match(/\d{2}([\/.-])\d{2}\1\d{4}/g);
                            endDate = endDate.toString();

                            endDay.push(parseInt(endDate.split('/')[1],10));
                            endMonth.push(parseInt(endDate.split('/')[0],10));
                            endYear.push(parseInt(endDate.split('/')[2],10));

                            return false;
                        }
                          x++


                    });
                  } catch (error) {
                      if(error.status != null) $("body").html("Request failed with error code", error.status, ". Message is - ", error.responseText);
                  }

                }
}

if(endYear.length == id.length){
 $("body").html("<h1>Last Stage</h1>");

    // Last Stage - just sends update requests

  for(let e = 0; e<id.length; e++){
$.ajax({
method: 'POST',
    url: 'https://myanimelist.net/ownlist/anime/'+id[e]+'/edit',
    data: {
'anime_id': id[e],
'aeps': aeps[e],
'astatus': astatus[e],
'add_anime[status]': status[e],
'add_anime[num_watched_episodes]': num_watched_episodes[e],
'add_anime[score]': score[e],
'add_anime[start_date][month]': startMonth[e],
'add_anime[start_date][day]': startDay[e],
'add_anime[start_date][year]': startYear[e],
'add_anime[finish_date][month]': endMonth[e],
'add_anime[finish_date][day]': endDay[e],
'add_anime[finish_date][year]': endYear[e],
'add_anime[tags]': tags[e],
'add_anime[priority]': priority[e],
'add_anime[storage_type]': storage_type[e],
'add_anime[storage_value]': storage_value[e],
'add_anime[num_watched_times]': num_watched_times[e],
'add_anime[rewatch_value]': rewatch_value[e],
'add_anime[comments]': comments[e],
'add_anime[is_asked_to_discuss]': is_asked_to_discuss[e],
'add_anime[sns_post_type]': sns_post_type[e],
'submitIt': 0,
'csrf_token': csrf_token
    },
    complete: function(){
      $("body").append("Successfully updated "+(e+1)+" Title</br>");
         if(e == id.length-1) setTimeout(function(){ $("body").append("<h1>The End</h1>");}, 500);
    },
    error: function(){
     $("body").append("<h2>Something went wrong with this ID - <a href='https://myanimelist.net/anime/"+id[e]+"'>Link</a></h2>");
    }
  });

}
}

         }


});