Netflix Customization: Adding Rotten Tomatoes, Pirate Bay and Apple Trailers Links

By BCBomb47 Last update Jun 26, 2009 — Installed 602 times. Daily Installs: 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 2, 1, 1, 0, 0, 4, 0, 1, 1, 0, 1, 0, 2, 1, 1, 0, 1, 1, 0, 2, 1, 4

There are 1 previous version of this script.

// ==UserScript==
// @name          Netflix Customization: Adding Rotten Tomatoes, Pirate Bay and Apple Trailers Links
// @description	  Adds custom features to Netflix
// @include       http://www.netflix.com/Movie/*
// @include       http://www.netflix.com/WiMovie/*
// ==/UserScript==

var ratingsdiv = document.getElementById('avgratings');

if (ratingsdiv != null) {
  var htmlcode = ratingsdiv.innerHTML;
  var rottenTomatoesName = document.title.substring(9,document.title.length).toLowerCase().replace(/ /g,'_').replace(/:/g,'').replace(/'/g,'').replace(/-/g,'');
  var prefix = rottenTomatoesName.substr(0,4);
  if ('the_' == prefix) {
    rottenTomatoesName = rottenTomatoesName.substr(4, rottenTomatoesName.length-4);
  }

  htmlcode = htmlcode + "<br><a href=\"http://www.rottentomatoes.com/m/"+rottenTomatoesName+"/\" target=\"_blank\"><img alt=\"RotTom\" style=\"height: 16px;\" src=\"http://images.rottentomatoes.com/images/tomatoes/fresh.gif\"></a> |";
  ratingsdiv.innerHTML = htmlcode;

htmlcode = htmlcode + "<a href=\"http://www.thepiratebay.com/search/"+rottenTomatoesName+"/0/7/200"+"/\" target=\"_blank\"><img alt=\"PirateBay\" style=\"height: 16px;\" src=\"http://thepiratebay.com/favicon.ico\"></a> |";
  ratingsdiv.innerHTML = htmlcode;

var appleName = document.title.substring(9,document.title.length).toLowerCase().replace(/:/g,'');
  var prefix = appleName.substr(0,4);
  if ('the_' == prefix) {
    appleName = appleName.substr(4, rottenTomatoesName.length-4);
  }

htmlcode = htmlcode + "<a href=\"http://www.apple.com/search/downloads/?q="+appleName+"\" target=\"_blank\"><img alt=\"Apple\" style=\"height: 16px;\" src=\"http://www.firewise.org/fw_youcanuse/images/apple_icon.gif\"></a><br>";
  ratingsdiv.innerHTML = htmlcode;

}

addRottenText();
getRottenRating(rottenTomatoesName);

function addRottenText() {
	var findPattern = "//div[@id='avgratings']";
	var results = document.evaluate( findPattern, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
	var link = results.snapshotItem(0);
	var addedDivRotten = document.createElement('div');
	var addedDivRotten_text;
	addedDivRotten.innerHTML = '\nchecking';
	addedDivRotten.setAttribute('id','greaseTextRotten');
	addedDivRotten.setAttribute('style','color:#627d11;text-align:center;font-size:24px;font-weight:bold');
	link.parentNode.insertBefore(addedDivRotten, link.nextSibling);
}

function getRottenRating(rottenTomatoesName) {
	var rottenTomatoesURL = "http://www.rottentomatoes.com/m/" + rottenTomatoesName;
	GM_xmlhttpRequest({
			method: 'GET',
			url: rottenTomatoesURL,
			onload: function(responseDetails) {
				var search_string_start = "tomatometer_score";
				var match = responseDetails.responseText.search(search_string_start);
				var rotten_rating = responseDetails.responseText.substring(match + 55,match + 60);
				var number_rotten_rating = parseInt(rotten_rating);
				if (rotten_rating == "N/A")
				{
					rotten_rating = "n/a";
				} else
				{
					rotten_rating = number_rotten_rating + "%";
				}
				if (match != -1) {
					// found a rotten_rating
					if ( number_rotten_rating == -1)
					{
						addedDivRotten.innerHTML = '\nNot enough reviews for a rating\n';
						addedDivRotten.style.color='#627d11';
					}
					else { // best default case
						var rotten_rating_image_url, rotten_rating_text;
						var addedDivRotten = document.getElementById('greaseTextRotten');
						if (number_rotten_rating >= 60) { // it's fresh
							rotten_rating_image_url = 'http://images.rottentomatoes.com/images/tomatoes/fresh.gif';
							rotten_rating_text = "Fresh";
						}
						else { // it's rotten
							rotten_rating_image_url = 'http://images.rottentomatoes.com/images/tomatoes/rotten.gif';
							rotten_rating_text = "Rotten";
						}
						addedDivRotten.innerHTML = '\n' + rotten_rating + ' \n<IMG SRC="' + rotten_rating_image_url + '" ALT="' + rotten_rating_text + '" title="' + rotten_rating_text + '" ALIGN="absmiddle">\n';
						
					}
				} else {
					// did not find rotten_rating
					const $xpath = '//div[@id="tn15title"]/h1/text()';
					var $nodes = document.evaluate($xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
					var movieName = escape($nodes.singleNodeValue.data.replace(/\s+$/, ''));
					googleRottenTomatoesFallbackURL = "http://www.google.com/search?q=" + "intitle%3A%22" + movieName + "%22+" + "site%3Arottentomatoes.com";
					addedDivRotten.innerHTML = '\nUnable to find\n';
					addedDivRotten.style.color='red';
				}
			}
	});
} // end function getRottenRating