-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIMDB-average-season-score.user.js
More file actions
241 lines (193 loc) · 6.75 KB
/
Copy pathIMDB-average-season-score.user.js
File metadata and controls
241 lines (193 loc) · 6.75 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// ==UserScript==
// @name IMDB-average-season-score
// @description Shows the average score of a season from any series.
// @version 0.2
// @author Tim Hänlein
// @namespace https://github.com/THaenlein
// @downloadURL https://github.com/THaenlein/imdb-score-filmography/raw/master/IMDB-average-season-score.user.js
// @match *://www.imdb.com/title/*/episodes*
// @match *://www.imdb.com/*/title/*/episodes*
// @run-at document-end
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
/*
MIT License
Copyright (c) 2018 Tim Hänlein
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
function getCurrentTitleId() {
var match = window.location.pathname.match(/\/title\/(tt\d+)/);
return match ? match[1] : "";
}
function parseRatingValue(text) {
var valueMatch;
var value;
if (!text) {
return NaN;
}
valueMatch = text.match(/(\d+(?:[.,]\d+)?)\s*\/\s*10/);
if (!valueMatch) {
return NaN;
}
value = Number(valueMatch[1].replace(",", "."));
if (isNaN(value) || value <= 0 || value > 10) {
return NaN;
}
return value;
}
function extractEpisodeId(container, currentTitleId) {
var links = container.querySelectorAll('a[href*="/title/tt"]');
var i;
var idMatch;
var id;
for (i = 0; i < links.length; i++) {
idMatch = links[i].getAttribute("href").match(/\/title\/(tt\d+)/);
if (!idMatch) {
continue;
}
id = idMatch[1];
if (id !== currentTitleId) {
return id;
}
}
return "";
}
function getEpisodeRatings() {
var currentTitleId = getCurrentTitleId();
var cards = document.querySelectorAll("main article");
var seenEpisodeIds = {};
var ratings = [];
var i;
var episodeId;
var ratingElement;
var value;
var cardText;
for (i = 0; i < cards.length; i++) {
cardText = cards[i].textContent || "";
if (!/S\d+\.\s*E\d+/i.test(cardText)) {
continue;
}
episodeId = extractEpisodeId(cards[i], currentTitleId);
if (!episodeId || seenEpisodeIds[episodeId]) {
continue;
}
ratingElement = cards[i].querySelector(".ipc-rating-star--imdb");
value = parseRatingValue(ratingElement ? ratingElement.textContent : cardText);
if (!isNaN(value)) {
seenEpisodeIds[episodeId] = true;
ratings.push(value);
}
}
return ratings;
}
function getInsertTarget() {
var heading = document.querySelector('main h2, main h1');
if (heading && heading.parentElement) {
return heading.parentElement;
}
return document.querySelector("main");
}
function renderAverageScore(ratings) {
var existing = document.getElementById("averageRating");
var target = getInsertTarget();
var scoreElement;
var sum = 0;
var averageRating;
var i;
if (!target) {
return;
}
if (existing) {
existing.remove();
}
if (!ratings.length) {
return;
}
for (i = 0; i < ratings.length; i++) {
sum = sum + ratings[i];
}
averageRating = sum / ratings.length;
scoreElement = document.createElement("h3");
scoreElement.setAttribute("id", "averageRating");
scoreElement.style.margin = "8px 0 16px";
scoreElement.textContent = "Season average: " + averageRating.toFixed(1) + "\u2b50";
target.appendChild(scoreElement);
}
function calculateAverageScore() {
renderAverageScore(getEpisodeRatings());
}
function setupAverageScoreObserver() {
var mainObserver;
var rootObserver;
var observedMain = null;
var timeoutId;
var lastUrl = window.location.href;
var originalPushState;
var originalReplaceState;
function scheduleAverageScoreCalculation() {
clearTimeout(timeoutId);
timeoutId = setTimeout(calculateAverageScore, 200);
}
function attachMainObserverIfNeeded() {
var main = document.querySelector("main");
if (!main || main === observedMain) {
return;
}
if (mainObserver) {
mainObserver.disconnect();
}
observedMain = main;
mainObserver = new MutationObserver(scheduleAverageScoreCalculation);
mainObserver.observe(main, { childList: true, subtree: true });
scheduleAverageScoreCalculation();
}
function handleLocationChange() {
if (window.location.href === lastUrl) {
return;
}
lastUrl = window.location.href;
attachMainObserverIfNeeded();
scheduleAverageScoreCalculation();
}
attachMainObserverIfNeeded();
if (document.body) {
rootObserver = new MutationObserver(function () {
attachMainObserverIfNeeded();
handleLocationChange();
});
rootObserver.observe(document.body, { childList: true, subtree: true });
}
window.addEventListener("popstate", handleLocationChange);
window.addEventListener("hashchange", handleLocationChange);
if (!window.__imdbAverageSeasonScoreHistoryPatched) {
originalPushState = history.pushState;
originalReplaceState = history.replaceState;
history.pushState = function () {
originalPushState.apply(history, arguments);
window.dispatchEvent(new Event("imdbAverageSeasonScore:navigation"));
};
history.replaceState = function () {
originalReplaceState.apply(history, arguments);
window.dispatchEvent(new Event("imdbAverageSeasonScore:navigation"));
};
window.__imdbAverageSeasonScoreHistoryPatched = true;
}
window.addEventListener("imdbAverageSeasonScore:navigation", handleLocationChange);
}
setupAverageScoreObserver();