This repository has been archived by the owner on Apr 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Letterboxd_Bio_Modifier.user.js
207 lines (178 loc) · 7.87 KB
/
Letterboxd_Bio_Modifier.user.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
198
199
200
201
202
203
204
205
206
207
// ==UserScript==
// @name Letterboxd Bio Modifier
// @namespace https://github.com/soyguijarro/userscripts
// @description Adds visual bio summary and Wikipedia link to actors and directors pages
// @copyright 2015, Ramón Guijarro (http://soyguijarro.com)
// @homepageURL https://github.com/soyguijarro/userscripts
// @supportURL https://github.com/soyguijarro/userscripts/issues
// @updateURL https://raw.githubusercontent.com/soyguijarro/userscripts/master/Letterboxd_Bio_Modifier.user.js
// @icon https://raw.githubusercontent.com/soyguijarro/userscripts/master/img/letterboxd_icon.png
// @license GPLv3; http://www.gnu.org/licenses/gpl.html
// @version 1.2
// @include *://letterboxd.com/director/*
// @include *://letterboxd.com/actor/*
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// ==/UserScript==
var sidebarElt = document.getElementsByClassName("sidebar")[0],
bioElt = sidebarElt.getElementsByClassName("tmdb-person-bio")[0],
tmdbId = bioElt.getAttribute("data-tmdb-id"),
tmdbBaseUrl = "http://themoviedb.org/person/",
tmdbUrl = tmdbBaseUrl + tmdbId;
function showBioSummary(res) {
var dom = new DOMParser().parseFromString(res.responseText, "text/html"),
tmdbBirthplaceElt = dom.getElementById("place_of_birth"),
tmdbBirthdayElt = dom.getElementById("birthday"),
tmdbDeathdayElt = dom.getElementById("deathday"),
creditsElt = dom.getElementById("leftCol").getElementsByTagName("p")[0],
creditsMatch = creditsElt.textContent.match(/Known Credits: (\d+)/),
gotRelevantData = isActualData(tmdbBirthplaceElt) ||
isActualData(tmdbBirthdayElt),
bioSummaryElt = document.createElement("section"),
bioSummaryElts,
bioInnerElt,
cssRules = "section.panel-text.bio-summary {\
border-bottom: 1px solid #456;\
}\
section.panel-text.bio-summary p {\
padding-left: 25px;\
display: block;\
}";
function getFormattedDate(date) {
var monthNum = date.getMonth(),
dayNum = date.getDate(),
yearNum = date.getFullYear(),
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return monthNames[monthNum] + " " + dayNum + ", " + yearNum;
}
function isActualData(elt) {
var data = elt.textContent,
exp = /^-$/;
return !(exp.test(data));
}
function showBirthplace() {
var birthplace = tmdbBirthplaceElt.textContent,
birthplaceElt = document.createElement("p"),
birthplaceIconElt = document.createElement("span");
// Fill element with data and apply styles
birthplaceElt.classList.add("icon-location");
birthplaceElt.textContent = birthplace.replace(/ - /g, ", ");
birthplaceIconElt.style.marginLeft = "2px";
// Insert element in section
birthplaceElt.appendChild(birthplaceIconElt);
bioSummaryElt.appendChild(birthplaceElt);
}
function showBornDeathDate() {
var birthday = new Date(tmdbBirthdayElt.firstElementChild.textContent),
dateElt = document.createElement("p"),
dateIconElt = document.createElement("span"),
msPerYear = 1000 * 60 * 60 * 24 * 365,
refDate,
date,
age;
// Fill element with data and apply styles
if (tmdbDeathdayElt) { // Person is dead
// Use death date as reference to calculate age
refDate = new Date(tmdbDeathdayElt.firstElementChild.textContent);
date = refDate; // Show death date
dateElt.classList.add("icon-hidden");
dateIconElt.style.marginLeft = "3px";
} else { // Person is alive
// Use today as reference to calculate age
refDate = new Date();
date = birthday; // Show birthday
dateElt.classList.add("icon-people");
}
age = Math.floor((refDate - birthday.getTime()) / msPerYear);
dateElt.textContent = getFormattedDate(date) +
" (age" + ((tmdbDeathdayElt) ? "d " : " ") + age + ")";
// Insert element in section
dateElt.appendChild(dateIconElt);
bioSummaryElt.appendChild(dateElt);
}
function showNumCredits() {
var numCredits = creditsMatch[1],
creditsElt = document.createElement("p"),
creditsIconElt = document.createElement("span");
// Fill element with data and apply styles
creditsElt.classList.add("icon-list-all");
creditsElt.textContent = numCredits + " known credits";
creditsIconElt.style.backgroundPosition = "-740px -110px";
// Insert element in section
creditsElt.appendChild(creditsIconElt);
bioSummaryElt.appendChild(creditsElt);
}
// Set up section to be inserted in page
bioSummaryElt.className = "section panel-text bio-summary";
// Fill section with available data
if (gotRelevantData) {
if (isActualData(tmdbBirthplaceElt)) {
showBirthplace();
}
if (isActualData(tmdbBirthdayElt)) {
showBornDeathDate();
}
if (creditsMatch) {
showNumCredits();
}
} else {
return; // Abort if no relevant data at all is available
}
// Apply common styles to section elements
bioSummaryElts = bioSummaryElt.children;
for (var i = 0; i < bioSummaryElts.length; i++) {
bioSummaryElts[i].classList.add("has-icon")
bioSummaryElts[i].classList.add("icon-16");
bioSummaryElts[i].firstElementChild.className = "icon";
}
// Insert section in page
bioInnerElt = bioElt.getElementsByClassName("panel-text condensed")[0] ||
bioElt.getElementsByClassName("panel-text")[0];
if (bioInnerElt) { // Already existing bio section
bioInnerElt.insertBefore(bioSummaryElt, bioInnerElt.firstElementChild.nextSibling);
} else { // No bio section, add missing header
var bioHeaderElt = document.createElement("h2");
bioHeaderElt.className = "section-heading";
bioHeaderElt.textContent = "Bio";
bioElt.appendChild(bioHeaderElt);
bioElt.appendChild(bioSummaryElt);
}
GM_addStyle(cssRules);
}
function showWikiLink() {
var linksElt = document.getElementsByClassName("bio-link")[0],
headerElt = document.getElementsByClassName("page-header")[0],
personNameElt = headerElt.querySelector("h1.inline-heading.prettify em"),
wikiLinkElt = document.createElement("li"),
wikiLinkInnerElt = document.createElement("a"),
personName = personNameElt.textContent,
wikiBaseUrl = "https://en.wikipedia.org/wiki/",
wikiUrl = wikiBaseUrl + personName,
linksInnerElt;
// Fill element with data and apply styles
wikiLinkInnerElt.className = "box-link";
wikiLinkInnerElt.href = wikiUrl;
wikiLinkInnerElt.textContent = "Search on Wikipedia";
wikiLinkElt.appendChild(wikiLinkInnerElt);
// Insert section in page
if (linksElt) { // Already existing link section
linksInnerElt = linksElt.getElementsByTagName("ul")[0];
linksInnerElt.insertBefore(wikiLinkElt, linksInnerElt.firstElementChild);
} else { // No link section, create first
linksElt = document.createElement("section");
linksInnerElt = document.createElement("ul");
linksElt.className = "section bio-link";
linksInnerElt.className = "box-link-list box-links";
linksInnerElt.appendChild(wikiLinkElt);
linksElt.appendChild(linksInnerElt);
sidebarElt.insertBefore(linksElt,
sidebarElt.getElementsByClassName("progresspanel")[0]);
}
}
GM_xmlhttpRequest({
method: "GET",
url: tmdbUrl,
onload: showBioSummary
});
showWikiLink();