This repository has been archived by the owner on Jan 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentscript.js
197 lines (171 loc) · 4.93 KB
/
contentscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
let allProviders, options;
/*
parse html
*/
//parse film title from letterboxd page
function getFilmTitle() {
return document.querySelector("#featured-film-header").firstElementChild.innerText;
}
//parse tMDB ID from letterboxd page
function getLBTmdbId() {
return parseInt(document.querySelector("body").dataset.tmdbId);
}
//parse Traile ID from letterboxd page
function getLBTrailerId() {
return document.querySelector(".watch-panel").children[1].firstElementChild.href.substr(30, 11);
}
/*
background & helper
*/
//set Listener for Chrome Storage API
function setStorageListener() {
chrome.storage.onChanged.addListener(function(changes, namespace) {
location.reload();
});
}
//check if a String is a locale
function isLocale(locale) {
return typeof locale === "string" && /[a-z][a-z]_[A-Z][A-Z]/.test(locale);
}
//get user location
function getLocale() {
if (isLocale(options.locale)) {
return options.locale;
} else if (isLocale(navigator.language.replace("-", "_"))) {
return navigator.language.replace("-", "_");
} else {
return "en_US" //default locale
}
}
//find the tMDB ID of a film from justwatch
function getJWTmdbId(scoring = []) {
return scoring.find(item => item.provider_type === "tmdb:id").value;
}
//find a film in a list of films from justwatch
function findFilm(films = []) {
return films.find(film => getJWTmdbId(film.scoring) === getLBTmdbId());
}
//get the Trailer ID from justwatch
function getJWTrailerId(clips = []) {
return clips.filter(clip => clip.type === "trailer")[0].external_id;
}
//returns only uniqie Providers: eg. Netflix is listed twice as HD and SD
function uniqueProviders(providers = []) {
return providers.filter((v, i, a) => a.map(v => v.provider_id).indexOf(v.provider_id) === i);
}
//turn provider id from justwatch to provider name
function IDtoProvider(id = 0) {
try {
return allProviders.find(provider => provider.id === id).clear_name;
} catch (err) {
console.log("Could not find Provider with ID " + id);
return "Unbekannt";
}
}
/*
justwatch
*/
function request(method, endpoint, data = null) {
return new Promise(function(resolve, reject) {
let xhr = new XMLHttpRequest();
xhr.open(method, 'https://cors-anywhere.herokuapp.com/apis.justwatch.com/content' + endpoint);
xhr.onload = function() {
if (this.status >= 200 && this.status < 300) {
resolve(JSON.parse(xhr.responseText));
} else {
reject({
status: this.status,
statusText: xhr.statusText
});
}
};
xhr.onerror = function() {
reject({
status: this.status,
statusText: xhr.statusText
});
};
xhr.send(JSON.stringify(data));
});
}
//get all Providers from justwatch
async function getProviders() {
return await request("GET", `/providers/locale/${getLocale()}`);
}
//search on justwatch for a film
async function searchTitle(title) {
try {
const res = await request("POST", '/titles/en_US/popular', {
query: title
});
return findFilm(res.items);
} catch {
console.log("Could not find film with title " + title);
return {};
}
}
//get film information from justwatch
async function getFilm(id = 0) {
try {
return await request("GET", `/titles/movie/${id}/locale/${getLocale()}`);
} catch {
console.log(id + " is not a valid ID");
return {};
}
}
//get film trailer from justwatch-api or parse it from letterboxd
function getTrailer({ clips }) {
if (clips != null) {
return options.trailerProvider + getJWTrailerId(clips);
}
try {
return options.trailerProvider + getLBTrailerId()
} catch (err) {
console.log("No trailer could be found");
return null;
}
}
//get providers that offer the film from justwatch
function getFilmProviders({ offers }) {
if (offers != null) {
return offers.filter(
provider => provider.monetization_type === "flatrate"
);
} else {
return [];
}
}
/*
create html
*/
//create a provider panel
function getProviderLabel(provider, url) {
return `<p>
<a href="${url}" class="label">
<span class="icon -play"></span>
<span class="name">${provider}</span>
</a>
</p>`
}
//display panel
function createStreamPanel(trailer, providers) {
return `<h3 class="title">Watch</h3>
<section>
${trailer != null ? getProviderLabel("Trailer", trailer) : ''}
${uniqueProviders(providers).map(p => getProviderLabel(IDtoProvider(p.provider_id), p.urls.standard_web)).join('')}
</section>`
}
/*
main
*/
async function main() {
setStorageListener();
options = await getOptions();
allProviders = await getProviders();
let filmOV = await searchTitle(getFilmTitle());
let film = await getFilm(filmOV.id);
let trailer = getTrailer(options.trailerOV ? filmOV : film);
let streamPanel = createStreamPanel(trailer, getFilmProviders(film));
document.querySelector(".watch-panel").innerHTML = streamPanel;
}
main();