IMDb actor rating

IMDb actor rating.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name            IMDb actor rating
// @version         2
// @namespace	    imdb-actor-rating
// @include http://www.imdb.com/name/*
// @include https://www.imdb.com/name/*
// @include http://www.reddit.com/r/highdeas/comments/3cdfyv*
// @include https://www.reddit.com/r/highdeas/comments/3cdfyv*
// @grant		   GM_xmlhttpRequest
// @description IMDb actor rating.
// ==/UserScript==

if (document.location.pathname.indexOf("/name/") == 0) {
	new function() {
		var ratings = [];
		var rating_response_index = 0;
		
		function get_ratings(url) {
			GM_xmlhttpRequest({
				method: "GET",
				url: url,
				onload: function(response) {
					try {
						var rating = response.responseText.match(/<div class="titlePageSprite star-box-giga-star"> (\d(?:\.\d)) <\/div>/)[1];
					}
					catch (e) {}
					
					rating_response_index++;
					
					if (rating_response_index == films_starred_in.length)
						inject_ratings();
					else if (typeof rating != "undefined")
						ratings.push(parseFloat(rating));
				}
			})
		}
		
		function inject_ratings() {
			if (ratings.length == 0)
				return false;
			
			var sum = ratings.reduce(function(a, b) { return a + b });
			var avg = sum / ratings.length;
			var avg_float = Math.round(avg *10) /10;
			
			var div = document.createElement("div");
			div.className = "star-box giga-star";
			div.style.marginBottom = "0.7em";
			div.innerHTML = '\
				<div class="titlePageSprite star-box-giga-star"> '+ avg_float +' </div>\
				<div class="star-box-details" itemtype="http://schema.org/AggregateRating" itemscope="" itemprop="aggregateRating">\
					Ratings: <strong><span itemprop="ratingValue">'+ avg_float +'</span></strong><span class="mellow">/<span itemprop="bestRating">10</span></span><br>\
					Based on average Ratings of all works person acted in.<br>\
					<a href="javascript://" id="gm-share" data-gm-share-rating="'+ avg_float +'">Share this rating on the related Reddit post</a>\
				</div>\
				<div class="clear"></div>';
			
			var reference_node = document.querySelector('.infobar + hr');
			
			reference_node.parentNode.insertBefore(div, reference_node);
			reference_node.parentNode.removeChild(reference_node);
			
			div.addEventListener("click", function(event) {
				try { var name = "Rating of " + document.title.split(" - ")[0] + ": " + event.target.getAttribute("data-gm-share-rating") + " stars"; }
				catch(e) { var name = ""; }
				
				window.open("https://www.reddit.com/r/highdeas/comments/3cdfyv/imdb_tweak/", name)
			}, false)
		}
		
		var films_starred_in = document.querySelectorAll("#filmo-head-actor + div > div > b > a[href^='/title/tt'], #filmo-head-actress + div > div > b > a[href^='/title/tt']");
		for (var i = 0; i < films_starred_in.length; i++) {
			get_ratings(films_starred_in[i].href, films_starred_in.length)
		}
	}
}
else if (document.location.pathname.indexOf("/r/highdeas/comments/3cdfyv") == 0) {
	new function() {
		if (window.name.indexOf("Rating of") == 0) {
			var textarea = document.querySelector(".commentarea > form textarea");
			textarea.value = window.name +"\n"+ document.referrer.split("?")[0];
			window.name = "";
		}
	}
}