显示youtube好评/差评比例(好评占比)

治好了我每次看到好评和差评时都忍不住心算一下好评占比的强迫症

目前為 2020-05-18 提交的版本,檢視 最新版本

// ==UserScript==
// @name            显示youtube好评/差评比例(好评占比)
// @name:en         Show likes/dislikes ratio of YouTube video
// @namespace       http://tampermonkey.net/
// @version         0.5.1
// @description     治好了我每次看到好评和差评时都忍不住心算一下好评占比的强迫症
// @description:en  Show likes/dislikes ratio of YouTube video.
// @author          SSmJaE
// @match           https://www.youtube.com/*
// @grant           GM_xmlhttpRequest
// @license         MIT
// ==/UserScript==

const USER_SETTINGS = {
    showAsideVideoRatio: true,
    AsideVideoCount: 20, //显示前n个视频的ratio,过多可能会导致网页卡顿
    checkInterval: 5000,
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

function calculate_ratio(upCount, downCount) {
    upCount = parseInt(upCount.replace(/[^0-9]/ig, ""));
    downCount = parseInt(downCount.replace(/[^0-9]/ig, ""));
    let ratio = Math.round(upCount * 1000 / (upCount + downCount)) / 10;
    if (upCount > 0 && !downCount) ratio = 100;
    if (isNaN(ratio)) ratio = 0; //只有0/0会为NaN
    return ratio + "%"
}

async function handle_aside() {
    let asideOrder = 0;
    let videos = document.querySelectorAll('#thumbnail[href]');
    console.log(1)
    for (let video of videos) {
        console.log(asideOrder)
        // if (video.parentElement.parentElement.parentElement.parentElement.parentElement.hasAttribute('hidden')) continue;
        // if (video.childElementCount < 3) {
        if (asideOrder >= USER_SETTINGS.AsideVideoCount) break;
        await sleep(300); //避免请求过快被ban ip
        console.log(video.getAttribute('href'))
        try {
            fetch('https://www.youtube.com/' + video.getAttribute('href')).then(response => response.text()).then(text => {
                let tooltip = /\"INDIFFERENT\",\"tooltip\":\"(.*?)\"}/.exec(text)[1];
                console.log(tooltip);

                let div = document.createElement('div');
                div.classList.add('style-scope', 'ytd-video-meta-block')
                div.textContent = calculate_ratio(tooltip.split('/')[0], tooltip.split('/')[1]);
                video.parentElement.nextElementSibling.querySelector('#metadata').appendChild(div);
            });
        } catch (error) {
            console.log(error)
        };

        asideOrder++;
    };
}

function handle_main() {
    try {
        let menuBar = document.querySelector('div#info div#menu div#top-level-buttons');
        let up = menuBar.childNodes[0].querySelector('[id="text"]');
        let down = menuBar.childNodes[1].querySelector('[id="text"]');
        let shareButton = menuBar.childNodes[2].querySelector('[id="text"]');
        shareButton.textContent = calculate_ratio(up.getAttribute('aria-label'), down.getAttribute('aria-label'));
    } catch (e) {
        console.error(e);
    };
}

var bufferUrl = "";
setInterval(() => {
    handle_main();
    if (bufferUrl != location.href)
        if (USER_SETTINGS.showAsideVideoRatio)
            handle_aside();

    bufferUrl = location.href;
}, USER_SETTINGS.checkInterval);

QingJ © 2025

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