Anilist Audience Score

9/2/2024, 12:22:50 PM

当前为 2024-09-02 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Anilist Audience Score
// @namespace   Violentmonkey Scripts
// @match       https://anilist.co/*
// @grant       none
// @version     1.0
// @author      dylan-dang
// @description 9/2/2024, 12:22:50 PM
// @license MIT
// ==/UserScript==

const query = `
query ($userId: Int, $userName: String, $type: MediaType) {
  MediaListCollection(userId: $userId, userName: $userName, type: $type) {
    lists {
      name
      isCustomList
      isCompletedList: isSplitCompletedList
      entries {
        ...mediaListEntry
      }
    }
  }
}

fragment mediaListEntry on MediaList {
  status
  score
  media {
    type
    format
    status(version: 2)
    episodes
    volumes
    chapters
    averageScore
    popularity
  }
}`;

const observer = new MutationObserver(onMutation)

observer.observe(document, {
  childList: true,
  subtree: true,
});


let prevPathname = '';
let prevPath = [];

function isUserpage(path) {
  return path.length === 2 && path[0].toLowerCase() === 'user';
}

function onMutation(mutList) {
  const pathname = window.location.pathname;
  const path = pathname.split('/').filter(Boolean);
  if (pathname != prevPathname) {
    // detect user navigated between two different user pages since stats element won't be readded
    if (isUserpage(prevPath) && isUserpage(path)) {
      const audienceStat = document.querySelector('#audience-score');
      refreshAudienceScore(audienceStat, {
        type: 'ANIME',
        userName: path[1],
      });
    }
    prevPathname = pathname;
    prevPath = path;
  }

  for (const mut of mutList) {
    for (const node of mut.addedNodes) {
      const stats = node.querySelector?.('.list-stats>.stats-wrap');
      if (!stats) continue;
      if (!isUserpage(path)) return;

      // clone a stat element and add audience score
      const audienceStat = stats.lastChild.cloneNode(true);
      audienceStat.id = 'audience-score'
      audienceStat.querySelector('.label').textContent = 'Audience Score'
      audienceStat.querySelector('.value').textContent = '...';
      stats.appendChild(audienceStat);

      refreshAudienceScore(audienceStat, {
        type: 'ANIME',
        userName: path[1],
      });
    }
  }
}

function refreshAudienceScore(audienceStat, variables) {
  audienceStat.querySelector('.value').textContent = '...';

  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
    },
    body: JSON.stringify({ query, variables }),
  };
  fetch('https://graphql.anilist.co', options)
    .then((res) => res.json())
    .then(({data: {MediaListCollection: {lists}}}) => {
      const completed = lists.flatMap((list) => list.entries).filter(entry => entry.status === 'COMPLETED');
      const sumAudienceScore = completed.reduce((a, b) => a + b.media.averageScore, 0);
      const avgAudienceScore = sumAudienceScore / completed.length;
      // abort if client changed page before finishing
      if (window.location.pathname.split('/').filter(Boolean)[1] != variables.userName) return;
      audienceStat.querySelector('.value').textContent = avgAudienceScore.toFixed(1);
    });
}