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

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

目前为 2020-06-29 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 显示YouTube好评/差评比例(好评占比)
  3. // @name:en yet another simple YouTube video likes/dislikes ratio display
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.6.0
  6. // @description 治好了我每次看到好评和差评时都忍不住心算一下好评占比的强迫症
  7. // @description:en Show likes/dislikes ratio of YouTube video.
  8. // @author SSmJaE
  9. // @match https://www.youtube.com/*
  10. // @license GPL-3.0
  11. // @compatible chrome
  12. // ==/UserScript==
  13.  
  14. const USER_SETTINGS = {
  15. checkInterval: 5000, //多久检查一次是否有新的视频|determine how often to check if there are any new videos
  16. showAllRatios: true, //是否显示所有视频的好评率,如果关闭,只显示主视频(也就是正在播放的视频)的好评率|whether show all videos' ratios
  17. sleepInterval: 500, //间隔多久查询下一个视频,避免请求过快被ban ip|determine to wait how much time to check next video, for avoiding being banned IP for checking too fast
  18. colorfulRatio: true, //是否显示彩色好评率,默认开启,关闭的话,显示灰色|whether to show different colors for the ratio, or just grey
  19. };
  20.  
  21. function sleep(ms) {
  22. return new Promise(resolve => setTimeout(resolve, ms));
  23. }
  24.  
  25. function calculate_ratio([upCount, downCount]) {
  26. upCount = parseInt(upCount.replace(/[^0-9]/ig, ""), 10);
  27. downCount = parseInt(downCount.replace(/[^0-9]/ig, ""), 10);
  28. let ratio = Math.round(upCount * 1000 / (upCount + downCount)) / 10;
  29.  
  30. if (upCount > 0 && !downCount) ratio = 100;//正无穷
  31. if (isNaN(ratio)) ratio = 0; //只有0/0会为NaN
  32. return ratio + "%";
  33. }
  34.  
  35. async function handle_all() {
  36. for (let video of document.querySelectorAll('#thumbnail[href]')) {
  37. if (video.parentElement.parentElement.parentElement.parentElement.parentElement
  38. .hasAttribute('hidden')) continue; //跳过被隐藏的视频
  39. if (video.classList.contains('ratio')) continue;
  40.  
  41. await sleep(USER_SETTINGS.sleepInterval);
  42. try {
  43. fetch(video.getAttribute('href'), { credentials: 'omit' }).then(response => response.text()).then(text => {
  44. if (!video.classList.contains('ratio')) { //跳过已添加ratio的视频
  45. let tooltip = /"INDIFFERENT","tooltip":"(.*?)"/.exec(text)[1];
  46. let ratio = calculate_ratio(tooltip.split('/'));
  47.  
  48. let ratioDiv = document.createElement('div');
  49. ratioDiv.classList.add('style-scope', 'ytd-video-meta-block', 'ratio');
  50. ratioDiv.textContent = ratio;
  51. let color = '';
  52. if (USER_SETTINGS.colorfulRatio) {
  53. if (parseInt(ratio, 10) >= 99) {
  54. color = 'rgb(30,144,255)';
  55. } else if (parseInt(ratio, 10) >= 90) {
  56. color = 'rgb(98,198,70)';
  57. } else if (parseInt(ratio, 10) >= 80) {
  58. color = 'rgb(255,192,0)';
  59. } else {
  60. color = 'rgb(207,32,0)';
  61. }
  62. }
  63. ratioDiv.style.color = color;
  64.  
  65. let dot = document.createElement('div');
  66. dot.textContent = "•";
  67. dot.style.margin = "0 4px";
  68.  
  69. video.parentElement.nextElementSibling.querySelector('#metadata-line').appendChild(dot);
  70. video.parentElement.nextElementSibling.querySelector('#metadata-line').appendChild(ratioDiv);
  71. video.classList.add('ratio');
  72. }
  73. });
  74. } catch (error) { }
  75. }
  76. setTimeout(handle_all, USER_SETTINGS.checkInterval);
  77. }
  78.  
  79. function handle_main() {
  80. try {
  81. let menuBar = document.querySelector('div#info div#menu div#top-level-buttons');
  82. let up = menuBar.childNodes[0].querySelector('[id="text"]');
  83. let down = menuBar.childNodes[1].querySelector('[id="text"]');
  84. let shareButton = menuBar.childNodes[2].querySelector('[id="text"]');
  85. shareButton.textContent = calculate_ratio([up.getAttribute('aria-label'), down.getAttribute('aria-label')]);
  86. } catch (e) { }
  87. }
  88.  
  89. /*------------------------------------------------------------------------- */
  90. setInterval(handle_main, USER_SETTINGS.checkInterval);
  91.  
  92. if (USER_SETTINGS.showAllRatios)
  93. setTimeout(handle_all, USER_SETTINGS.checkInterval);

QingJ © 2025

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