Last active
January 4, 2016 12:46
-
-
Save barvian/7d4899c67ce3f348e62e to your computer and use it in GitHub Desktop.
Add RottenTomatoes ratings to Letterboxd film pages.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Letterboxd RT | |
// @namespace http://barvian.me/ | |
// @version 1.0.0 | |
// @description Add RottenTomatoes ratings to Letterboxd film pages. | |
// @author Maxwell Barvian | |
// @match *://letterboxd.com/* | |
// ==/UserScript== | |
(function(window, document, undefined) { | |
var RT_API = "http://api.rottentomatoes.com/api/public/v1.0/", | |
API_KEY = "uk8evfz8t2x6q8rzmyfqxc6n", // please replace this with your own | |
stripIMDBID = /tt\d{7}/g; | |
var rtResponseHandler = function(response) { | |
if (response.readyState != 4) | |
return; | |
try { | |
var film = JSON.parse(response.responseText); | |
if (film.ratings.critics_rating || film.ratings.audience_rating) { | |
var rtNode = document.createElement("section"); | |
rtNode.className = "section friends-watched rotten-tomatoes"; | |
rtNode.innerHTML += '<h2 class="section-heading"><a href="' + film.links.alternate + '">Rotten Tomatoes</a></h2>'; | |
rtNode.innerHTML += '<a href="' + film.links.alternate + '" class="has-icon icon-16 icon-more all-link"><span class="icon"></span>More</a>'; | |
if (film.ratings.critics_rating) { | |
rtNode.innerHTML += '<p class="has-icon icon-16 icon-watched small-watched" style="display:block;"><span class="icon"></span> <a href="' + film.links.alternate + 'reviews/">' + film.ratings.critics_score + '%</a> ' + film.ratings.critics_rating + '</p>' | |
} | |
if (film.ratings.audience_rating) { | |
rtNode.innerHTML += '<p class="has-icon icon-16 icon-watched small-watched" style="display:block;"><span class="icon"></span> <a href="' + film.links.alternate + 'reviews/?type=user">' + film.ratings.audience_score + '%</a> ' + film.ratings.audience_rating + '</p>' | |
} | |
sidebar.appendChild(rtNode); | |
} | |
} catch (e) { | |
} | |
}; | |
var sidebar = document.querySelector(".film .sidebar"); | |
if (sidebar) { | |
// Retrieve IMDB ID of current film | |
var imdbLink = document.querySelector(".col-main .text-link a[href*='imdb.com']"); | |
if (imdbLink) { | |
var match = stripIMDBID.exec(imdbLink.getAttribute("href")); | |
if (match && match.length == 1) { | |
var imdbID = match[0].replace("tt", ""); | |
// Make RT API request using Alias API and IMDB ID | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: RT_API + "movie_alias.json?type=imdb&id=" + imdbID + "&apikey=" + API_KEY, | |
accept: 'text/json', | |
onreadystatechange: rtResponseHandler | |
}); | |
} | |
} | |
} | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment