// ==UserScript== // @name trakt.tv v2.0 - add IMDb & RottenTomatoes movie ratings (and sorting options for ratings) // @namespace https://greasyfork.org/en/users/167473-benedict-harris // @author jesuis-parapluie // @description Inserts movie ratings from IMDb, RottenTomatoes and Metacritic into trakt (with sorting options). // // @include /^https?://(.+\.)?trakt\.tv/?.*$/ // @exclude /^https?://(.+\.)?trakt\.tv/(shows|calendars)/?.*$/ // // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js // // @grant GM_xmlhttpRequest // // @version 2.0.5 // // @downloadURL none // ==/UserScript== /*==================NOTE - REPLACE the eight @ symbols is this code below 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 = $('', { 'class': 'ratings', 'html': '
 ' }), tomatoes = $('', { 'class': 'ratings', 'html': '
 ' }), dummy = $('', { 'class': 'ratings', 'style': 'opacity: 0.8; height: 18px;' }), url = $(this).attr('data-url'); if ($(this).attr('data-type') !== 'movie') { $(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('          loading'); $.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 = '#FF5555'; 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; } } if (res.tomatoRating === undefined || res.tomatoRating === "N/A") { res.tomatoRating = '-'; } if (res.tomatoRating < '60' ) { rtcolour = '#00CA5F'; } if (res.tomatoRating >= '100' ) { rtcolour = '#FF5555'; } if (res.Metascore === undefined || res.Metascore === "N/A") { res.Metascore = '-'; } if (res.imdbRating === undefined || res.imdbRating === "N/A") { $(imdb).find('span.value').html('     -'); } else { $(imdb).find('span.value').html('IMDb ' + res.imdbRating + ' (' + res.imdbVotes + ')'); } $(tomatoes).find('span.value').html('RT ' + res.tomatoRating + '    M ' + res.Metascore); }, onerror: function () { $(imdb).find('span.value').html('          failed'); } }); }).fail(function () { $(imdb).find('span.value').html('          failed'); }); } } }, 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() + ' '); } $("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($('
  • ', { html: "IMDb rating" })); sortMenu.append($('
  • ', { html: "TomatoMeter" })); sortMenu.append($('
  • ', { html: "Metascore" })); 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").not('.ratingsloaded').each(loadRatingsForItem); } }; $(window).ready(function () { $('head').append(''); $('head').append(''); init(); $(window).on('DOMNodeInserted', function (e) { if (e.target.tagName === 'BODY') { $(e.target).ready(init); } }); }); }(jQuery));