YTRating

Shows the rating of videos in the related videos

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

  1. // ==UserScript==
  2. // @name YTRating
  3. // @namespace absolut-fair.com
  4. // @description Shows the rating of videos in the related videos
  5. // @include http://*youtube.com*
  6. // @include https://*youtube.com*
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
  8. // @version 1.1.3
  9. // @grant unsafeWindow
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM_info
  14. // @grant GM_setClipboard
  15. // @grant GM_addStyle
  16. // @grant GM_openInTab
  17. // ==/UserScript==
  18.  
  19. $(document).ready(function () {
  20. window.setInterval(function() { loadrating(); },1000);
  21. });
  22.  
  23. function loadrating(div,link)
  24. {
  25. $("img").each(function() {
  26. if( $(this).attr("ytrating-preprocessed") ) return;
  27.  
  28. var myhref = $(this).attr("src");
  29. if( typeof myhref == 'undefined') {
  30. //console.log(this);
  31. } else if( myhref.indexOf("ytimg.com") != -1) {
  32. if( !$(this).attr("alt")!="Thumbnail" && !$(this).attr("data-thumb") && !$(this).attr("aria-hidden")!="true" ) $(this).attr("ytrating-queue",1);
  33. }
  34. $(this).attr("ytrating-preprocessed", 1);
  35. });
  36. $('img[alt="Thumbnail"], img[data-thumb], img[aria-hidden="true"], img[ytrating-queue], img.yt-img-shadow').closest('a').each(function (i) {
  37. //console.log("ytrating "+i);
  38. var that=this;
  39. var that_parent = $(that).closest("div,li");
  40. var inda=i;
  41. var hrea = $(this).attr('href');
  42. if( typeof hrea == "undefined" || hrea == null || hrea == "" ) return;
  43.  
  44. if(hrea.indexOf('?v=')==-1) {
  45. if($(this).closest('div[data-context-item-id]').length) {
  46. hrea = "/watch?v="+($(this).closest('div[data-context-item-id]').attr("data-context-item-id"));
  47. }
  48. else return true;
  49. }
  50. hrea = hrea.replace("&","&");
  51. hrea=hrea+"&";
  52. var vidid = hrea.between('?v=','&');
  53. /*
  54. if( $(this).attr('done') || $('.video_time',that_parent).attr('done') ) {
  55. return true;
  56. }
  57. else {
  58. $(this).attr('done','1');
  59. $('.video_time',that_parent).attr('done','1');
  60. }
  61. */
  62. if( $("span.ytd-thumbnail-overlay-time-status-renderer > label", that_parent).length ) return true;
  63.  
  64. var nowdate = Math.floor(Date.now() / 1000);
  65. var cachedvalue = GM_getValue("ytrating_cache_"+vidid,"nothing|0").split("|");
  66. //console.log("cache", vidid, cachedvalue);
  67. //$(that_parent).attr("data-debug","this is "+vidid);
  68. if( cachedvalue[1] < nowdate - 2*24*60*60 ) {
  69. //console.log("loadlive", vidid);
  70. GM_xmlhttpRequest({
  71. method: "GET",
  72. url: "https://www.googleapis.com/youtube/v3/videos?id="+vidid+"&key=AIzaSyBUB918luaFnGAoT6x6-l4agPIb8RcgxN0&part=statistics&fields=items/statistics,items/snippet",
  73. onload: function(resp) {
  74. var jsondata = jQuery.parseJSON(resp.responseText);
  75. var items = jsondata.items;
  76.  
  77. let appendstr;
  78. if(typeof items[0] !== 'undefined')
  79. {
  80. var stats = items[0].statistics;
  81. var likes = parseInt(stats.likeCount);
  82. var dislikes = parseInt(stats.dislikeCount);
  83.  
  84. var resul = nFormatter(likes)+"/"+nFormatter(dislikes);
  85. var dr = (likes/(likes+dislikes))*100;
  86. dr=Math.round(dr);
  87.  
  88. if((likes+dislikes)>80) {
  89. if(dr > 70) appendstr="<label style='color:#82FA58;font-size:10px;'> "+resul+"</label>";
  90. else if(dr > 40) appendstr="<label style='color:#C9C618;font-size:10px;'> "+resul+"</label>";
  91. else appendstr="<label style='color:red;font-size:10px;'> "+resul+"</label>";
  92. }
  93. else {
  94. if((likes+dislikes)==0) appendstr="<label style='color:red; font-size:10px;'> OFF</label>";
  95. else appendstr="<label style='color:#848484; font-size:10px;'> "+resul+"</label>";
  96. }
  97. }
  98. else appendstr="<label style='color:red;font-size:10px;'> ERR</label>";
  99. GM_setValue("ytrating_cache_"+vidid, appendstr+"|"+nowdate);
  100. $('span.ytd-thumbnail-overlay-time-status-renderer', that_parent).attr("loadlive",1).append(appendstr);
  101. }
  102. });
  103. }
  104. else {
  105. //console.log("loadcache", that_parent, cachedvalue, $('span.ytd-thumbnail-overlay-time-status-renderer', that_parent));
  106. $('span.ytd-thumbnail-overlay-time-status-renderer', that_parent).attr("loadcache",1).append(cachedvalue[0]);
  107. }
  108. //return false;
  109. });
  110. $('span.ytd-thumbnail-overlay-time-status-renderer').each(function() {
  111. if($("label", this).length > 1) $("label",this).not(':first').remove();
  112. });
  113. }
  114.  
  115.  
  116. String.prototype.between = function(prefix, suffix) {
  117. const s = this;
  118. var i = s.indexOf(prefix);
  119. if (i >= 0) {
  120. s = s.substring(i + prefix.length);
  121. }
  122. else {
  123. return '';
  124. }
  125. if (suffix) {
  126. i = s.indexOf(suffix);
  127. if (i >= 0) {
  128. s = s.substring(0, i);
  129. }
  130. else {
  131. return '';
  132. }
  133. }
  134. return s;
  135. }
  136.  
  137. function nFormatter(num) {
  138. if (num >= 1000000) {
  139. return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'm';
  140. }
  141. if (num >= 1000) {
  142. return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'k';
  143. }
  144. return num;
  145. }