VIBRankFetch

条目页显示VIB排名

当前为 2023-09-07 提交的版本,查看 最新版本

// ==UserScript==
// @name         VIBRankFetch
// @namespace    https://jirehlov.com
// @version      0.1
// @description  条目页显示VIB排名
// @include      /^https?://(bangumi|bgm|chii).(tv|in)/subject/.*$/
// @author       Jirehlov
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function getVIBRank(id) {
        return fetch(`https://api.jirehlov.com/vib/${id}`, {
            method: 'GET',
            redirect: 'manual'
        })
            .then(response => {
                if (response.status === 200) {
                    return response.json();
                } else {
                    throw new Error('VIB rank api: Response status is not 200 OK');
                }
            })
            .then(data => data.VIB_rank)
            .catch(error => {
                console.error('Error fetching VIB rank:', error);
                return null;
            });
    }

    let id = window.location.pathname.split('/')[2];

    getVIBRank(id)
        .then(vibRank => {
            if (vibRank !== null && vibRank !== 0) {
                const lastDiv = document.querySelector('.global_score > div:last-child');
                if (lastDiv) {
                    const vibDiv = document.createElement('div');
                    vibDiv.innerHTML = '<small class="grey">Very Important Bangumier Ranked:</small><small class="alarm">#' + vibRank + '</small>';
                    vibDiv.style.marginLeft = '38px';
                    lastDiv.after(vibDiv);
                }
            }
        });
})();