-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathscript.user.js
More file actions
159 lines (127 loc) · 4.63 KB
/
script.user.js
File metadata and controls
159 lines (127 loc) · 4.63 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
// ==UserScript==
// @name IMDb - Show Digital Release Date
// @description Displays the digital release date for movies and TV shows on IMDb
// @version 1.1
// @namespace io.github.ni554n
// @match https://www.imdb.com/title/tt*
// @match https://m.imdb.com/title/tt*
// @run-at document-idle
// @grant GM_xmlhttpRequest
// @supportURL https://github.com/ni554n/userscripts/issues
// @license MIT
// @author Nissan Ahmed
// @homepageURL https://anissan.com
// @contributionURL https://paypal.me/ni554n
// ==/UserScript==
const imdbId = document.querySelector(
`meta[property="imdb:pageConst"]`,
)?.content;
if (!imdbId) throw new Error("Failed to find IMDb ID on the page");
requestTmdb(
`https://api.themoviedb.org/3/find/${imdbId}?external_source=imdb_id`,
(response) => {
if (response.response.tv_results.length !== 0) {
addReleaseDateInfo(response.response.tv_results[0].first_air_date);
return;
}
if (response.response.movie_results.length !== 0) {
const tmdbId = response.response.movie_results[0].id;
requestTmdb(
`https://api.themoviedb.org/3/movie/${tmdbId}/release_dates`,
(response) => {
let earliestReleaseDate;
// https://developer.themoviedb.org/reference/movie-release-dates
for (const obj of response.response.results) {
for (const date of obj.release_dates) {
if (date.type !== 4) continue;
const releaseDate = new Date(date.release_date);
if (earliestReleaseDate === undefined) {
earliestReleaseDate = releaseDate;
} else if (releaseDate < earliestReleaseDate) {
earliestReleaseDate = date.release_date;
}
}
}
addReleaseDateInfo(earliestReleaseDate);
},
);
}
},
);
/**
* @param { string | Date } releaseDate
*/
function addReleaseDateInfo(releaseDate) {
const subtitleElement = document.querySelector(
`h1[data-testid="hero__pageTitle"]`,
)?.parentElement?.lastElementChild;
const releasedNode = subtitleElement?.lastElementChild?.cloneNode();
if (!releasedNode) {
console.error(
"Failed to create a suitable element to add the release date in IMDb webpage.",
);
return;
}
if (releaseDate) {
releasedNode.textContent = `🖥️ ${getRelativeTime(releaseDate)}`;
releasedNode.title = `Digital release on ${releaseDate.toString()}`;
} else {
releasedNode.textContent = "🖥️ Unknown";
releasedNode.title = "Digital release date not found";
}
subtitleElement?.appendChild(releasedNode);
}
/**
* @param { string } url
* @param { (response: object) => void } onload
*/
function requestTmdb(url, onload) {
// Request the release date with TMDb ID
GM_xmlhttpRequest({
method: "GET",
url,
headers: {
accept: "application/json",
Authorization: `Bearer ${atob("ZXlKaGJHY2lPaUpJVXpJMU5pSjkuZXlKaGRXUWlPaUl4Wm1FelpETTFaR1l4TVdOak9HRmpNR1F6WVRsaU5qaGtZVGt4WTJZeVpTSXNJbk4xWWlJNklqWTBZbUU0TUdSa01URXpPRFpqTURCallXWTRNelE1TlNJc0luTmpiM0JsY3lJNld5SmhjR2xmY21WaFpDSmRMQ0oyWlhKemFXOXVJam94ZlEuUXNTVlFIWlhYSE1fYlpMZGs3dHo5ck5CSzFYVGpuc1FSS2dCLU92aUdrWQ==")}`,
},
responseType: "json",
onload,
onerror: console.error,
});
}
/**
* Formats date relatively such as "6 months ago", "1 week ago", "in 2 months".
*
* @param { string | Date } date
* @returns { string }
*/
function getRelativeTime(date) {
if (!(date instanceof Date)) date = new Date(date);
if (isNaN(date)) throw new Error(`Invalid date: ${date}`);
const timeFormatter = new Intl.RelativeTimeFormat();
const diffMilliSeconds = date - new Date();
const diffSeconds = Math.round(diffMilliSeconds / 1000);
if (Math.abs(diffSeconds) < 60) {
return timeFormatter.format(diffSeconds, "second");
}
const diffMinutes = Math.round(diffSeconds / 60);
if (Math.abs(diffMinutes) < 60) {
return timeFormatter.format(diffMinutes, "minute");
}
const diffHours = Math.round(diffMinutes / 60);
if (Math.abs(diffHours) < 24) {
return timeFormatter.format(diffHours, "hour");
}
const diffDays = Math.round(diffHours / 24);
const diffDaysAbs = Math.abs(diffDays);
if (diffDaysAbs < 7) {
return timeFormatter.format(diffDays, "day");
}
if (diffDaysAbs < 30) {
return timeFormatter.format(Math.round(diffDays / 7), "week");
}
if (diffDaysAbs < 365) {
return timeFormatter.format(Math.round(diffDays / 30), "month");
}
return timeFormatter.format(Math.round(diffDays / 365), "year");
}