YTRating

Shows the rating of videos in the related videos

Ekde 2015/02/16. Vidu La ĝisdata versio.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name          YTRating
// @namespace     absolut-fair.com
// @description   Shows the rating of videos in the related videos
// @include       http://*youtube.com*
// @include       https://*youtube.com*
// @require       http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
// @version       1.0.10
// @grant         unsafeWindow
// @grant         GM_getValue
// @grant         GM_setValue
// @grant         GM_xmlhttpRequest
// @grant         GM_info
// @grant         GM_setClipboard
// @grant         GM_addStyle
// @grant         GM_openInTab
// ==/UserScript==

$(document).ready(function () {
	window.setInterval(function() { loadrating(); },1000);
});

function loadrating(div,link)
{
    $("img").each(function() {
        if( $(this).attr("ytrating-preprocessed") ) return;
        
    	var myhref = $(this).attr("src");
        if( myhref.indexOf("ytimg.com") != -1)
        {
            if( !$(this).attr("alt")!="Thumbnail" && !$(this).attr("data-thumb") && !$(this).attr("aria-hidden")!="true" ) $(this).attr("ytrating-queue",1);
        }
    	$(this).attr("ytrating-preprocessed", 1);
    });
	$('img[alt="Thumbnail"], img[data-thumb], img[aria-hidden="true"], img[ytrating-queue]').closest('a').each(function (i) {
        //console.log("ytrating "+i);
		var that=this;
		var that_parent = $(that).closest("div,li");
		var inda=i;
		var hrea = $(this).attr('href');
        if( typeof hrea == "undefined" || hrea == null || hrea == "" ) return;
		
		if(hrea.indexOf('?v=')==-1) 
		{
			if($(this).closest('div[data-context-item-id]').length)
			{
				hrea = "/watch?v="+($(this).closest('div[data-context-item-id]').attr("data-context-item-id"));
			}
			else return true;
		}
		hrea = hrea.replace("&","&");
		hrea=hrea+"&";
		var vidid = hrea.between('?v=','&');
        
        if( $(this).attr('done') || $('.video_time',that_parent).attr('done') ) 
        {
            return true;
        }
		else 
        {
            $(this).attr('done','1');
            $('.video_time',that_parent).attr('done','1');
        }
		
		//alert(vidid);
	
		GM_xmlhttpRequest({
			method: "GET",
			url: "http://gdata.youtube.com/feeds/api/videos/"+vidid+"?v=2",
			headers: {
				"Accept": "text/xml",
				"GData-Version": "2",
				"X-GData-Key": "key=AI39si59MgtnnLtTwZTnvZ--XNvnXWlYpVcDfYO8AJK-CJSmS3pTytqktlxTp3YVriK0IYGcx1z2BK1_ud1DEduKBAI7T3JdpQ"
			  },
			onload: function(resp) {
				var xmldat = new DOMParser().parseFromString(resp.responseText, "text/xml");
				var conti=resp.responseText;
				var rating = xmldat.getElementsByTagNameNS("http://gdata.youtube.com/schemas/2007","rating");
				if(conti.indexOf("yt:rating")!=-1)
				{
					var likes = rating[0].getAttribute("numLikes");
					var dislikes = rating[0].getAttribute("numDislikes");
				}
				else 
				{
					var likes="1";
					var dislikes="0";
				}
				//console.log("Ergebnis:"+likes+"/"+dislikes);
				likes=likes.replace('.', '');
				dislikes=dislikes.replace('.', '');
				if(likes.length>0 && conti.indexOf("action='rate' permission='denied'")==-1)
				{
					var resul = likes+"/"+dislikes;
					likes=parseInt(likes);
					dislikes=parseInt(dislikes);
					
					var dr = (likes/(likes+dislikes))*100;
					dr=Math.round(dr);

					if((likes+dislikes)>80)
					{
						if(dr > 70) appendstr="<label style='color:#82FA58;font-size:18px;'> "+dr+"%</label>";
						else if(dr > 40) appendstr="<label style='color:#C9C618;font-size:18px;'> "+dr+"%</label>";
						else appendstr="<label style='color:red;font-size:18px;'> "+dr+"%</label>";
					}
					else 
					{
						if((likes+dislikes)==0)	appendstr="<label style='color:#848484; font-size:18px;'> NO</label>";
						else appendstr="<label style='color:#848484; font-size:18px;'> "+dr+"%</label>";
					}
				}
				else 
				{
					appendstr="<label style='color:red;font-size:18px;'> OFF</label>";
				}
                $('.video-time', that_parent).append(appendstr);
			}
		});
		//return false;
	});
}


String.prototype.between = function(prefix, suffix) {
s = this;
var i = s.indexOf(prefix);
if (i >= 0) {
s = s.substring(i + prefix.length);
}
else {
return '';
}
if (suffix) {
i = s.indexOf(suffix);
if (i >= 0) {
s = s.substring(0, i);
}
else {
return '';
}
}
return s;
}