-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgazelle-file-count.js
293 lines (251 loc) · 10.7 KB
/
gazelle-file-count.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
// ==UserScript==
// @name Gazelle File Count
// @namespace github.com/euamotubaina
// @description Shows the number of tracks and/or files in each torrent
// @version 2024-12-15
// @match https://redacted.sh/torrents.php*id=*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM.getValue
// @grant GM.setValue
// @grant GM_registerMenuCommand
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js
// @downloadURL https://raw.githubusercontent.com/euamotubaina/userscripts/refs/heads/main/gazelle-file-count.js
// @updateURL https://raw.githubusercontent.com/euamotubaina/userscripts/refs/heads/main/gazelle-file-count.js
// ==/UserScript==
(function() {
"use strict";
// _____________________________________________________________
// _____________ Preferences ___________________________________
function createSettingsMenu() {
const fields = {
display: {
label: "Display",
type: "number",
default: GM_getValue("display", 1),
title: `How to display the file count:
1 = Total number of files in torrent (15)
2 = Number of tracks out of total files (12/15)
3 = Number of tracks plus extra files (12+3)
4 = Only the number of tracks (12)`,
},
checkEditions: {
label: "Highlight track count conflicts",
type: "checkbox",
default: GM_getValue("checkEditions", false),
title: "Highlight editions with conflicting track counts",
},
extraSizeLimit: {
label: "Highlight if extra files exceeds size",
type: "number",
default: GM_getValue("extraSizeLimit", 0),
title: "Highlight torrents with extra files exceeding this size (in MB; 0 = disable)",
},
tooltipAll: {
label: "Always show extra files size",
type: "checkbox",
default: GM_getValue("tooltipAll", false),
title: "Always show the size of extras when hovering over a torrent size (false = only the highlighted ones)",
}
}
GM_config.init({
id: "GfcConfig",
title: "Gazelle File Count Settings",
fields: fields,
css: `
#GfcConfig {
background: #333;
color: #fff;
padding: 20px;
width: 400px;
max-width: 90%;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#GfcConfig .field_label {
color: #fff;
width: 90%;
}
#GfcConfig .config_header {
color: #fff;
padding-bottom: 10px;
}
#GfcConfig .config_var {
display: flex;
justify-content: space-between;
padding: 8px 0;
}
#GfcConfig .config_var input {
width: 60%;
padding: 4px;
}
`,
events: {
save: function () {
const fields = GM_config.fields;
for (const field in fields) {
if (fields.hasOwnProperty(field)) {
const value = GM_config.get(field);
GM_setValue(field, value);
}
}
},
},
});
GM_registerMenuCommand("Configure", () => {
GM_config.open();
});
}
let display = GM_getValue("display", 1);
const checkEditions = GM_getValue("checkEditions", false);
let extraSizeLimit = GM_getValue("extraSizeLimit", 0);
const tooltipAll = GM_getValue("tooltipAll", false);
// _____________________________________________________________
// __________ End of Preferences _______________________________
function toBytes(size) {
const num = parseFloat(size.replace(',', ''));
const i = ' KMGT'.indexOf(size.charAt(size.length - 2));
return Math.round(num * Math.pow(1024, i));
}
function toSize(bytes) {
if (bytes <= 0) return '0 B';
const i = Math.floor(Math.log(bytes) / Math.log(1024));
const num = Math.round(bytes / Math.pow(1024, i));
return num + ' ' + ['B', 'KB', 'MB', 'GB', 'TB'][i];
}
function addStyle(css) {
const s = document.createElement('style');
s.textContent = css;
document.head.appendChild(s);
}
function setTitle(elem, str) {
elem.title = str;
if (window.jQuery && jQuery.fn.tooltipster) {
jQuery(elem).tooltipster({ delay: 500, maxWidth: 400 });
}
}
const table = document.getElementById('torrent_details');
if (table) {
const isMusic = !!document.querySelector('.box_artists');
extraSizeLimit = extraSizeLimit * 1048576;
addStyle(
'.gmfc_extrasize { background-color: rgba(228, 169, 29, 0.12) !important; }'
);
table.rows[0].insertCell(1).innerHTML = '<strong>Files</strong>';
let rows = table.querySelectorAll('.edition, .torrentdetails');
for (var i = rows.length; i--; ) {
++rows[i].cells[0].colSpan;
}
rows = table.getElementsByClassName('torrent_row');
const editions = {};
for (let i = rows.length; i--; ) {
const fileRows = rows[i].nextElementSibling.querySelectorAll('.filelist_table tr:not(:first-child)');
const numFiles = fileRows.length;
let numTracks = 0;
if (isMusic) {
let extraSize = 0;
for (let j = numFiles; j--; ) {
if (/\.(flac|mp3|m4a|ac3|dts)\s*$/i.test(fileRows[j].cells[0].textContent)) {
++numTracks;
} else if (extraSizeLimit || tooltipAll) {
extraSize += toBytes(fileRows[j].cells[1].textContent);
}
}
if (checkEditions) {
const ed = /edition_\d+/.exec(rows[i].className)[0];
editions[ed] = ed in editions && editions[ed] !== numTracks ? -1 : numTracks;
}
const largeExtras = extraSizeLimit && extraSize > extraSizeLimit;
if (largeExtras || tooltipAll) {
const sizeCell = rows[i].cells[1];
setTitle(sizeCell, 'Extras: ' + toSize(extraSize));
if (largeExtras) {
sizeCell.classList.add('gmfc_extrasize');
}
}
} else {
display = 0;
}
const cell = rows[i].insertCell(1);
cell.textContent = display < 2 ? numFiles : numTracks;
cell.className = 'td_filecount';
if (display != 3) {
cell.className = `number_column ${cell.className}`;
} else {
const numExtras = numFiles - numTracks;
if (numExtras) {
const sml = document.createElement('small');
sml.textContent = '+' + numExtras;
cell.appendChild(sml);
}
}
if (display == 2) {
cell.textContent += '/' + numFiles;
}
}
if (checkEditions) {
let sel = '';
for (let ed in editions) {
if (editions.hasOwnProperty(ed) && editions[ed] < 1) {
sel += [sel ? ',.' : '.', ed, ' > .td_filecount'].join('');
}
}
if (sel) addStyle(sel + '{background-color: rgba(236, 17, 0, 0.09) !important;}');
}
// Show filelist on filecount click
/*table.addEventListener('click', function (e) {
function get(type, id) {
return document.getElementById([type, id].join('_'));
}
const elem = e.target.nodeName != 'SMALL' ? e.target : e.target.parentNode;
if (elem.classList.contains('td_filecount')) {
const id = elem.parentNode.id.replace('torrent', '');
const tEl = get('torrent', id);
const fEl = get('files', id);
const show = [tEl.className, fEl.className].join().indexOf('hidden') > -1;
tEl.classList[show ? 'remove' : 'add']('hidden');
fEl.classList[show ? 'remove' : 'add']('hidden');
if (show) {
const sections = ['peers', 'downloads', 'snatches', 'reported', 'logs'];
for (let i = sections.length; i--; ) {
const el = get(sections[i], id);
if (el) el.classList.add('hidden');
}
}
}
}, false);
function checkAndDispatchEvents() {
if (display === 2 || display === 3) {
const event = new CustomEvent('vardisplay3');
document.dispatchEvent(event);
} else if (display === 1 || display === 4) {
const event = new CustomEvent('vardisplay4');
document.dispatchEvent(event);
}
}
// Run the function once when the script first runs
checkAndDispatchEvents();
// Set up an interval to repeat the event dispatching
const interval1 = setInterval(() => {
checkAndDispatchEvents();
}, 200); // Repeat every 200 ms
// Listen for the custom event 'OPSaddREDreleasescomplete'
document.addEventListener('OPSaddREDreleasescomplete', function () {
// console.log("Detected OPSaddREDreleasescomplete event, stopping event dispatching");
// Stop further dispatching of vardisplay3 and vardisplay4
clearInterval(interval1);
// Get all elements with the class 'RED_filecount_placeholder'
const fileCountElements = document.querySelectorAll('td.RED_filecount_placeholder');
// Loop through the elements and remove the 'hidden' class
fileCountElements.forEach(function (element) {
element.classList.remove('hidden');
});
//console.log("Finished processing RED_filecount_placeholder elements");
});*/
}
createSettingsMenu();
})();