MALScore

Adds MyAnimeList link and score to AniList anime and manga pages

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name MALScore
// @namespace Ract
// @author Ract
// @description Adds MyAnimeList link and score to AniList anime and manga pages
// @match https://anilist.co/*
// @require      https://greasyfork.org/scripts/5679-wait-for-elements/code/Wait%20For%20Elements.js?version=147465
// @grant GM_xmlhttpRequest
// @version 2
// ==/UserScript==
// Thanks to https://greasyfork.org/en/scripts/5890-kitsu-mal-rating for code reference
var SCRIPT_NAME = 'MAL Score';


var Util = {
    log: function() {
        var args = [].slice.call(arguments);
        args.unshift('%c' + SCRIPT_NAME + ':', 'font-weight: bold;color: #233c7b;');
        console.log.apply(console, args);
    },
    q: function(query, context) {
        return (context || document).querySelector(query);
    },
    qq: function(query, context) {
        return [].slice.call((context || document).querySelectorAll(query));
    }
};

function getRating(malURL,cb){
    GM_xmlhttpRequest({
        method: 'GET',
        url: malURL,
        onload: function(response) {
          try {
            var tempDiv = document.createElement('div');
            tempDiv.innerHTML = response.responseText;

            var sidebar = Util.q('#content > table > tbody > tr > td.borderClass', tempDiv);
            var rating = Util.q('span[itemprop="ratingValue"]', sidebar);
            var headerNum;

            if (Util.q('h2.mt8', sidebar)) headerNum = 4;
            else headerNum = 3;

            if (rating) {
              rating = rating.innerText;
            } else {
              var score = Util.q('h2:nth-of-type(' + headerNum + ') + div', sidebar).innerText.replace(/[\n\r]/g, '');
              if (score.match(/Score:\s+N\/A/)) {
                rating = null;
              } else {
                rating = score.match(/[0-9]{1,2}\.[0-9]{2}/)[0];
              }
            }
            Util.log(rating)
            cb(rating);
          } catch (err) {
            Util.log('Failed to parse MAL page');
          }
        }
        });
}
waitForUrl(function() {
GM_xmlhttpRequest({
    method: "POST",
    url: "https://graphql.anilist.co",
    data: JSON.stringify({
          query: `query ($id: Int,$type:MediaType) {
                      Media (id: $id, type:$type) {
                          idMal
                      }
                  }`,
          variables: {
            "id": window.location.pathname.split("/")[2],
            "type": window.location.pathname.split("/")[1].toUpperCase()
          }
      }),
    headers: {
        "Content-Type": "application/json"
    },
    onload: function(response) {
        var type = window.location.pathname.split("/")[1].toLowerCase();
        var malID=JSON.parse(response.responseText)["data"]["Media"]["idMal"];
        var malURL = "https://myanimelist.net/" + type+ "/"+ malID;
        //Util.log(malURL);
        getRating(malURL,function(rating) {
            //Util.log("rating"+rating);
            waitForElems({
              sel: '.data:not(#malscorebox)',
              stop: true,
              onmatch: function() {
                  //Util.log("ratingfound"+rating);
                  document.getElementsByClassName("data")[0].innerHTML+=`<div id="malscorebox" data-v-ead17872="" class="data-set"><div data-v-ead17872="" class="type">MAL Score</div> <div id="malscore" data-v-ead17872="" class="value"><a rel="noopener noreferrer" href="${malURL}">${rating}</a></div></div>`
              }
            });
        });

    }
});
});