-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPTP-Add-Time-Column-and-Highlight-Recent-Torrents-anut.js
392 lines (350 loc) · 16.2 KB
/
PTP-Add-Time-Column-and-Highlight-Recent-Torrents-anut.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// ==UserScript==
// @name PTP - Add Time Column and Highlight Recent Torrents
// @namespace PTP-Add-Time-Column-and-Highlight-Recent-Torrents
// @version 0.5.6
// @description Add a Time column to the Torrent Group Page, Collage Page,
// Artist Page, and Bookmark Page.
// Highlight recent and latest torrent within a group.
// @author mcnellis (additions by Audionut)
// @match https://passthepopcorn.me/torrents.php*
// @match https://passthepopcorn.me/collages.php?*
// @match https://passthepopcorn.me/artist.php?*
// @match https://passthepopcorn.me/bookmarks.php*
// @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.js
// @downloadURL https://github.com/Audionut/add-trackers/raw/main/PTP-Add-Time-Column-and-Highlight-Recent-Torrents-anut.js
// @updateURL https://github.com/Audionut/add-trackers/raw/main/PTP-Add-Time-Column-and-Highlight-Recent-Torrents-anut.js
// ==/UserScript==
/* globals $, moment, coverViewJsonData, ungroupedCoverViewJsonData */
(function() {
'use strict';
const TIME_FORMAT = 'rounded-relative'; // 'relative', 'rounded-relative', or any format value supported by moment.js
const HIGHLIGHT_LATEST_TEXT_COLOR = 'black';
const HIGHLIGHT_LATEST_BACKGROUND_COLOR = 'silver';
const HIGHLIGHT_LATEST_FONT_WEIGHT = 'bold';
const HIGHLIGHT_RECENT_TEXT_COLOR = 'black';
const HIGHLIGHT_RECENT_BACKGROUND_COLOR = 'gainsboro';
const HIGHLIGHT_RECENT_FONT_WEIGHT = 'bold';
const RECENT_DAYS_IN_MILLIS = 7 * 24 * 60 * 60 * 1000;
function main() {
if (isIgnoredTorrentsPage()) {
return;
} else if (isTorrentsGroupPage()) {
torrentsGroupPage();
} else if (isTorrentsPage()) {
torrentsPage();
} else if (isCollageSubscriptionsPage()) {
collageSubscriptionsPage();
} else if (isCollagesPage() || isArtistPage() || isBookmarksPage()) {
artistAndBookmarksAndCollagesPage();
}
}
function torrentsGroupPage() {
if (!$('.torrent_table thead tr th:contains("Time")').length) {
$('.torrent_table thead tr th:nth(0)').after($('<th>Time</th>'));
}
$('.group_torrent td.basic-movie-list__torrent-edition').each((i, edition) => {
$(edition).attr('colspan', parseInt($(edition).attr('colspan')) + 1);
});
$('.torrent_info_row td').each((i, info) => {
$(info).attr('colspan', parseInt($(info).attr('colspan')) + 1);
});
handleExistingTorrents();
handleNewTorrents();
}
function handleExistingTorrents() {
let times = [];
$('.group_torrent_header').each(function(i, element) {
const torrentRow = $(element);
if (!torrentRow.find('td.time-cell').length) {
let time = torrentRow.next().find('span.time').first();
if (time.length) {
const timeTitle = time.attr('title');
const parsedDate = moment.utc(timeTitle, "MMM DD YYYY, HH:mm");
if (parsedDate.isValid()) {
const isoString = parsedDate.toISOString();
const formattedTime = formatTime(parsedDate);
times.push(timeTitle);
const clonedTime = time.clone().html(formattedTime).addClass('nobr');
const timeCell = $('<td class="time-cell"></td>').append(clonedTime);
torrentRow.find('td:nth(0)').after(timeCell);
} else {
console.error('Invalid date:', timeTitle);
const timeCell = $('<td class="time-cell"></td>').append($('<span>').addClass('nobr').text('Invalid date'));
torrentRow.find('td:nth(0)').after(timeCell);
}
}
}
});
highlightTimes(times);
}
function handleNewTorrents() {
let times = [];
$('.group_torrent_header').each(function(i, element) {
const torrentRow = $(element);
if (!torrentRow.find('td.time-cell').length) {
let time = torrentRow.find('span.release.time').first();
if (time.length) {
const unixTimestamp = parseInt(time.attr('title'));
if (!isNaN(unixTimestamp)) {
const formattedTime = formatTime(moment.unix(unixTimestamp));
time.html(formattedTime);
times.push(time.attr('title'));
$(time).addClass('nobr');
const timeCell = $('<td class="time-cell"></td>').append(time);
torrentRow.find('td:nth(0)').after(timeCell);
}
} else {
const timeCell = $('<td class="time-cell"></td>').append($('<span>').addClass('nobr'));
torrentRow.find('td:nth(0)').after(timeCell);
}
}
});
highlightTimes(times);
}
function highlightTimes(times) {
times.sort(descendingDate);
const nowMillis = new Date().getTime();
setTimeout(() => {
let latestTimeHighlighted = false;
for (let i in times) {
const time = times[i];
let timeMillis;
if (isNaN(time)) {
timeMillis = moment.utc(time, "MMM DD YYYY, HH:mm [UTC]").valueOf();
} else {
timeMillis = parseInt(time) * 1000;
}
if (nowMillis - timeMillis < RECENT_DAYS_IN_MILLIS) {
highlightRecentTime(time, '.group_torrent_header');
}
if (!latestTimeHighlighted && timeMillis) {
highlightLatestTime(time, '.group_torrent_header');
latestTimeHighlighted = true;
}
}
}, 100); // 100 milliseconds delay
}
function torrentsPage() {
$('.torrent_table tbody').each(function(i, group) {
let times = collectTimes(group);
times.sort(descendingDate);
const nowMillis = new Date().getTime();
for (let i in times) {
const time = times[i];
const parsedDate = moment.utc(time, "MMM DD YYYY, HH:mm");
if (parsedDate.isValid() && nowMillis - parsedDate.valueOf() < RECENT_DAYS_IN_MILLIS) {
highlightRecentTime(time, '.basic-movie-list__torrent-row', group);
}
}
if (times.length > 0) {
highlightLatestTime(times[0], '.basic-movie-list__torrent-row', group);
}
});
}
function artistAndBookmarksAndCollagesPage() {
if (!$('.torrent_table:visible thead tr th:contains("Time")').length) {
$('.torrent_table:visible thead tr th:nth(1)').after($('<th>Time</th>'));
}
$('.torrent_table:visible td.basic-movie-list__torrent-edition').each((i, edition) => {
$(edition).attr('colspan', parseInt($(edition).attr('colspan')) + 1);
});
$('.basic-movie-list__details-row').each((i, detailsRow) => {
const detailsCell = $(detailsRow).find('td:nth(1)');
detailsCell.attr('colspan', parseInt(detailsCell.attr('colspan')) + 1);
});
const torrentGroupTimes = {};
const torrentIdToTime = {};
const torrentGroups = new Set();
$(coverViewJsonData).each((i, data) => {
$(data.Movies).each((j, movieGroup) => {
const groupId = movieGroup.GroupId;
torrentGroups.add(groupId);
$(movieGroup.GroupingQualities).each((k, edition) => {
$(edition.Torrents).each((m, torrent) => {
if (!torrentGroupTimes[groupId]) {
torrentGroupTimes[groupId] = [];
}
const timeTitle = $(torrent.Time).attr('title');
const parsedDate = moment.utc(timeTitle, "MMM DD YYYY, HH:mm");
if (parsedDate.isValid()) {
torrentGroupTimes[groupId].push(parsedDate.toISOString());
torrentIdToTime[torrent.TorrentId] = formatTime(parsedDate);
} else {
console.error('Invalid date:', timeTitle);
torrentIdToTime[torrent.TorrentId] = 'Invalid date';
}
});
});
});
});
$('.basic-movie-list__torrent-row .basic-movie-list__torrent__action').each((i, element) => {
const parent = $(element).parent();
if (!parent.find('td.time-cell').length) {
const href = parent.find('a.torrent-info-link').attr('href');
const hrefParts = href.match(/torrents.php\?id=([0-9]+)&torrentid=([0-9]+)/);
const groupId = hrefParts[1];
const torrentId = hrefParts[2];
const timeText = torrentIdToTime[torrentId] || 'Unknown';
parent.after($('<td class="nobr time-cell">' + timeText + '</td>'));
}
});
torrentGroups.forEach(groupId => {
torrentGroupTimes[groupId] = torrentGroupTimes[groupId].sort(descendingDate);
});
const nowMillis = new Date().getTime();
for (const i in torrentGroupTimes) {
const times = torrentGroupTimes[i];
for (const j in times) {
const time = times[j];
if (nowMillis - moment.utc(time).valueOf() < RECENT_DAYS_IN_MILLIS) {
highlightRecentTime(time, '.basic-movie-list__torrent-row');
}
}
if (times.length > 1) {
highlightLatestTime(times[0], '.basic-movie-list__torrent-row');
}
}
}
function collageSubscriptionsPage() {
const torrentGroupTimes = {};
const torrentIdToTime = {};
const torrentGroups = new Set();
$(coverViewJsonData).each((j, jsonData) => {
$(jsonData.Movies).each((i, movieGroup) => {
const groupId = movieGroup.GroupId;
torrentGroups.add(groupId);
$(movieGroup.GroupingQualities).each((j, edition) => {
$(edition.Torrents).each((k, torrent) => {
if (!torrentGroupTimes[groupId]) {
torrentGroupTimes[groupId] = [];
}
const timeTitle = $(torrent.Time).attr('title');
const unixTimestamp = moment.utc(timeTitle, "MMM DD YYYY, HH:mm").unix();
if (!isNaN(unixTimestamp)) {
torrentGroupTimes[groupId].push(timeTitle);
torrentIdToTime[torrent.TorrentId] = formatTime(moment.unix(unixTimestamp));
} else {
console.error('Invalid date:', timeTitle);
torrentIdToTime[torrent.TorrentId] = 'Invalid date';
}
});
});
});
});
torrentGroups.forEach(groupId => {
torrentGroupTimes[groupId] = torrentGroupTimes[groupId].sort(descendingDate);
});
const nowMillis = new Date().getTime();
for (const i in torrentGroupTimes) {
const times = torrentGroupTimes[i];
for (const j in times) {
const time = times[j];
if (nowMillis - moment.utc(time + ' UTC').valueOf() < RECENT_DAYS_IN_MILLIS) {
highlightRecentTime(time, '.basic-movie-list__torrent-row');
}
}
if (times.length > 1) {
highlightLatestTime(times[0], '.basic-movie-list__torrent-row');
}
}
}
function isArtistPage() {
return window.location.pathname === '/artist.php';
}
function isBookmarksPage() {
return window.location.pathname === '/bookmarks.php';
}
function isCollageSubscriptionsPage() {
return (
window.location.pathname === '/collages.php' &&
window.location.search.includes('action=subscriptions')
);
}
function isCollagesPage() {
return window.location.pathname === '/collages.php';
}
function isTorrentsPage() {
return window.location.pathname === '/torrents.php';
}
function isTorrentsGroupPage() {
return isTorrentsPage() && window.location.search.includes('id=');
}
function isIgnoredTorrentsPage() {
return (
isTorrentsPage() &&
(
window.location.search.includes('type=downloaded') ||
window.location.search.includes('type=uploaded') ||
window.location.search.includes('type=leeching') ||
window.location.search.includes('type=snatched') ||
window.location.search.includes('type=seeding')
)
);
}
function collectTimes(group) {
let times = [];
$(group).find('.basic-movie-list__torrent-row').each(function (i, torrentRow) {
const spanTime = $(torrentRow).find('td span.time');
if (spanTime && spanTime.length > 0) {
const timeTitle = spanTime.attr('title');
times.push(moment.utc(timeTitle, "MMM DD YYYY, HH:mm").toISOString());
}
});
return times;
}
function descendingDate(a, b) {
const aDate = new Date(a);
const bDate = new Date(b);
if (aDate === bDate) return 0;
return aDate > bDate ? -1 : 1;
}
function highlightTime(time, rowClassName, textColor, backgroundColor, fontWeight) {
$(rowClassName + " span.time[title='" + time + "'], " + rowClassName + " span.release.time[title='" + time + "']").each(function(i, span) {
highlightSpan(span, textColor, backgroundColor, fontWeight);
});
}
function highlightSpan(span, textColor, backgroundColor, fontWeight) {
if (textColor) $(span).css('color', textColor);
if (backgroundColor) $(span).parent().css('background-color', backgroundColor);
if (fontWeight) $(span).css('font-weight', fontWeight);
}
function highlightRecentTime(time, rowClassName) {
highlightTime(time, rowClassName, HIGHLIGHT_RECENT_TEXT_COLOR, HIGHLIGHT_RECENT_BACKGROUND_COLOR, HIGHLIGHT_RECENT_FONT_WEIGHT);
}
function highlightLatestTime(time, rowClassName) {
highlightTime(time, rowClassName, HIGHLIGHT_LATEST_TEXT_COLOR, HIGHLIGHT_LATEST_BACKGROUND_COLOR, HIGHLIGHT_LATEST_FONT_WEIGHT);
}
function formatRoundedRelative(time) {
const relativeTimeText = time.html();
if (relativeTimeText !== undefined) {
const relativeTimeParts = relativeTimeText.split(' ');
if (relativeTimeParts.length > 1 && relativeTimeText !== 'Just now') {
time.html(`${relativeTimeParts[0]} ${relativeTimeParts[1].replace(',', '')} ago`);
} else if (relativeTimeText === 'Just now') {
time.html('Just now');
}
} else {
time.html('Undefined');
}
}
function formatTime(momentTime) {
if (TIME_FORMAT === 'relative') {
return momentTime.fromNow();
} else if (TIME_FORMAT === 'rounded-relative') {
const relativeTimeText = momentTime.fromNow(true);
const relativeTimeParts = relativeTimeText.split(' ');
if (relativeTimeParts.length > 1 && relativeTimeText !== 'Just now') {
return `${relativeTimeParts[0]} ${relativeTimeParts[1].replace(',', '')} ago`;
} else if (relativeTimeText === 'Just now') {
return 'Just now';
}
} else {
return momentTime.format(TIME_FORMAT);
}
}
main();
document.addEventListener('PTPAddReleasesFromOtherTrackersComplete', () => {
console.log("Rerunning Time Column to fix added releases");
handleNewTorrents(); // Run the script again for new torrents
});
})();