Greasy Fork

Greasy Fork is available in English.

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

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

当前为 2020-05-18 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            显示youtube好评/差评比例(好评占比)
// @name:en         Show likes/dislikes ratio of YouTube video
// @namespace       http://tampermonkey.net/
// @version         0.5
// @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: 10, //显示前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('div#related #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);