// ==UserScript==
// @name Show Rottentomatoes meter - Trakt UI Addon
// @description This is an addon script for "Show Rottentomatoes meter" by cuzi, that displays the Rottentomatoes info cleanly on the trakt.tv infobar
// @namespace 1N07
// @author 1N07
// @license Unlicense
// @icon https://raw.githubusercontent.com/OneNot/Userscripts/main/Show%20Rottentomatoes%20meter%20-%20Trakt%20UI%20Addon/logo.png
// @version 48.1.4
// @match https://trakt.tv/movies/*
// @match https://trakt.tv/shows/*
// @require https://update.greasyfork.org/scripts/511024/1457631/Simple%20WaitForKeyElement.js
// @downloadURL https://update.greasyfork.icu/scripts/498861/Show%20Rottentomatoes%20meter%20-%20Trakt%20UI%20Addon.user.js
// @updateURL https://update.greasyfork.icu/scripts/498861/Show%20Rottentomatoes%20meter%20-%20Trakt%20UI%20Addon.meta.js
// ==/UserScript==
const HideRottenTomatoesMeterPanel = true;
function ApplyHideRottenTomatoesMeterPanelCSS() {
const style = document.createElement("style");
style.innerHTML = `
#mcdiv321rotten {
display: none;
}
`;
document.head.appendChild(style);
}
const RottentomatoesIcons = {
empty_tomato: ``,
empty_popcorn: ``,
red_popcorn: ``,
green_popcorn: ``,
rotten: ``,
fresh: ``,
certified_fresh: ``,
};
const MakeRottenTomatoesScoreElement = (data, placeholder = false) => {
const li = document.createElement("li");
li.className = `rtm-ui-${data.type} ${placeholder ? "srtm-uia-placeholder" : ""}`;
li.innerHTML = `
${data.icon}
${data.score}%
${data.type} score
`;
return li;
};
const SetRealData = (from) => {
const placeholders = document.getElementsByClassName("srtm-uia-placeholder");
while (placeholders.length > 0) placeholders[0].remove();
const link = from?.getElementsByTagName("a")?.[0]?.getAttribute("href");
const criticsTitle = from
?.querySelector("[title^='Critics']")
?.getAttribute("title");
const audienceTitle = from
?.querySelector("[title^='Audience']")
?.getAttribute("title");
const audienceIconString = audienceTitle?.split("% ")?.[1]?.split(/\s/)?.[0];
const criticsIconString = criticsTitle?.split("% ")?.[1]?.split(/\s/)?.[0];
return {
audience: {
type: "Audience",
score: audienceTitle?.split("Audience ")?.[1]?.split("%")?.[0] ?? "N/A",
icon: RottentomatoesIcons[
!audienceIconString || audienceIconString === "null"
? "empty_popcorn"
: audienceIconString
],
title:
!audienceTitle || audienceIconString === "null"
? "No audience score available"
: audienceTitle,
link: link ?? "#",
},
critics: {
type: "Critics",
score: criticsTitle?.split("Critics ")?.[1]?.split("%")?.[0] ?? "N/A",
icon: RottentomatoesIcons[
!criticsIconString || criticsIconString === "null"
? "empty_tomato"
: criticsIconString
],
title:
!criticsTitle || criticsIconString === "null"
? "No critics score available"
: criticsTitle,
link: link ?? "#",
},
};
};
const SetPlaceholderData = () => {
return {
audience: {
type: "Audience",
score: "...",
icon: RottentomatoesIcons.empty_popcorn,
title: "Loading...",
link: "#",
},
critics: {
type: "Critics",
score: "...",
icon: RottentomatoesIcons.empty_tomato,
title: "Loading...",
link: "#",
},
};
};
//TODO: timeouts for WaitForElements?
//TODO: try to root out false positives
//e.g.
//John Wick 5 -> finds John Wick
//Se7en (1995) -> finds Se7en days (2010) - because it's called just Seven in Rottentomatoes
//More results... is available in some cases
//Could load in all the results found and choose the first result with the matching year. That sounds like a fairly accurate solution.
(() => {
if (HideRottenTomatoesMeterPanel) {
ApplyHideRottenTomatoesMeterPanelCSS();
}
WaitForKeyElement(`
.shows.show #summary-ratings-wrapper .ratings,
.movies.show #summary-ratings-wrapper .ratings
`).then((insertLocation) => {
const placeHolderData = SetPlaceholderData();
insertLocation.appendChild(
MakeRottenTomatoesScoreElement(placeHolderData.critics, true),
);
insertLocation.appendChild(
MakeRottenTomatoesScoreElement(placeHolderData.audience, true),
);
WaitForKeyElement("#mcdiv321rotten > .firstResult").then((rottenEl) => {
const data = SetRealData(rottenEl);
insertLocation.appendChild(MakeRottenTomatoesScoreElement(data.critics));
insertLocation.appendChild(MakeRottenTomatoesScoreElement(data.audience));
});
});
})();