Greasy Fork is available in English.

Discussões » Desenvolvimento

Comparing two arrays doesn't return 0

§
Publicado: 25/06/2021
Editado: 25/06/2021

I think that ChecknapprovedAnimes[0] should return the stored ID number, not ChecknapprovedAnimes[0][0], not sure what I did wrong

It's interesting because when I run this on the browser console it works, but doesn't work on the script...

ChecknapprovedAnimes = []

ChecknapprovedAnimes = [
    "49282"
]

UnnapprovedAnimes = [
    "49282"
]

ChecknapprovedAnimes.filter(d => !UnnapprovedAnimes.includes(d)) //Should be =[]

I will write below the parts of the script that probably has the problem in it somewhere

  GM_listValues().forEach(function(a) { //For each stored value on tampermonkey
    if(a.match('anime') !== null) //If the saved value is an anime id
    { //Starts the if condition
      ChecknapprovedAnimes.push(a.match(/\d+/)); //Add the stored anime id to an array
    } //Finishes the if condition
    if(a.match('manga') !== null) //If the saved value is a manga id
    { //Starts the if condition
      CheckapprovedMangas.push(a.match(/\d+/)); //Add the stored manga id to an array
    } //Finishes the if condition
  }); //Add all Entry IDs and types on tampermonkey to the array
    var FinalApprovedAnimesArray = ChecknapprovedAnimes.filter(d => !UnnapprovedAnimes.includes(d)); //Get the entry ids that the user is waiting to be approved but sean's website are missing
    var FinalApprovedMangasArray = CheckapprovedMangas.filter(d => !UnnapprovedMangas.includes(d)); //Get the entry ids that the user is waiting to be approved but sean's website are missing

    console.log(ChecknapprovedAnimes[0][0]) //This returns the stored ID number on tampermonkey
    console.log(UnnapprovedAnimes[0]) //This returns the stored ID number on tampermonkey
    document.title = FinalApprovedAnimesArray //This should be = []

Full Script below

