MAL Date Fix

Script that fixes your Start/Finish Dates on MAL

目前為 2021-02-07 提交的版本,檢視 最新版本

// ==UserScript==
// @name         MAL Date Fix
// @namespace    Fix your dates on MAL
// @version      1.2
// @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==

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 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') {

             $('a.entries-trigger')[0].click();
             how_much = parseInt($('a.entries-trigger').text());


             console.log("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);

                        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');




for (let i = 0; i < id.length; i++) {
  console.log("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) {
                         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) { // in case the GET request fails
      if(error.status != null) console.error("Request failed with error code", error.status, ". Message is ", error.responseText);
  }

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

                               for(let f = 0; f<id.length; f++){
                  console.log("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) {

                        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 == aeps[f]-1){
                            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));

                        }
                      if(f == id.length-1 && x == length-1){
                          console.log("Last Stage");
                      }


                    });
                  } catch (error) { // in case the GET request fails
                      if(error.status != null) console.error("Request failed with error code", error.status, ". Message is ", error.responseText);
                  }

                }
}

if(startDate.length > id.length){
  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(){
      console.log("Successfully updated "+(e+1)+" Title");
         if(e == id.length-1) console.log("The End");
    },
    error: function(){
      console.log("Something went wrong");
    }
  });

}
}

         }


});