-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNyaaTorrentHelper.user.js
More file actions
274 lines (260 loc) · 8.6 KB
/
NyaaTorrentHelper.user.js
File metadata and controls
274 lines (260 loc) · 8.6 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
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
// ==UserScript==
// @name Nyaa Torrent Helper
// @name:zh Nyaa 助手
// @namespace https://github.com/jc3213/userscript
// @version 1.3.0
// @description Nyaa Torrent ease to access torrent info and preview, filter search result, and aria2c intergration
// @description:zh 能便捷操作 Nyaa 的种子信息,预览缩微图,过滤搜索结果,联动aria2c
// @author jc3213
// @match *://*.nyaa.si/*
// @exclude *://*.nyaa.si/view/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM_deleteValues
// @grant GM_listValues
// @grant GM_openInTab
// ==/UserScript==
// variables
let torrents = [];
let preview = {};
let working = {};
let selected = new Set();
let keyword;
let filterResult = {};
let active = document.querySelector('.pagination > .active');
// i18n
let messages = {
'en-US': {
name: 'Name:',
preview: 'Preview:',
torrent: 'Torrent:',
magnet: 'Magnet:',
prompt: 'Enter a filter keyword:',
oncopy: 'Confirm copying selected torrent data to clipboard?',
aria2c: 'Confirm sending JSON-RPC request to aria2c?',
onsend: 'JSON-RPC request has been sent.',
clear: 'Confirm clearing all cached torrent data?',
onclear: 'All cached data has been cleared.'
},
'zh-CN': {
name: '名字:',
preview: '预览:',
torrent: '种子:',
magnet: '磁链:',
prompt: '输入过滤关键词:',
oncopy: '确认复制所选种子数据到剪贴板?',
aria2c: '确认向 aria2c 发送 JSON-RPC 请求?',
onsend: 'JSON-RPC 请求已发送。',
clear: '确认清除所有已缓存的种子数据?',
onclear: '所有缓存数据已清除。'
}
};
let i18n = messages[navigator.language] ?? messages['en-US'];
// css
let css = document.createElement('style');
css.textContent = `
.nyaa-hidden { display: none; }
.nyaa-cached > td { background-color: #cde7f0 !important; }
.nyaa-checked > td { background-color: #f0d8d8 !important; }
.nyaa-preview { position: absolute; }
`;
document.body.appendChild(css);
// shortcut
function filterTorrents() {
let result = prompt(i18n.prompt, keyword);
if (result === null) {
return;
}
if (result === '' || result === keyword) {
for (let tr of torrents) {
tr.classList.remove('nyaa-hidden');
}
keyword = '';
delete filterResult[result];
} else {
let regexp = new RegExp(result.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i');
for (let tr of torrents) {
regexp.test(tr.info.name)
? tr.classList.remove('nyaa-hidden')
: tr.classList.add('nyaa-hidden');
}
keyword = result;
}
}
async function copyToClipboard() {
if (!confirm(i18n.oncopy)) return;
let data = [];
for (let tr of selected) {
data.push(getClipboardInfo(tr));
}
let info = await Promise.all(data);
let copy = info.join('\n\n');
navigator.clipboard.writeText(copy);
alert(copy);
}
function downloadWithAria2() {
if (!confirm(i18n.aria2c)) return;
let params = [];
for (let tr of selected) {
params.push(tr.info.magnet);
}
postMessage({ aria2c: 'aria2c_download', params });
alert(i18n.onsend);
}
async function clearStorage() {
if (!confirm(i18n.clear)) return;
GM_deleteValues(GM_listValues());
for (let tr of torrents) {
tr.classList.remove('nyaa-cached');
}
alert(i18n.onclear);
}
document.addEventListener('keydown', (event) => {
let { key, ctrlKey, altKey, shiftKey } = event;
key = key.toLowerCase();
if (ctrlKey) {
let button = key === 'arrowleft' ? active.previousElementSibling : key === 'arrowright' ? active.nextElementSibling : null;
if (button && button.className !== 'disabled') {
event.preventDefault();
button.children[0].click();
}
return;
}
if (!altKey) return;
if (shiftKey) {
if (key === 'e') {
event.preventDefault();
clearStorage();
}
} else {
if (key === 'c') {
event.preventDefault();
copyToClipboard();
} else if (key === 'd') {
event.preventDefault();
downloadWithAria2()
} else if (key === 'f') {
event.preventDefault();
filterTorrents();
}
}
});
// get torrent info
for (let tr of document.querySelectorAll('table > tbody > tr')) {
let [genre, name, link, size] = tr.children;
let id = name.children[0].href.match(/\/([^/#]+)(?:#comments)?$/)[1];
let url = '/view/' + id;
let [{ href: magnet }, { href: torrent } = {}] = [...link.children].reverse();
tr.info = { id, url, magnet, torrent, size: size.textContent, name: name.textContent };
torrents.push(tr);
if (GM_getValue(id)) {
tr.classList.add('nyaa-cached');
}
name.addEventListener('contextmenu', async (event) => {
event.preventDefault();
let { ctrlKey, altKey, layerX, layerY } = event;
if (ctrlKey) {
let copy = await getClipboardInfo(tr);
navigator.clipboard.writeText(copy)
} else if (altKey) {
postMessage({ aria2c: 'aria2c_download', params: [magnet] });
} else {
getTorrentPreview(tr, layerY, layerX);
}
});
tr.addEventListener('mousedown', (event) => {
if (!event.shiftKey) return;
event.preventDefault();
});
tr.addEventListener('click', async (event) => {
let { shiftKey, ctrlKey } = event;
if (ctrlKey) {
event.preventDefault();
GM_deleteValue(id);
tr.classList.remove('nyaa-cached');
} else if (shiftKey) {
event.preventDefault();
selected.has(tr) ? selected.delete(tr) : selected.add(tr);
tr.classList.toggle('nyaa-checked');
}
});
}
function fetchTorrent(id, url, tr, retries = 3) {
if (working[url]) {
throw new SyntaxError(`${GM_info.script.name} is processing "${url}"`);
}
working[url] = true;
let site = new Set();
let image = new Set();
let info = {};
return fetch(url).then((res) => res.text()).then((text) => {
let result = text.match(/<div[^>]*id=["']torrent-description["'][^>]*>([\s\S]*?)<\/div>/i)[1];
let urls = result.match(/https?:\/\/[^\]&)* ]+/g);
if (urls) {
for (let u of urls) {
/\.(jpe?g|png|gif|avif|bmp|webp)/i.test(u) ? image.add(u) : site.add(u);
}
}
if (image.size > 0) {
info.image = [...image][0];
}
else if (site.size > 0) {
info.site = [...site][0];
}
GM_setValue(id, info);
tr.classList.add('nyaa-cached');
delete working[url];
return info;
}).catch((err) => {
delete working[url];
if (retries === 0) throw {};
return new Promise((resolve) => {
setTimeout(() => {
resolve(fetchTorrent(id, url, tr, --retries));
}, 5000);
});
});
}
async function getTorrentDetail(tr) {
let { id, url, name, torrent, magnet, size } = tr.info;
let info = GM_getValue(id) ?? fetchTorrent(id, url, tr);
Object.assign(info, tr.info);
return info;
}
// copy info to clipboard
async function getClipboardInfo(tr) {
let { url, name, torrent, magnet, size, image, site } = await getTorrentDetail(tr);
return `${i18n.name}
${name} (${size})
${i18n.preview}
${image ?? site ?? 'Null'}
${torrent ? `${i18n.torrent}\n ${torrent}\n` : ''}${i18n.magnet}\n ${magnet.slice(0, magnet.indexOf('&'))}`;
}
// show/open preview
async function getTorrentPreview(tr, top, left) {
let { image, site } = await getTorrentDetail(tr);
if (image) {
let img = preview[image];
if (!img) {
img = document.createElement('img');
img.src = redirectURL(image);
img.className = 'nyaa-preview';
img.addEventListener('click', event => img.remove());
preview[image] = img;
}
img.style.cssText = `top: ${top}px; left: ${left}px;`;
document.body.append(img);
} else if (site) {
GM_openInTab(site);
}
}
function redirectURL(url) {
let start = url.indexOf('//') + 2;
let end = url.indexOf('/', start);
let host = url.substring(start, end);
if (host === 'files.catbox.moe') {
return url.replace('files.catbox.moe', 'i0.wp.com/files.catbox.moe') + '?ssl=1';
}
return url;
}