-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops-red-exact-filesizes.js
170 lines (149 loc) · 5.9 KB
/
ops-red-exact-filesizes.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
// ==UserScript==
// @name OPS-RED: Exact filesizes
// @description Get exact size of files. Click [SZ] next to [PL]
// @version 2024-12-20_03
// @namespace github.com/euamotubaina
// @author userscript1
// @match https://redacted.sh/torrents.php?id=*
// @match https://orpheus.network/torrents.php?id=*
// @license GPLv3
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_listValues
// @grant GM_registerMenuCommand
// @require https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.5.0/lz-string.min.js
// @downloadURL https://raw.githubusercontent.com/euamotubaina/userscripts/main/ops-red-exact-filesizes.js
// @updateURL https://raw.githubusercontent.com/euamotubaina/userscripts/main/ops-red-exact-filesizes.js
// ==/UserScript==
(function() {
'use strict';
const url = new URL(location);
const isOps = url.hostname === 'orpheus.network';
const apiURL = `https://${url.hostname}/ajax.php?action=torrent&id=`;
const CACHE_EXPIRY_DAYS = GM_getValue("CACHE_EXPIRY_TIME", 7);
const CACHE_EXPIRY_TIME = CACHE_EXPIRY_DAYS * 24 * 60 * 60 * 1000;
function flushCache() {
const keys = GM_listValues();
keys.forEach(key => GM_deleteValue(key));
alert("Cache has been flushed.");
}
GM_registerMenuCommand("Flush cache", () => {
flushCache();
});
async function getApi(torrentId) {
let cacheData;
const cacheKey = `${isOps ? 'OPS' : 'RED'}_${torrentId}`;
const compressedData = GM_getValue(cacheKey, null);
if (compressedData && typeof compressedData === "string") {
const decompressedData = LZString.decompress(compressedData);
if (decompressedData) {
cacheData = JSON.parse(decompressedData);
const currentTime = Date.now();
if (currentTime - cacheData.timestamp > CACHE_EXPIRY_TIME) {
GM_deleteValue(cacheKey);
cacheData = null;
};
};
};
if (!cacheData) {
const data = await ((await fetch(apiURL + torrentId)).json())
if (!data) {
throw new Error("Something went wrong with the API");
}
cacheData = {
timestamp: Date.now(),
data: data,
};
const compressedData = LZString.compress(JSON.stringify(cacheData));
GM_setValue(cacheKey, compressedData);
}
return cacheData.data;
};
function normalizeName(name) {
return new DOMParser().parseFromString(name, 'text/html').querySelector('html').textContent;
};
function parseFilesData(torRowEl, filesEl, filesData) {
let totalSZ = torRowEl.querySelector('.td_totalexactsize');
const totalSizeEl = torRowEl.querySelector(':scope > td.td_size, :scope > td.nobr:not(.td_filecount, .td_totalexactsize)');
if (!totalSZ) {
totalSZ = totalSizeEl.cloneNode();
totalSZ.classList.add('td_totalexactsize');
totalSZ.classList.remove('td_size');
totalSZ.textContent = filesData.totalSize.toLocaleString();
torRowEl.insertBefore(totalSZ, totalSizeEl);
} else {
totalSZ.classList.toggle('hidden');
}
totalSizeEl.classList.toggle('hidden');
const matchRowEl = document.getElementById(`${torRowEl.id}_match`);
if (matchRowEl) {
const matchTotalSZ = matchRowEl.querySelector('.td_totalexactsize');
const matchTotalSizeEl = matchRowEl.querySelector('.td_size');
matchTotalSZ.classList.toggle('hidden');
matchTotalSizeEl.classList.toggle('hidden');
}
filesEl.querySelectorAll('tr:not(.colhead_dark)').forEach((tr, i) => {
const fileData = filesData.fileList[i];
if (tr.children[0].textContent === fileData.name) {
let tdSZ = tr.querySelector('.td_exactsize');
if (!tdSZ) {
tdSZ = tr.children[1].cloneNode();
tdSZ.classList.add('td_exactsize');
tdSZ.textContent = fileData.size.toLocaleString();
tr.children[1].classList.add('hidden');
tr.appendChild(tdSZ);
} else {
tdSZ.classList.toggle('hidden');
tr.children[1].classList.toggle('hidden');
};
};
});
};
document.querySelectorAll(
'a[title="Permalink"],.button_pl,\
.torrent_links_block a.tooltip[href^="torrents.php?torrentid="]'
).forEach(a => {
const torrentId = a.href.split('torrentid=')[1];
const szEl = document.createElement('a');
szEl.href = "#";
szEl.classList.add("tooltip", "button_sz");
szEl.dataset.id = torrentId;
szEl.title = "Exact filesizes";
szEl.textContent = "SZ";
a.parentElement.insertBefore(szEl, a);
const divEl = document.createTextNode(' | ');
a.parentElement.insertBefore(divEl, a);
});
let firstRun = true;
document.querySelectorAll('a.button_sz').forEach(a => {
a.addEventListener('click', async evt => {
evt.preventDefault();
const torRowEl = document.getElementById(`torrent${evt.target.dataset.id}`)
const filesEl = document.getElementById(`files_${evt.currentTarget.dataset.id}`);
if (filesEl.classList.contains('hidden')) {
evt.currentTarget.parentElement.nextElementSibling.click();
document.getElementById(`torrent_${evt.currentTarget.dataset.id}`)
.querySelector('a[onclick^="show_files"], a.view-filelist')
.click();
}
const res = await getApi(evt.target.dataset.id);
const filesData = {
fileList: res.response.torrent.fileList.split('|||').map(f => {
const s = f.split(/{{3}|}{3}/);
return {name: normalizeName(s[0]), size: parseInt(s[1])};
}),
get totalSize() {
return this.fileList.reduce((acc, cv) => acc + cv.size, 0);
}
};
if (isOps && firstRun) {
const observer = new MutationObserver(() => parseFilesData(torRowEl, filesEl, filesData));
observer.observe(filesEl, {childList: true});
firstRun = false;
} else {
parseFilesData(torRowEl, filesEl, filesData);
}
});
});
})();