forked from soyguijarro/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Letterboxd_Extra_Profile_Stats.user.js
78 lines (70 loc) · 3.3 KB
/
Letterboxd_Extra_Profile_Stats.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
// ==UserScript==
// @name Letterboxd Extra Profile Stats
// @namespace https://github.com/worldwidewaves/letterboxd-scripts
// @description Adds average number of films watched per month and per week to profile pages
// @copyright 2014+, Ramón Guijarro (http://soyguijarro.com)
// @homepageURL https://github.com/worldwidewaves/letterboxd-scripts
// @supportURL https://github.com/worldwidewaves/letterboxd-scripts/issues
// @updateURL https://raw.githubusercontent.com/worldwidewaves/letterboxd-scripts/master/Letterboxd_Extra_Profile_Stats.user.js
// @icon https://raw.githubusercontent.com/worldwidewaves/letterboxd-scripts/master/img/letterboxd_icon.png
// @license GPLv3; http://www.gnu.org/licenses/gpl.html
// @version 1.7
// @include *://letterboxd.com/*/
// @exclude *://letterboxd.com/*/*/
// @exclude *://letterboxd.com/films/
// @exclude *://letterboxd.com/lists/
// @exclude *://letterboxd.com/people/
// @exclude *://letterboxd.com/search/
// @exclude *://letterboxd.com/settings/
// @exclude *://letterboxd.com/activity/
// @exclude *://letterboxd.com/invitations/
// @exclude *://letterboxd.com/about/
// @exclude *://letterboxd.com/pro/
// @exclude *://letterboxd.com/welcome/
// @exclude *://letterboxd.com/contact/
// @exclude *://letterboxd.com/201\d/
// @grant none
// ==/UserScript==
{
var headerElt = document.getElementById("profile-header"),
avatarElt = headerElt.getElementsByClassName("avatar")[0],
infoElt = headerElt.getElementsByClassName("profile-info")[0],
statsElt = headerElt.getElementsByClassName("profile-stats")[0],
statitsticsElt = headerElt.getElementsByClassName("profile-statistic")[1],
diaryUrl = statitsticsElt.getElementsByTagName("a")[0].href,
filmsPerYear = statitsticsElt.getElementsByClassName("value")[0].innerText,
filmsPerMonth,
filmsPerWeek,
avgElt,
avgInnerElt,
numElt,
textElt;
// Calculate averages
filmsPerMonth = (filmsPerYear / (new Date().getMonth() + 1));
filmsPerWeek = ((filmsPerMonth / 30) * 7);
// Insert calculated averages in page
[filmsPerWeek, filmsPerMonth].forEach(function (filmsAvg, index) {
avgElt = document.createElement("h4");
avgElt.className = "profile-statistic statistic";
avgInnerElt = document.createElement("a");
numElt = document.createElement("span");
numElt.className = "value";
textElt = document.createElement("span");
textElt.className = "definition";
// Round to one decimal place and remove trailing zero if present
filmsAvg = filmsAvg.toFixed(1).replace(/^(\d+)\.0$/, "$1");
// Fill element with data
avgInnerElt.href = diaryUrl;
numElt.textContent = filmsAvg;
textElt.textContent = (index === 0) ? "Per week" : "Per month";
// Build element structure
avgInnerElt.appendChild(numElt);
avgInnerElt.appendChild(textElt);
avgElt.appendChild(avgInnerElt);
// Insert element in page
statsElt.insertBefore(avgElt, statsElt.children[2]);
});
// Prevent overflow in layout
infoElt.style.width = "auto";
infoElt.style.maxWidth = headerElt.offsetWidth - avatarElt.offsetWidth - infoElt.offsetWidth + "px";
}