Greasy Fork is available in English.
治好了我每次看到好评和差评时都忍不住心算一下好评占比的强迫症
当前为
// ==UserScript==
// @name 显示youtube好评/差评比例(好评占比)
// @name:en Show likes/dislikes ratio of YouTube video
// @namespace http://tampermonkey.net/
// @version 0.5.2
// @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 = {
checkInterval: 5000,
showOthersRatio: true,
otherVideoCount: 20, //显示前n个视频的ratio,过多可能会导致网页卡顿
}
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_others() {
let asideOrder = 0;
let videos = document.querySelectorAll('#thumbnail[href]');
for (let video of videos) {
if (video.parentElement.parentElement.parentElement.parentElement.parentElement.hasAttribute('hidden')) continue; //跳过被隐藏的视频
if (asideOrder >= USER_SETTINGS.otherVideoCount) break;
await sleep(500); //避免请求过快被ban ip
try {
fetch(video.getAttribute('href')).then(response => response.text()).then(text => {
if (!video.classList.contains('ratio')) { //跳过已添加ratio的视频
let tooltip = /\"INDIFFERENT\",\"tooltip\":\"(.*?)\"}/.exec(text)[1];
// console.log(tooltip);
let div = document.createElement('div');
div.classList.add('style-scope', 'ytd-video-meta-block', 'ratio')
div.textContent = calculate_ratio(tooltip.split('/')[0], tooltip.split('/')[1]);
video.parentElement.nextElementSibling.querySelector('#metadata').appendChild(div);
video.classList.add('ratio');
}
});
} 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 (USER_SETTINGS.showOthersRatio)
if (location.href != bufferUrl)
handle_others();
bufferUrl = location.href;
}, USER_SETTINGS.checkInterval);