// ==UserScript==
// @name trakt.tv with tmdb rating
// @author NKay08
// @description Inserts movie ratings from tmdb into trakt.
// @namespace https://greasyfork.org/de/users/155913-nkay08
// @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 0.0.1.20171023083943
// @downloadURL none
// ==/UserScript==
(function ($) {
'use strict';
/*jslint browser: true, regexp: true, newcap: true*/
/*global jQuery, GM_xmlhttpRequest */
$.noConflict();
var loadRatingsForItem = function () {
tmdb = $('
', {
'class': 'ratings',
'html': '
R.T. / TMDB
'
}),
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(tmdb);
if (url) {
}
},
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 === 'tmdb') {
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: "tmdb rating" }));
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));