Friends Average Score - MAL

Show all your friends weighted score for an entry.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Friends Average Score - MAL
// @namespace    https://greasyfork.org/en/users/670188-hacker09?sort=daily_installs
// @version      10
// @description  Show all your friends weighted score for an entry.
// @author       hacker09
// @include      /^https:\/\/myanimelist\.net\/(anime|manga)(id=)?(\.php\?id=)?\/?\d+\/?(?!.*\/).*(\?q=.*&cat=anime|manga)?$/
// @icon         https://t3.gstatic.com/faviconV2?client=SOCIAL&type=FAVICON&fallback_opts=TYPE,SIZE,URL&url=http://myanimelist.net&size=64
// @run-at       document-end
// @grant        none
// ==/UserScript==

(async function() {
  'use strict';
  document.querySelector("div.fl-l.score").style.cursor = 'pointer'; //Make the score look like it's clickable
  document.querySelector("div.fl-l.score").onclick = async function GetScores() { //Creates a new function to run when the mouse is hovering the page
    var Average; //Global Variable
    var array = []; //Create a new array
    var nextpagenum = 0; //Create a variable to hold the page number
    const increaseby = 75; //Create a variable to Increase the list page number

    while (true) { //Fetch until there's no next page
      const response = await fetch(document.querySelector("a[href*='stats']").href.split('?')[0] + '?show=' + nextpagenum); //Fetch
      const html = await response.text(); //Gets the fetch response
      const newDocument = new DOMParser().parseFromString(html, 'text/html'); //Parses the fetch response
      nextpagenum += increaseby; //Increase the next page number
      newDocument.querySelectorAll("table.table-recently-updated > tbody > tr > td:nth-child(2)").forEach(async function(el) { //For each friend score
        if (el.innerText.match(/\d+/) !== null) //If the score is not = -
        { //Start the if condition
          array.push(parseInt(el.innerText.match(/\d+/)[0])); //Add each score to an array
          var ScoresTotal = array.filter(Boolean).map(i => Number(i)).reduce((a, b) => a + b); //Sum all scores
          Average = (ScoresTotal / array.length).toFixed(2); //Divide all scores by total amount of scores and convert the average to show numbers only up to 2 decimal places
        } //Finishes the if condition
      }); //Finishes the for each loop
      document.querySelector("div.fl-l.score").title = 'Average based on 0 Friend scores.'; //Show the amount of friends friends score
      if (response.status === 404) { //If the next page does not exist
        if (Average !== undefined) //If the Average is not = undefined
        { //Start the if condition
          document.querySelector("div.fl-l.score").dataset.title += '(' + Average + ')'; //Show the friends entry average
          document.querySelector("div.fl-l.score").title = 'Average based on ' + array.length + ' Friend scores.'; //Show the amount of friends friends score
        } //Finishes the if condition
        return; //End the fetching loop
      } //Finishes the if condition
      await new Promise(resolve => setTimeout(resolve, 600)); //Wait 600 ms to fetch the next page
    } //Finishes the while condition
  }; //Finishes the onmousemove event listener
})();