Advertisement
Kocayine

Untitled

Jul 11th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Youtube Video Ratings
  3. // @description Shows ratings of related videos on Youtube
  4. // @namespace http://userscripts.org/scripts/show/119840
  5. // @version 1.0.8
  6. // @include http://www.youtube.com/*
  7. // @include https://www.youtube.com/*
  8. // @exclude http://www.youtube.com/user/*
  9. // @exclude https://www.youtube.com/user/*
  10. // @exclude http://www.youtube.com/channel/*
  11. // @exclude https://www.youtube.com/channel/*
  12. // ==/UserScript==
  13.  
  14. var loaded = {};
  15. var containerName="yt-thumb-default"
  16. loaded[""] = true;
  17. window.addEventListener (
  18. 'scroll',
  19. function (e) {
  20. iterateClips(document.getElementsByClassName(containerName));
  21. },
  22. false);
  23. var wm = document.getElementById("watch-more-related");
  24. if (wm) {
  25. // On "Load More Suggestions" button click
  26. wm.addEventListener (
  27. 'DOMNodeInserted',
  28. function (e) {
  29. iterateClips(e.target.getElementsByClassName(containerName));
  30. },
  31. false);
  32. }
  33.  
  34. // starts here
  35. iterateClips(document.getElementsByClassName(containerName));
  36.  
  37. function iterateClips(clips)
  38. {
  39. if (clips)
  40. {
  41. for (var i=0; i<clips.length; ++i)
  42. if (isVisible(clips[i]))
  43. requestRating(clips[i]);
  44. }
  45. }
  46.  
  47. function requestRating(box)
  48. {
  49. var id = getVideoId(box);
  50. if (loaded[id])
  51. return;
  52.  
  53. loaded[id] = true;
  54. setTimeout( function() {
  55. GM_xmlhttpRequest({
  56. method: 'GET',
  57. url: "http://gdata.youtube.com/feeds/api/videos/" + id + "?v=2&alt=json&fields=yt:rating",
  58. onload: function(response)
  59. {
  60. if (response.status == 200)
  61. {
  62. var rsp = eval( '(' + response.responseText + ')' );
  63. if (rsp && rsp.entry && rsp.entry.yt$rating)
  64. attachBar(box, parseInt(rsp.entry.yt$rating.numLikes),
  65. parseInt(rsp.entry.yt$rating.numDislikes));
  66. }
  67. else
  68. delete loaded[id]; // give it a chance to reload while scrolling
  69. }
  70. });
  71. }, 0);
  72. }
  73.  
  74. function getVideoId(box)
  75. {
  76. var anchor=box.parentNode.parentNode;
  77. var isAnchorFound = 2;
  78. while (anchor && anchor.tagName != undefined)
  79. {
  80. if (anchor.tagName.toLowerCase()=="a")
  81. break;
  82. anchor = anchor.parentNode;
  83. --isAnchorFound;
  84. if (0==isAnchorFound)
  85. break;
  86. }
  87. if ( isAnchorFound>0 )
  88. {
  89. var href = anchor.getAttribute("href");
  90. if (href)
  91. {
  92. var id = href.replace(/.*v=([^&]*).*/, "$1");
  93. if (id.length<href.length)
  94. return id;
  95. }
  96. }
  97. return "";
  98. }
  99.  
  100. function attachBar(videoThumb, likes, dislikes)
  101. {
  102. var total = likes + dislikes;
  103.  
  104. if (total > 0)
  105. {
  106. var ratingDiv = document.createElement("div");
  107. ratingDiv.setAttribute("class", "video-extras-sparkbarks");
  108. ratingDiv.setAttribute("style", "position: relative; top: 1px;" );
  109. ratingDiv.setAttribute("title", likes + " likes, " + dislikes + " dislikes");
  110.  
  111. var likesDiv = document.createElement("div");
  112. likesDiv.setAttribute("class", "video-extras-sparkbar-likes");
  113. likesDiv.setAttribute("style", "width: "+(100*likes)/total+"%");
  114.  
  115. var dislikesDiv = document.createElement("div");
  116. dislikesDiv.setAttribute("class", "video-extras-sparkbar-dislikes");
  117. dislikesDiv.setAttribute("style", "width: "+(100*dislikes)/total+"%;"+"background: #C00;");
  118.  
  119. ratingDiv.appendChild(likesDiv);
  120. ratingDiv.appendChild(dislikesDiv);
  121. videoThumb.parentNode.parentNode.appendChild(ratingDiv);
  122. //videoThumb.appendChild(ratingDiv);
  123.  
  124. // fixing time element position to be inside of the thumb image
  125. var spans = videoThumb.parentNode.parentNode.getElementsByTagName("span");
  126. for (var i=0; i<spans.length; ++i )
  127. if (spans[i].getAttribute("class")=="video-time")
  128. {
  129. spans[i].style.bottom = "6px";
  130. break;
  131. }
  132. }
  133. }
  134.  
  135. function isVisible ( el )
  136. {
  137. var top = el.offsetTop;
  138. var left = el.offsetLeft;
  139. var width = el.offsetWidth;
  140. var height = el.offsetHeight;
  141.  
  142. while(el.offsetParent) {
  143. el = el.offsetParent;
  144. top += el.offsetTop;
  145. left += el.offsetLeft;
  146. }
  147. return (
  148. top < (window.pageYOffset + window.innerHeight) &&
  149. left < (window.pageXOffset + window.innerWidth) &&
  150. (top + height) > window.pageYOffset &&
  151. (left + width) > window.pageXOffset
  152. );
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement