// ==UserScript==
// @name trakt.tv ++ IMDb, MetaCritic & RottenTomatoes movie ratings + links
// @namespace https://greasyfork.org/en/users/167473-benedict-harris
// @author jesuis-parapluie (plus modifications by Benedict)
// @description Inserts movie ratings from IMDb, RottenTomatoes and Metacritic into trakt (with sorting options).
//
// @include /^https?://(.+\.)?trakt\.tv/?.*$/
// @exclude /^https?://(.+\.)?trakt\.tv/(calendars)/?.*$/
//
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
//
// @grant GM_xmlhttpRequest
//
// @version 2.0.8
//
// ==/UserScript==
/*==================NOTE - REPLACE the eight @ symbols is this code (line 53) with your free API from omdbapi.com ====================*/
(function ($) {
'use strict';
/*jslint browser: true, regexp: true, newcap: true*/
/*global jQuery, GM_xmlhttpRequest */
$.noConflict();
var loadRatingsForItem = function () {
var imdb = $('<span>', {
'class': 'ratings',
'html': '<div style="float: left; width: 0px;"></div><span class="value"> </span>'
}),
tomatoes = $('<span>', {
'class': 'ratings',
'html': '<div style="float: left; width: 0px;"></div><span class="value"> </span>'
}),
dummy = $('<span>', {
'class': 'ratings',
'style': 'opacity: 0.8; height: 18px;'
}),
url = $(this).attr('data-url');
if ($(this).attr('data-type') !== 'movie' && ($(this).attr('data-type') !== 'show')) {
$(this).find('.quick-icons').after(dummy).after(dummy.clone());
} else {
$(this).find('.quick-icons').after(tomatoes).after(imdb);
if (url) {
$(imdb).find('span.value').html('<span style="color: #999!important; font-weight: normal; font-size: 11px;"> loading<span>');
$.get(url, function (data) {
var imdb_id = $(data).find('.external a:contains("IMDB")').attr('href').split('/').pop();
GM_xmlhttpRequest({
method: "GET",
url: "http://www.omdbapi.com/?plot=short&tomatoes=true&r=json&apikey=@@@@@@@@&i=" + imdb_id,
onload: function (json) {
var rtcolour = '#00CA5F';
var rtimglink = 'https://staticv2-4.rottentomatoes.com/static/images/icons/splat-16.png';
var imdbimglink = 'https://ia.media-imdb.com/images/M/MV5BMTk3ODA4Mjc0NF5BMl5BcG5nXkFtZTgwNDc1MzQ2OTE@._V1_.png';
var metacimglink = 'https://upload.wikimedia.org/wikipedia/commons/2/20/Metacritic.svg';
var rtimglong = '<img src="https://staticv2-4.rottentomatoes.com/static/images/icons/splat-16.png" alt="rt" height="16" width="16">';
var r, res = $.parseJSON(json.responseText);
for (r in res.Ratings) {
if (res.Ratings.hasOwnProperty(r) && res.Ratings[r].Source === "Rotten Tomatoes") {
res.tomatoRating = res.Ratings[r].Value;
}
}
console.log('++');
console.log(res.tomatoURL);
console.dir(res);
if (res.tomatoRating === undefined || res.tomatoRating === "N/A") {
res.tomatoRating = '-';
console.log('NULLLLLLLLLLLLLLLLLLLLLLLLLLLLLL');
rtimglink = '';
rtimglong = '';
}
if (res.tomatoRating >= '60' ) {
rtcolour = '#FF5555';
rtimglink = 'https://staticv2-4.rottentomatoes.com/static/images/icons/fresh-16.png';
rtimglong = '<img src="https://staticv2-4.rottentomatoes.com/static/images/icons/fresh-16.png" alt="rt" height="16" width="16">';
}
if (res.tomatoRating == '100%' ) {
rtcolour = '#FF5555';
rtimglink = 'https://staticv2-4.rottentomatoes.com/static/images/icons/fresh-16.png';
rtimglong = '<img src="https://staticv2-4.rottentomatoes.com/static/images/icons/fresh-16.png" alt="rt" height="16" width="16">';
/*console.log('============================' + res.tomatoRating);*/
}
if (res.Metascore === undefined || res.Metascore === "N/A") {
res.Metascore = '-';
}
if (res.imdbRating === undefined || res.imdbRating === "N/A") {
$(imdb).find('span.value').html(' -');
} else {
/*console.log('============================' + res.tomatoRating);*/
$(imdb).find('span.value').html('<a href="http://www.imdb.com/title/' + res.imdbID + '"><font color="#555"><font color="#F6F68B">' + res.imdbRating + '<span style="font-size: 11px!important; font-style: normal; color: #999;"> </span></a>');
}
$(tomatoes).find('span.value').html('<font color="#555"> <span class="uuu"><font color="orange">' + res.Metascore + '</span><span class="sunsh" style="float: right; padding-right: 10px;"><a href="' + res.tomatoURL + '">' + rtimglong + '<font color="' + rtcolour + '">' + res.tomatoRating + '</a></span>');
},
onerror: function () {
$(imdb).find('span.value').html('<span style="color: #c11!important; font-weight: normal; font-size: 12px;"> failed<span>');
}
});
}).fail(function () {
$(imdb).find('span.value').html('<span style="color: #c11!important; font-weight: normal; font-size: 12px;"> failed<span>');
});
}
}
},
parseRating = function (item, type) {
var r;
if (type === 'originalOrder') {
return $(item).attr('startOrder');
}
if (type === 'trakt') {
r = $(item).find("div.percentage").text().slice(0, -1);
if (r !== null && r >= 0 && r <= 100) {
return r;
}
}
if (type === 'imdb') {
r = $(item).find("span.ratings").text().match(/IMDb\W+(\d(\.\d)?).*/i);
}
if (type === 'tomatometer') {
r = $(item).find("span.ratings").text().match(/R\.T\.[^\d]+(\d*)%?.*/i);
}
if (type === 'metascore') {
r = $(item).find("span.ratings").text().match(/R\.T\. \/[^\/]+\/\W*(\d+).*/i);
}
if (r !== null && r.length > 1 && r[1] !== '-') {
return r[1];
}
return -1;
},
sortByRating = function (e) {
var items, order,
dict = {},
parent = $("div.grid-item").parent(),
how = function (a, b) { return a - b; };
$(".no-top").hide();
if ($('#sortable-name').size() > 0) {
$('#sortable-name').text($(e.target).text()).attr('data-sort-by', $(e.target).attr('data-sort-by'));
} else {
$('.trakt-icon-swap-vertical').next().find('.btn-default').html($(e.target).text() + ' <span class="caret"></span>');
}
$("div.grid-item").each(function () {
var rating = parseRating(this, $(e.target).attr('data-sort-by'));
if (dict[rating] === undefined) {
dict[rating] = [$(this)];
} else {
dict[rating].push($(this));
}
});
if ($('#sort-direction').hasClass('desc')) {
how = function (b, a) { return a - b; };
}
order = Object.keys(dict).sort(how);
while (order.length > 0) {
items = dict[order.pop()];
while (items.length > 0) {
parent.append(items.shift());
}
}
},
setPositioning = function () {
setTimeout(function () {
if ($('.trakt-icon-swap-vertical').next().find('a.rating:contains("' + $('.trakt-icon-swap-vertical').next().find('.btn-default').text().trim() + '")').size() > 0) {
$("div.grid-item").each(function () { $(this).attr('style', '{position:relative;top:0px;left:0px;}'); });
} else if ($('.grid-item').first().attr('style') !== undefined) {
$("div.grid-item").each(function () { $(this).css('position', 'absolute'); });
}
}, 500);
},
init = function () {
$("div[id*=\"huckster-desktop\"]").html("");
if (/^\/users\/.+\/(collection|ratings|lists\/|watchlist)/.test(window.location.pathname) && $('.trakt-icon-swap-vertical').next().find('ul a.rating').size() === 0) {
var sortMenu = $('.trakt-icon-swap-vertical').next().find('ul');
sortMenu.append($('<li>', { html: "<a class='rating' data-sort-by='imdb' data-sort-how='desc'>IMDb rating</a>" }));
sortMenu.append($('<li>', { html: "<a class='rating' data-sort-by='tomatometer' data-sort-how='desc'>TomatoMeter</a>" }));
sortMenu.append($('<li>', { html: "<a class='rating' data-sort-by='metascore' data-sort-how='desc'>Metascore</a>" }));
sortMenu.find('a.rating').click(sortByRating);
sortMenu.find('a').click(setPositioning);
$(window).on('resize', setPositioning);
$('#sort-direction').click(function () {
$('.trakt-icon-swap-vertical').next().find('a.rating:contains("' + $('.trakt-icon-swap-vertical').next().find('.btn-default').text().trim() + '")').click();
});
} else {
$(window).off('resize', setPositioning);
}
if (($("div.grid-item[data-type='movie']").size() > 0||$("div.grid-item[data-type='show']").size() > 0)) {
$("div.grid-item").not('.ratingsloaded').each(loadRatingsForItem);
}
};
$(window).ready(function () {
$('head').append('<style>span.ratings, span.ratings a { padding-top:4px!important; padding-bottom:0px!important; padding-left: 0px!important; margin-top: 0px!important; margin-left:1px!important; margin-right:0px; background-color: transparent; color: white; text-align: left!important; };</style>');
$('head').append('<style>span.value, span.value>a { padding-left: 3px!important; font-weight: bolder!important; font-size: 18px; };</style><style>.quick-icons { border-bottom:none!important; };</style>');
$('head').append('<style>div.posters, div.poster, div.quick-icons { background-color: #1d1d1d; border: none !important; };</style>');
$('head').append('<style>span.sunsh img {vertical-align: text-top; margin-right: 4px; margin-top: 1px; };</style>');
$('head').append('<style>div.grid-item.col-xxlg-1.col-xlg-2.col-lg-2.col-md-3.col-sm-3.col-xs-6 span.value {font-size: 15px !important; margin-right: 0px!important;};</style>');
$('head').append('<style>div.grid-item.col-xxlg-1.col-xlg-2.col-lg-2.col-md-3.col-sm-3.col-xs-6 span.value>a {font-size: 14px !important; font-weight: 700 !important; margin-right: 0px!important; };</style>');
init();
$(window).on('DOMNodeInserted', function (e) {
if (e.target.tagName === 'BODY') {
$(e.target).ready(init);
}
});
});
}(jQuery));