Last active
November 12, 2017 11:43
-
-
Save snipem/f6b063e404ec43d90e7f4175b6ad9ee9 to your computer and use it in GitHub Desktop.
Userscript Letterboxd.com JustWatch Integration
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.com to JustWatch | |
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js | |
// @namespace https://gist.githubusercontent.com/snipem/f6b063e404ec43d90e7f4175b6ad9ee9/raw/justwatchletterboxd.js | |
// @description Adds Just Watch links and offers to a letterboxd site. Helps on countries not supported by gowatchit. This script is heavily inspired from the work http://userscripts.org/users/luckyluciano did. | |
// @include *letterboxd.com/* | |
// @version 1.0 | |
// @grant GM_log | |
// @grant GM_xmlhttpRequest | |
// @connect justwatch.com | |
// ==/UserScript== | |
this.$ = this.jQuery = jQuery.noConflict(true); | |
var just_watch_base = "https://www.justwatch.com"; | |
function getLocale() { | |
return navigator.language.replace("-","_"); | |
} | |
function getTitle() { | |
return $(".headline-1").first().text(); | |
} | |
function getYear() { | |
// TODO Might be flaky | |
return $(".number").text(); | |
} | |
function capitalizeFirstLetter(string) { | |
return string.charAt(0).toUpperCase() + string.slice(1); | |
} | |
function addLinkTitle() { | |
var title = getTitle(); | |
var year = getYear(); | |
//TODO: This link will only work on the German version | |
var link = just_watch_base + "/de/Suche?q=" + title + " " + year; | |
var l = $("#userpanel ul"); | |
var parent = l[0]; | |
var url = "https://apis.justwatch.com/content/titles/"+getLocale()+"/popular?body=%7B%22content_types%22:%5B%22show%22,%22movie%22%5D,%22page%22:1,%22page_size%22:7,%22query%22:%22"+getTitle()+"%22%7D"; | |
var ret = GM_xmlhttpRequest({ | |
method: "GET", | |
headers: {"Accept": "application/json"}, | |
url: url, | |
onreadystatechange: function(res) { | |
GM_log("Request state changed to: " + res.readyState); | |
}, | |
onload: function(res) { | |
var myJSON = res.responseJSON; | |
movie = jQuery.parseJSON(res.response).items[0]; | |
image_url = just_watch_base + "/images"+movie.poster.replace("{profile}","s166"); | |
var listItem = document.createElement('li'); | |
listItem.setAttribute('id','listitem'); | |
var div = document.createElement("div"); | |
div.innerHTML=movie.title + " (" + movie.original_release_year + ")"; | |
var img = document.createElement("img"); | |
img.setAttribute('src', image_url); | |
img.setAttribute('style', "margin-left: auto; margin-right: auto"); | |
var a = document.createElement("a"); | |
a.setAttribute('href', just_watch_base + movie.full_path); | |
a.setAttribute('title', movie.short_description); | |
a.appendChild(img); | |
listItem.appendChild(div); | |
listItem.appendChild(a); | |
parent.appendChild(listItem); | |
movie.offers.forEach(function(offer) { | |
console.log(offer); | |
// Ignore cinema offers which are lacking standard web urls | |
try { | |
link = offer.urls.standard_web; | |
store_name = new URL(link).hostname.split('.').reverse()[1]; | |
add_item(offer.retail_price, link, capitalizeFirstLetter(store_name), offer.provider_id, offer.presentation_type.toUpperCase()); | |
} | |
catch (e) | |
{ | |
GM_log("Not a valid offer " + offer); | |
} | |
}); | |
} | |
}); | |
var listItem = document.createElement('li'); | |
listItem.setAttribute('id','listitem'); | |
var a = document.createElement("a"); | |
a.innerHTML="Just Watch Search"; | |
a.setAttribute('href', link); | |
a.setAttribute('target', "_blank"); | |
listItem.appendChild(a); | |
parent.appendChild(listItem); | |
$(l[0]).listview("refresh"); | |
} | |
function add_item(price, link, store_name, provider, quality="") | |
{ | |
var l = $("#userpanel ul"); | |
var parent = l[0]; | |
var listItem = document.createElement('li'); | |
listItem.setAttribute('id','listitem'); | |
var a = document.createElement("a"); | |
if (price === undefined) | |
price = "🍿"; | |
a.setAttribute('href', link); | |
a.setAttribute('target', "_blank"); | |
provider_list_item = $('#jw_provider_' + provider); | |
// If there is already a existing entry for the provider | |
if (provider_list_item.length > 0) | |
{ | |
a.innerHTML= "</br>" + price + " " + quality; | |
provider_list_item[0].appendChild(a); | |
} | |
else | |
{ | |
listItem.setAttribute('id', "jw_provider_"+provider); | |
a.innerHTML=store_name + "</br>" + price + " " + quality; | |
listItem.appendChild(a); | |
parent.appendChild(listItem); | |
} | |
} | |
function getImdb(href) { | |
var from = href.indexOf("imdb.com/title/tt") + 17; | |
if(from < 17) | |
return null; | |
var to = href.indexOf("/", from); | |
if(to < 0) | |
to = href.length; | |
return href.substring(from, to); | |
} | |
String.prototype.contains = function(it) { | |
return this.indexOf(it) != -1; | |
}; | |
(function () { | |
var href = window.location.href; | |
if(href.contains("letterboxd.com/film/")) | |
addLinkTitle(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment