// ==UserScript== // @name YouTube Video Ratings Bar with Power Meter // @description Places a bar below YouTube thumbnail images which shows not only the like/dislikes ratings, but also the enthusiasm people have for each videos. // @version 2014.06.09 // @include http://www.youtube.com/* // @include https://www.youtube.com/* // @grant GM_addStyle // @grant GM_xmlhttpRequest // @namespace https://greasyfork.org/users/253 // @downloadURL none // ==/UserScript== GM_addStyle(".video-actions, .video-time {bottom:6px !important;})"); var lastScanTime = new Date().getTime(); function powerMeter(views, likes) { if (views < 2000) { var viewLikeRatio2k = Math.round( (views + views * ((3000-views)/2000)) / (likes) ); if (views < 255) { var viewLikeRatio = Math.round( viewLikeRatio2k / (views/255) ); } else { var viewLikeRatio = viewLikeRatio2k; } } else { var viewLikeRatio = Math.round( (views+7000) / 3 / (likes) ); } if ((viewLikeRatio < 1) || (viewLikeRatio > 255)) { return 0; } var powerMeter = ((255-viewLikeRatio)/2.55); var powerMeterLog = Math.round(Math.pow(powerMeter, 3)) / 10000; return powerMeterLog; } function makeBar(node, daysAgo, views, likes, dislikes) { var container = document.createElement('div'); container.classList.add('ratingsBarContainer'); container.setAttribute("style","position:absolute; bottom:0px; width:100%; height: 4px;"); var barMsg = "" var totalVotes = likes + dislikes; if (dislikes > 0) { var redBar = document.createElement('div'); redBar.classList.add('redBar'); redBar.setAttribute("style","position:absolute; bottom:0px; right:0px; width:100%; height:100%; background-color:#c00;"); container.appendChild(redBar); } if (((views > 300) && (views < 320) && (daysAgo <= 0.5)) || (totalVotes > views)) { if (likes > 0) { var pauseBar = document.createElement('div'); pauseBar.classList.add('pauseBar'); pauseBar.setAttribute("style","position:absolute; bottom:0px; background-color:#E9F126; border-top: 4px dotted #1FB15A; height:0px; width:"+ (100 * likes / totalVotes) +"%;"); container.appendChild(pauseBar); } barMsg = " View Count Incorrect"; } else { powerMeterLog = powerMeter(views, likes); if (likes > 0) { var middleBar = document.createElement('div'); middleBar.classList.add('middleBar'); middleBar.setAttribute("style","position:absolute; bottom:0px;"); if ((100 * likes / totalVotes) >= powerMeterLog) { middleBar.classList.add('green'); middleBar.style.height = "4px"; middleBar.style.width = (100 * likes / totalVotes)+"%"; middleBar.style.backgroundColor = "#00bb22"; } else { middleBar.classList.add('blue'); middleBar.style.width = powerMeterLog+"%"; middleBar.style.borderTop = "4px dotted rgb(185, 102, 165)"; middleBar.style.height = "0px"; middleBar.style.backgroundColor = "rgb(5, 5, 209)"; } container.appendChild(middleBar); } if (powerMeterLog > 0) { var blueBar = document.createElement('div'); blueBar.classList.add('blueBar'); blueBar.setAttribute("style","position:absolute; bottom:0px; background-color:rgb(53, 165, 201); border-top: 4px dotted rgb(0, 41, 255); height:0px; "); if ((100 * likes / totalVotes) > powerMeterLog) { blueBar.style.width = powerMeterLog+"%"; } else { blueBar.style.width = ((100 * likes / totalVotes))+"%"; } barMsg = " Power: "+ Math.round(powerMeterLog*100)/100 +"%"; container.appendChild(blueBar); } } node.appendChild(container); node.setAttribute("title","Likes: "+ likes +" Dislikes: "+ dislikes + barMsg); node.classList.add('processed'); } function getGdata(node,id) { GM_xmlhttpRequest({ method: 'GET', url: "http://gdata.youtube.com/feeds/api/videos/" + id + "?v=2&alt=json&fields=yt:rating,yt:statistics,published", onload: function(response) { if (response.status == 200) { var rsp = eval( '(' + response.responseText + ')' ); if (rsp && rsp.entry && rsp.entry.published && rsp.entry.yt$statistics && rsp.entry.yt$rating) { var daysAgo = (lastScanTime - new Date(rsp.entry.published.$t).getTime())/1000/60/60/24; var views = parseInt(rsp.entry.yt$statistics.viewCount); var likes = parseInt(rsp.entry.yt$rating.numLikes); var dislikes = parseInt(rsp.entry.yt$rating.numDislikes); makeBar(node, daysAgo, views, likes, dislikes); } else { node.classList.add('processed'); } } } }); } function scanVideos() { var videoList = document.querySelectorAll('a.ux-thumb-wrap[href^="/watch"] > span.video-thumb:not(.processed), a.related-video[href^="/watch"] > span:first-child:not(.processed), a.playlist-video[href^="/watch"] > span.yt-thumb-64:first-child:not(.processed)'); for ( var i = 0; i < videoList.length; i++ ) { var videoId = videoList[i].parentNode.getAttribute("href").replace(/.*[v|s]=([^&%]*).*/, "$1"); getGdata(videoList[i],videoId); } lastScanTime = new Date().getTime(); }; scanVideos(); document.onload = function() { scanVideos(); }; window.onscroll = function() { var timeNow = new Date().getTime(); var timeDiff = timeNow - lastScanTime if (timeDiff >= 1000) { scanVideos(); } };