// ==UserScript==
// @name         Approved Entries Notifier - MAL
// @namespace    NotifyWhenApproved
// @version      0.1
// @description  This script auto performs a few checks daily to notify you when specific anime/manga entries gets approved.
// @author       hacker09
// @match        https://myanimelist.net/*
// @icon         https://www.google.com/s2/favicons?domain=myanimelist.net
// @run-at       document-end
// @grant        GM_notification
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(async function() {
  'use strict';
  //https://www.bennish.net/web-notifications.html

  var ActualTime = new Date().getHours(); //Save the actual time in a variable
  var UnnapprovedAnimes = []; //Store all the unapproved animes in this array later
  var UnnapprovedMangas = []; //Store all the unapproved mangas in this array later
  var ChecknapprovedAnimes = []; //Store all the animes stored in tampermonkey in this array later
  var CheckapprovedMangas = []; //Store all the mangas stored in tampermonkey in this array later
  var OpenApprovedAnimes = []; //Create a new blank array
  var OpenApprovedMangas = []; //Create a new blank array
  var Three_Hours_From_LastTime = new Date(new Date().setHours(GM_getValue('Last_Check_Hour') + 3)).getHours(); //Check when it's going to be 3 hours from the last check time
  var EntryType = location.pathname.split('/')[1]; //Store the Entry type
  var EntryID = location.pathname.match(/\d+/)[0]; //Store the Entry ID

  GM_listValues().forEach(function(a) { //For each stored value on tampermonkey
    if(a.match('anime') !== null) //If the saved value is an anime id
    { //Starts the if condition
      ChecknapprovedAnimes.push(a.match(/\d+/)); //Add the stored anime id to an array
    } //Finishes the if condition
    if(a.match('manga') !== null) //If the saved value is a manga id
    { //Starts the if condition
      CheckapprovedMangas.push(a.match(/\d+/)); //Add the stored manga id to an array
    } //Finishes the if condition
  }); //Add all Entry IDs and types on tampermonkey to the array

  //if (GM_getValue('Last_Check_Hour') === undefined || GM_getValue('Last_Day_MAL_Was_Opened') === undefined || GM_getValue('Last_Check_Hour') >= Three_Hours_From_LastTime || (GM_getValue('Last_Check_Hour') <= Three_Hours_From_LastTime && new Date().getDate() !== GM_getValue('Last_Day_MAL_Was_Opened'))) //If the Last_Check_Hour or Last_Day_MAL_Was_Opened variables wasn't set yet
  //Or if 3 hours from the last check time has passed or if more than 3 hours has passed since the last check
  //Or if the actual time is before the 3 hours from the last check time and if today is a another day
  if(true){ //Starts the if condition
    GM_setValue('Last_Check_Hour', ActualTime); //Get and save the last check hour
    GM_setValue('Last_Day_MAL_Was_Opened', new Date().getDate()); //Get and save the Last_Day_MAL_Was_Opened

    const response = await fetch('https://api.allorigins.win/raw?url=https://sean.fish/mal_unapproved/anime'); //Fetch
    const html = await response.text(); //Gets the fetch response
    const newDocument = new DOMParser().parseFromString(html, 'text/html'); //Parses the fetch response
    newDocument.querySelectorAll("#mal-unapproved > ol > li").forEach(a => UnnapprovedAnimes.push(a.innerText.match(/\d+/)[0])); //Store all unnapproved anime entries on the array
    const response2 = await fetch('https://api.allorigins.win/raw?url=https://sean.fish/mal_unapproved/manga'); //Fetch
    const html2 = await response2.text(); //Gets the fetch response
    const newDocument2 = new DOMParser().parseFromString(html2, 'text/html'); //Parses the fetch response
    newDocument2.querySelectorAll("#mal-unapproved > ol > li").forEach(a => UnnapprovedMangas.push(a.innerText.match(/\d+/)[0])); //Store all unnapproved manga entries on the array

    var FinalApprovedAnimesArray = ChecknapprovedAnimes.filter(d => !UnnapprovedAnimes.includes(d)); //Get the entry ids that the user is waiting to be approved but sean's website are missing
    var FinalApprovedMangasArray = CheckapprovedMangas.filter(d => !UnnapprovedMangas.includes(d)); //Get the entry ids that the user is waiting to be approved but sean's website are missing

    console.log(ChecknapprovedAnimes[0][0]) //This returns the stored ID number on tampermonkey
    console.log(UnnapprovedAnimes[0]) //This returns the stored ID number on tampermonkey
    document.title = FinalApprovedAnimesArray //This should be = []
    if (FinalApprovedAnimesArray.length === 0 || FinalApprovedMangasArray.length === 0) //If the anime/manga id we want to know when is approved is not on sean's website (the anime/manga got approved)
    { //Starts the if condition
      FinalApprovedAnimesArray.forEach(a => OpenApprovedAnimes.push('https://myanimelist.net/anime/'+a));
      FinalApprovedMangasArray.forEach(a => OpenApprovedMangas.push('https://myanimelist.net/manga/'+a));

      GM_notification ({ //Shows a browser notification
        title: 'Found Approved Entries', text: 'Click on here to open the approved entries links', image: 'https://i.imgur.com/RmsXhIl.jpg', highlight: true, silent: true, timeout: 15000, //Define the browser notification details
        onclick: () => { //If the browser notification is clicked

          if (FinalApprovedAnimesArray.length === 0) //If the anime id we want to know when is approved is not on sean's website (the anime got approved)
          { //Starts the if condition
            OpenApprovedAnimes.forEach(a => window.open(a, '_blank'));
          } //Finishes the if condition
          if (FinalApprovedMangasArray.length === 0) //If the manga id we want to know when is approved is not on sean's website (the manga got approved)
          { //Starts the if condition
            OpenApprovedMangas.forEach(a => window.open(a, '_blank'));
          } //Finishes the if condition

          window.focus(); //Refocus on the tab
        } //Finishes the onclick event listener
      }); //Finishes the browser notification definitions

    } //Finishes the if condition
  } //Finishes the if condition

  if (document.querySelector("span.disabled-btn-user-status-add-list") !== null) //If an unapproved anime/manga entry was opened
  { //Starts the if condition
    document.querySelector("span.disabled-btn-user-status-add-list").className = 'btn-user-status-add-list js-form-user-status js-form-user-status-btn  myinfo_addtolist'; //Make the button look better
    document.querySelector("span.btn-user-status-add-list.js-form-user-status.js-form-user-status-btn.myinfo_addtolist").innerText = 'Notify when Approved'; //Give the user the option to get notified to know when the entry gets approved
    document.querySelector("span.btn-user-status-add-list.js-form-user-status.js-form-user-status-btn.myinfo_addtolist").onclick = function() //If the Notify when Approved button is clicked
    { //Starts the onclick listener
      GM_notification ({ //Shows a browser notification
        title: "You will be notified when this entry get's approved!", text: ' ', image: 'https://i.imgur.com/RmsXhIl.jpg', highlight: true, silent: true, timeout: 5000, //Define the browser notification details
        onclick: () => { //If the browser notification is clicked
          //Copy or open all links add this code here
          window.focus (); //Refocus on the tab
        } //Finishes the onclick event listener
      }); //Finishes the browser notification definitions
      GM_setValue(EntryType+EntryID, EntryID); //Get and save the Entry type and entry id
    } //Finishes the onclick listener
    document.querySelector("div.js-myinfo-error.badresult-text.al.pb4").remove(); //Remove the error message
  } //Finishes the if condition
})();
§
Publicado: 02/07/2021

change ChecknapprovedAnimes.push(a.match(/\d+/)); to ChecknapprovedAnimes.push(a.match(/\d+/)[0]); or use

    if(a.match(/anime.*?(\d+)/) !== null) //If the saved value is an anime id
    { //Starts the if condition
      ChecknapprovedAnimes.push(RegExp.$1); //Add the stored anime id to an array
    } //Finishes the if condition

the string.match return a custom Array, which is an object that unique to each and always return false if you compare to others, that's why the filter not work. the match result contains not only the match strings but also the match group, which can be access by result[1]/result[2] or RegExp.$1/RegExp.$2, etc. that's why you need to use [0][0] to reach the result id

§
Publicado: 02/07/2021

@indefined Thank you, I was already able to solve that. The problem was what you mentioned here I've already tried changing ChecknapprovedAnimes.push(a.match(/\d+/)); to ChecknapprovedAnimes.push(a.match(/\d+/)[0]);

But that wasn't working because my if condition was also wrong... if (FinalApprovedAnimesArray.length === 0 || FinalApprovedMangasArray.length === 0) But it should be this if (FinalApprovedAnimesArray.length !== 0 || FinalApprovedMangasArray.length !== 0)

Now the only issue I've found is how to make it run every 3 hours, but I'm trying to work on that and see if I can find how to do it properly https://greasyfork.org/en/scripts/428614-approved-entries-notifier-mal

Publicar resposta

Faça o login para publicar uma resposta.