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

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

目前为 2020-05-18 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 显示youtube好评/差评比例(好评占比)
  3. // @name:en Show likes/dislikes ratio of YouTube video
  4. // @namespace http://tampermonkey.net/
  5. // @version 0.5.1
  6. // @description 治好了我每次看到好评和差评时都忍不住心算一下好评占比的强迫症
  7. // @description:en Show likes/dislikes ratio of YouTube video.
  8. // @author SSmJaE
  9. // @match https://www.youtube.com/*
  10. // @grant GM_xmlhttpRequest
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. const USER_SETTINGS = {
  15. showAsideVideoRatio: true,
  16. AsideVideoCount: 20, //显示前n个视频的ratio,过多可能会导致网页卡顿
  17. checkInterval: 5000,
  18. }
  19.  
  20. function sleep(ms) {
  21. return new Promise(resolve => setTimeout(resolve, ms));
  22. }
  23.  
  24. function calculate_ratio(upCount, downCount) {
  25. upCount = parseInt(upCount.replace(/[^0-9]/ig, ""));
  26. downCount = parseInt(downCount.replace(/[^0-9]/ig, ""));
  27. let ratio = Math.round(upCount * 1000 / (upCount + downCount)) / 10;
  28. if (upCount > 0 && !downCount) ratio = 100;
  29. if (isNaN(ratio)) ratio = 0; //只有0/0会为NaN
  30. return ratio + "%"
  31. }
  32.  
  33. async function handle_aside() {
  34. let asideOrder = 0;
  35. let videos = document.querySelectorAll('#thumbnail[href]');
  36. console.log(1)
  37. for (let video of videos) {
  38. console.log(asideOrder)
  39. // if (video.parentElement.parentElement.parentElement.parentElement.parentElement.hasAttribute('hidden')) continue;
  40. // if (video.childElementCount < 3) {
  41. if (asideOrder >= USER_SETTINGS.AsideVideoCount) break;
  42. await sleep(300); //避免请求过快被ban ip
  43. console.log(video.getAttribute('href'))
  44. try {
  45. fetch('https://www.youtube.com/' + video.getAttribute('href')).then(response => response.text()).then(text => {
  46. let tooltip = /\"INDIFFERENT\",\"tooltip\":\"(.*?)\"}/.exec(text)[1];
  47. console.log(tooltip);
  48.  
  49. let div = document.createElement('div');
  50. div.classList.add('style-scope', 'ytd-video-meta-block')
  51. div.textContent = calculate_ratio(tooltip.split('/')[0], tooltip.split('/')[1]);
  52. video.parentElement.nextElementSibling.querySelector('#metadata').appendChild(div);
  53. });
  54. } catch (error) {
  55. console.log(error)
  56. };
  57.  
  58. asideOrder++;
  59. };
  60. }
  61.  
  62. function handle_main() {
  63. try {
  64. let menuBar = document.querySelector('div#info div#menu div#top-level-buttons');
  65. let up = menuBar.childNodes[0].querySelector('[id="text"]');
  66. let down = menuBar.childNodes[1].querySelector('[id="text"]');
  67. let shareButton = menuBar.childNodes[2].querySelector('[id="text"]');
  68. shareButton.textContent = calculate_ratio(up.getAttribute('aria-label'), down.getAttribute('aria-label'));
  69. } catch (e) {
  70. console.error(e);
  71. };
  72. }
  73.  
  74. var bufferUrl = "";
  75. setInterval(() => {
  76. handle_main();
  77. if (bufferUrl != location.href)
  78. if (USER_SETTINGS.showAsideVideoRatio)
  79. handle_aside();
  80.  
  81. bufferUrl = location.href;
  82. }, USER_SETTINGS.checkInterval);

QingJ © 2025

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