Who Defriended ME!? - MAL

Now you can easily and quickly know who DeFriended you on MAL!

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Who Defriended ME!? - MAL
// @namespace    MALDeFriendStalker
// @version      0.4
// @description  Now you can easily and quickly know who DeFriended you on MAL!
// @author       hacker09
// @include      https://myanimelist.net/profile/*/friends
// @include      https://myanimelist.net/myfriends.php?go=remove&id=*
// @icon         https://www.google.com/s2/favicons?domain=myanimelist.net
// @grant        GM_deleteValue
// @grant        GM_listValues
// @grant        GM_getValue
// @grant        GM_setValue
// @run-at       document-end
// ==/UserScript==
(function() {
  'use strict';
  var ScriptUserName = document.querySelector("a.header-profile-link").innerText; //Get the script user user name
  if (location.href.match('https://myanimelist.net/profile/' + ScriptUserName + '/friends')) //Make the script work only on the script user friend list
  { //Starts the if condition
    var PastFriends = []; //Creates a new global array
    var ActualFriendsArray = []; //Creates a new global array

    var WhoDeFriendedMEBTN = document.createElement("a"); //Creates a button element
    WhoDeFriendedMEBTN.innerHTML = "Check Who DeFriended ME!"; //Adds a text to the button
    document.querySelector("a.header-right.mt4.mr0").parentElement.appendChild(WhoDeFriendedMEBTN); //Add the button to the page
    WhoDeFriendedMEBTN.setAttribute("id", "WhoDeFriendedMEBTN"); //Set an id to the button
    WhoDeFriendedMEBTN.setAttribute("style", "cursor: pointer;margin-left: 15px;font-size: 10px;"); //Set the button css style
    document.querySelector("#WhoDeFriendedMEBTN").addEventListener("click", GetNewAndActualFriendsAndShowWhoDeFriendedME, false); //Add a click advent listener to the button

    for (var i = GM_listValues().length; i--;) { //For every single MAL friend saved on tampermonkey
      PastFriends.push(GM_listValues()[i]); //Add all Past MAL Friends saved on tampermonkey to an array
    } //Finishes the for condition

    var nextpagenum = 0; //Creates a new variable
    var increaseby = 1; //Creates a new variable
    async function GetNewAndActualFriendsAndShowWhoDeFriendedME() { //Creates an async function
      while (true) { //While the fetched page constains 100 friends
        nextpagenum += increaseby; //Increase the page number
        const url = 'https://api.jikan.moe/v3/user/' + ScriptUserName + '/friends/' + nextpagenum; //Creates a variable to fetch the friend list and the next pages
        var JsonResponse = await (await fetch(url)).json(); //Fetches the friend list and the next pages and converts the fetched pages to json

        for (var i = JsonResponse.friends.length; i--;) { //For every single MAL friend
          GM_setValue(JsonResponse.friends[i].username, 'Actual MAL Friend'); //Get and save the actual mal friend and store the username
          ActualFriendsArray.push(JsonResponse.friends[i].username); //Add all Actual MAL Friends to an array
        } //Finishes the for condition

        if (JsonResponse.friends.length !== 100) { //If the fetched page doesn't have 100 friends on it
          var WhoDefriendedME = PastFriends.filter(d => !ActualFriendsArray.includes(d)); //Creates a variable to get the Past Friend User Names that tampermonkey has and check which Past Friends User Names are currently Missing
          if (WhoDefriendedME.length > 0) //If someone defriended you
          { //Starts the if condition
            alert('You was DeFriended by:\n' + WhoDefriendedME); //Shows who DeFriended you!
            for (var i = WhoDefriendedME.length; i--;) { //For every single MAL friend that defriended you and is saved on tampermonkey
              GM_deleteValue(WhoDefriendedME[i]); //Remove the defriended user name of the script storage
            } //Finishes the for condition
          } //Finishes the if condition
          else //If nobody defriended you
          { //Starts the else condition
            alert('Your current Friends list was updated and saved!\n Nobody Has DeFriended you!'); //Get the Past Friend User Names that tampermonkey has and check which Past Friends User Names are Missing and shows who DeFriended you!
          } //Finishes the else condition
          return;
        } //Finishes the if condition
        await new Promise(resolve => setTimeout(resolve, 600)); //Timeout to start the next fetch request
      } //Finishes the while condition
    } //Finishes the async function

  } //Finishes the if condition
  else //If the user is defriending a friend on the page https://myanimelist.net/myfriends.php?go=remove&id=
  { //Starts the else condition
    document.querySelector("input.inputButton").onclick = function() { //Detects the mouse click on the 'Remove Friend' button
      GM_deleteValue(document.querySelector("td.dialog-text > strong").innerText); //Remove the defriended user name of the script storage
    }; //Finishes the onclick advent listener
  } //Finishes the else condition
})();