Greasy Fork 支持简体中文。

B站♥视频评级

计算B站视频的(点赞数+投币数+收藏数+分享数)与播放量的比率并显示出来

// ==UserScript==
// @name         B站♥视频评级
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  计算B站视频的(点赞数+投币数+收藏数+分享数)与播放量的比率并显示出来
// @author       Zola
// @match        https://www.bilibili.com/video/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function parseCount(text) {
        let count = 0;
        if (text.includes('万')) {
            count = parseFloat(text) * 10000;
        } else if (text.includes('亿')) {
            count = parseFloat(text) * 100000000;
        } else {
            count = parseInt(text.replace(/,/g, ''));
        }
        return isNaN(count) ? 0 : count;
    }

    function calculateRatio(totalInteractions, viewCount) {
        const ratio = ((totalInteractions / viewCount) * 100).toFixed(2);
        return ratio;
    }

    function getRating(ratio) {
        let rating = '';
        const ratioValue = parseFloat(ratio);

        if (ratioValue > 30) {  // 满分视频
            rating = '<span style="color:#02c8d3">满分视频</span>';
        } else if (ratioValue >= 19) {  // 9.5 * 2
            rating = '<span style="color:green">好评如潮</span>';
        } else if (ratioValue >= 16) {  // 8.0 * 2
            rating = '<span style="color:limegreen">特别好评</span>';
        } else if (ratioValue >= 14) {  // 7.0 * 2
            rating = '<span style="color:yellowgreen">多半好评</span>';
        } else if (ratioValue >= 8) {  // 4.0 * 2
            rating = '<span style="color:orange">褒贬不一</span>';
        } else if (ratioValue >= 4) {  // 2.0 * 2
            rating = '<span style="color:orangered">多半差评</span>';
        } else if (ratioValue >= 1) {  // 1.0 * 2
            rating = '<span style="color:red">差评如潮</span>';
        } else {  // 低于1
            rating = '<span style="color:grey">惨不忍睹</span>';
        }

        return rating;
    }


    function addRatio() {
        const viewElement = document.querySelector('.view-text');
        const likeElement = document.querySelector('.video-like-info.video-toolbar-item-text');
        const coinElement = document.querySelector('.video-coin-info.video-toolbar-item-text');
        const favElement = document.querySelector('.video-fav-info.video-toolbar-item-text');
        const shareElement = document.querySelector('.video-share-info-text');

        if (viewElement) {
            const viewText = viewElement.innerText.trim();
            const likeText = likeElement ? likeElement.innerText.trim().replace(/,/g, '') : '0';
            const coinText = coinElement ? coinElement.innerText.trim().replace(/,/g, '') : '0';
            const favText = favElement ? favElement.innerText.trim().replace(/,/g, '') : '0';
            const shareText = shareElement ? shareElement.innerText.trim().replace(/,/g, '') : '0';

            console.log('View text:', viewText);
            console.log('Like text:', likeText);
            console.log('Coin text:', coinText);
            console.log('Fav text:', favText);
            console.log('Share text:', shareText);

            const viewCount = parseCount(viewText);
            const likeCount = parseCount(likeText);
            const coinCount = parseCount(coinText);
            const favCount = parseCount(favText);
            const shareCount = parseCount(shareText);

            const totalInteractions = likeCount + coinCount + favCount + shareCount;

            if (!isNaN(viewCount) && !isNaN(totalInteractions)) {
                let displayText;
                let ratingText;
                if (viewCount < 1000) {
                    displayText = '播放不足';
                    ratingText = '';
                } else {
                    const ratio = calculateRatio(totalInteractions, viewCount);
                    const rating = getRating(ratio);
                    displayText = `好评:${(ratio * 5).toFixed(1)}%`;
                    ratingText = rating;
                }

                // 检查是否已经存在比率元素,避免重复添加
                let ratioElement = document.querySelector('.video-like-ratio');
                let ratingElement = document.querySelector('.video-like-rating');
                if (ratioElement) {
                    ratioElement.querySelector('.video-like-ratio-info').innerText = displayText;
                } else {
                    // 创建新的元素来显示比率
                    ratioElement = document.createElement('div');
                    ratioElement.className = 'toolbar-left-item-wrap';
                    ratioElement.setAttribute('data-v-1359e6fc', '');
                    ratioElement.innerHTML = `
                        <div title="互动播放比" class="video-like-ratio video-toolbar-left-item" data-v-1359e6fc="">
                            <span class="video-like-ratio-info video-toolbar-item-text">${displayText}</span>
                        </div>
                    `;
                    // 将新的元素添加到 .video-toolbar-left-main 中
                    const toolbarLeftMain = document.querySelector('.video-toolbar-left-main');
                    if (toolbarLeftMain) {
                        toolbarLeftMain.appendChild(ratioElement);
                        console.log('Ratio element added');
                    } else {
                        console.log('Toolbar left main element not found');
                    }
                }

                if (ratingElement) {
                    ratingElement.querySelector('.video-like-rating-info').innerHTML = ratingText;
                } else {
                    // 创建新的元素来显示评级
                    ratingElement = document.createElement('div');
                    ratingElement.className = 'toolbar-left-item-wrap';
                    ratingElement.setAttribute('data-v-1359e6fc', '');
                    ratingElement.innerHTML = `
                        <div title="评价" class="video-like-rating video-toolbar-left-item" data-v-1359e6fc="">
                            <span class="video-like-rating-info video-toolbar-item-text">${ratingText}</span>
                        </div>
                    `;
                    // 将新的元素添加到 .video-toolbar-left-main 中
                    const toolbarLeftMain = document.querySelector('.video-toolbar-left-main');
                    if (toolbarLeftMain) {
                        toolbarLeftMain.appendChild(ratingElement);
                        console.log('Rating element added');
                    } else {
                        console.log('Toolbar left main element not found');
                    }
                }
            } else {
                console.log('Invalid view count or total interactions');
            }
        } else {
            console.log('View element not found');
        }
    }

    function refreshRatioPeriodically() {
        setInterval(() => {
            console.log('Refreshing ratio');
            addRatio();
        }, 3000); // 每3秒刷新一次
    }

    window.addEventListener('load', () => {
        console.log('Page loaded');
        refreshRatioPeriodically();
    });
})();