YTRating

Shows the rating of videos in the related videos

当前为 2017-09-14 提交的版本,查看 最新版本

  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.1.0
  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. var myhref = $(this).attr("src");
  28. if( typeof myhref == 'undefined') {
  29. //console.log(this);
  30. } else if( myhref.indexOf("ytimg.com") != -1) {
  31. if( !$(this).attr("alt")!="Thumbnail" && !$(this).attr("data-thumb") && !$(this).attr("aria-hidden")!="true" ) $(this).attr("ytrating-queue",1);
  32. }
  33. $(this).attr("ytrating-preprocessed", 1);
  34. });
  35. $('img[alt="Thumbnail"], img[data-thumb], img[aria-hidden="true"], img[ytrating-queue], img.yt-img-shadow').closest('a').each(function (i) {
  36. //console.log("ytrating "+i);
  37. var that=this;
  38. var that_parent = $(that).closest("div,li");
  39. var inda=i;
  40. var hrea = $(this).attr('href');
  41. if( typeof hrea == "undefined" || hrea == null || hrea == "" ) return;
  42. if(hrea.indexOf('?v=')==-1) {
  43. if($(this).closest('div[data-context-item-id]').length) {
  44. hrea = "/watch?v="+($(this).closest('div[data-context-item-id]').attr("data-context-item-id"));
  45. }
  46. else return true;
  47. }
  48. hrea = hrea.replace("&","&");
  49. hrea=hrea+"&";
  50. var vidid = hrea.between('?v=','&');
  51. /*
  52. if( $(this).attr('done') || $('.video_time',that_parent).attr('done') ) {
  53. return true;
  54. }
  55. else {
  56. $(this).attr('done','1');
  57. $('.video_time',that_parent).attr('done','1');
  58. }
  59. */
  60. if( $("span.ytd-thumbnail-overlay-time-status-renderer > label", that_parent).length ) return true;
  61.  
  62. var nowdate = Math.floor(Date.now() / 1000);
  63. var cachedvalue = GM_getValue("ytrating_cache_"+vidid,"nothing|0").split("|");
  64. //console.log("cache", vidid, cachedvalue);
  65. //$(that_parent).attr("data-debug","this is "+vidid);
  66. if( cachedvalue[1] < nowdate - 2*24*60*60 ) {
  67. //console.log("loadlive", vidid);
  68. GM_xmlhttpRequest({
  69. method: "GET",
  70. url: "https://www.googleapis.com/youtube/v3/videos?id="+vidid+"&key=AIzaSyBUB918luaFnGAoT6x6-l4agPIb8RcgxN0&part=statistics&fields=items/statistics,items/snippet",
  71. onload: function(resp) {
  72. var jsondata = jQuery.parseJSON(resp.responseText);
  73. var items = jsondata.items;
  74.  
  75. if(typeof items[0] !== 'undefined')
  76. {
  77. var stats = items[0].statistics;
  78. var likes = parseInt(stats.likeCount);
  79. var dislikes = parseInt(stats.dislikeCount);
  80.  
  81. var resul = likes+"/"+dislikes;
  82. var dr = (likes/(likes+dislikes))*100;
  83. dr=Math.round(dr);
  84.  
  85. if((likes+dislikes)>80) {
  86. if(dr > 70) appendstr="<label style='color:#82FA58;font-size:18px;'> "+dr+"%</label>";
  87. else if(dr > 40) appendstr="<label style='color:#C9C618;font-size:18px;'> "+dr+"%</label>";
  88. else appendstr="<label style='color:red;font-size:18px;'> "+dr+"%</label>";
  89. }
  90. else {
  91. if((likes+dislikes)==0) appendstr="<label style='color:red; font-size:18px;'> OFF</label>";
  92. else appendstr="<label style='color:#848484; font-size:18px;'> "+dr+"%</label>";
  93. }
  94. }
  95. else appendstr="<label style='color:red;font-size:18px;'> ERR</label>";
  96. GM_setValue("ytrating_cache_"+vidid, appendstr+"|"+nowdate);
  97. $('span.ytd-thumbnail-overlay-time-status-renderer', that_parent).attr("loadlive",1).append(appendstr);
  98. }
  99. });
  100. }
  101. else {
  102. //console.log("loadcache", that_parent, cachedvalue, $('span.ytd-thumbnail-overlay-time-status-renderer', that_parent));
  103. $('span.ytd-thumbnail-overlay-time-status-renderer', that_parent).attr("loadcache",1).append(cachedvalue[0]);
  104. }
  105. //return false;
  106. });
  107. }
  108.  
  109.  
  110. String.prototype.between = function(prefix, suffix) {
  111. s = this;
  112. var i = s.indexOf(prefix);
  113. if (i >= 0) {
  114. s = s.substring(i + prefix.length);
  115. }
  116. else {
  117. return '';
  118. }
  119. if (suffix) {
  120. i = s.indexOf(suffix);
  121. if (i >= 0) {
  122. s = s.substring(0, i);
  123. }
  124. else {
  125. return '';
  126. }
  127. }
  128. return s;
  129. }