VIBDataListInsertion

Add VIB Rank, stars, VIB_score decimal, and rating count to anime list based on API data

目前为 2023-09-28 提交的版本。查看 最新版本

// ==UserScript==
// @name         VIBDataListInsertion
// @namespace    https://jirehlov.com
// @version      0.1.1
// @description  Add VIB Rank, stars, VIB_score decimal, and rating count to anime list based on API data
// @include      /^https?://(bangumi\.tv|bgm\.tv|chii\.in)/(.+?/list|.+?/tag|.+?/browser|subject_search|index)(/|\?).+$/
// @author       Jirehlov
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
    const processedElements = new Set();
    async function addVIBRankAndInfoToElement(rateInfoElement) {
        if (processedElements.has(rateInfoElement)) {
            return;
        }
        const existingRankElement = rateInfoElement.nextElementSibling;
        if (existingRankElement && existingRankElement.classList.contains('rank')) {
            processedElements.add(rateInfoElement);
            return;
        }
        const h3Element = rateInfoElement.parentElement.querySelector('h3');
        if (!h3Element) {
            return;
        }
        const match = h3Element.querySelector('a').getAttribute('href').match(/\/(\d+)$/);
        if (!match) {
            return;
        }
        const id = match[1];
        const apiUrl = `https://api.jirehlov.com/vib/${id}`;
        const response = await fetch(apiUrl, { redirect: 'manual' });
        if (response.status === 301) {
            return;
        }
        if (response.ok) {
            const data = await response.json();
            const vibRank = data.VIB_rank;
            const newRankElement = document.createElement('span');
            newRankElement.classList.add('rank');
            newRankElement.innerHTML = `<small>VIB Rank </small>${vibRank}`;
            newRankElement.style.right = '85px';
            rateInfoElement.insertAdjacentElement('afterend', newRankElement);
            processedElements.add(rateInfoElement);
            const vibScore = data.VIB_score;
            if (!isNaN(vibScore)) {
                const starsElement = document.createElement('span');
                starsElement.classList.add('starstop-s');
                const starCount = Math.round(vibScore);
                starsElement.innerHTML = `<span class="starlight stars${starCount}"></span>`;
                const newRateInfoElement = document.createElement('p');
                newRateInfoElement.classList.add('rateInfo');
                newRateInfoElement.appendChild(starsElement);
                const vibScoreDecimal = parseFloat(vibScore).toFixed(1);
                const vibEnum = data.VIB_enum;
                newRateInfoElement.innerHTML += `<small class="fade"> ${vibScoreDecimal}</small> <span class="tip_j">(${vibEnum}人VIB评分)</span>`;
                rateInfoElement.insertAdjacentElement('afterend', newRateInfoElement);
            }
        }
    }
    const observer = new MutationObserver((mutationsList, observer) => {
        mutationsList.forEach((mutation) => {
            if (mutation.type === 'childList') {
                mutation.addedNodes.forEach((node) => {
                    if (node.nodeType === Node.ELEMENT_NODE && node.querySelector('p.rateInfo')) {
                        const rateInfoElements = node.querySelectorAll('p.rateInfo');
                        rateInfoElements.forEach(addVIBRankAndInfoToElement);
                    }
                });
            }
        });
    });
    const initialRateInfoElements = document.querySelectorAll('p.rateInfo');
    initialRateInfoElements.forEach(addVIBRankAndInfoToElement);
    const browserItemList = document.getElementById('browserItemList');
    if (browserItemList) {
        observer.observe(browserItemList, { childList: true, subtree: true });
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址