Skip to content

Commit 533e0c6

Browse files
authored
v1.2.6: retry on error
1 parent 4f2083c commit 533e0c6

1 file changed

Lines changed: 29 additions & 18 deletions

File tree

NyaaTorrentHelper.user.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// @name Nyaa Torrent Helper
33
// @name:zh Nyaa 助手
44
// @namespace https://github.com/jc3213/userscript
5-
// @version 1.2.5
5+
// @version 1.2.6
66
// @description Nyaa Torrent ease to access torrent info and preview, filter search result, and aria2c intergration
77
// @description:zh 能便捷操作 Nyaa 的种子信息,预览缩微图,过滤搜索结果,联动aria2c
88
// @author jc3213
@@ -101,7 +101,7 @@ let filterMap = {
101101
delete filterMap[result];
102102
},
103103
_default_(result) {
104-
let regexp = new RegExp(result.replace(/[?.\(\)\[\]+]/g, '\\$&'), 'i');
104+
let regexp = new RegExp(result.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i');
105105
for (let tr of torrents) {
106106
regexp.test(tr.info.name)
107107
? tr.classList.remove('nyaa-hidden')
@@ -163,7 +163,6 @@ for (let tr of document.querySelectorAll('table > tbody > tr')) {
163163
let a = [...name.children].at(-1);
164164
let url = a.href;
165165
let [{ href: magnet }, { href: torrent } = {}] = [...link.children].reverse();
166-
magnet = magnet.slice(0, magnet.indexOf('&'));
167166
tr.info = { url, magnet, torrent, size: size.textContent, name: a.textContent };
168167
torrents.push(tr);
169168
if (GM_getValue(url)) {
@@ -199,29 +198,41 @@ for (let tr of document.querySelectorAll('table > tbody > tr')) {
199198
});
200199
}
201200

202-
async function getTorrentDetail(tr) {
203-
let { url, name, torrent, magnet, size } = tr.info;
204-
let info = GM_getValue(url);
205-
if (!info) {
206-
if (working[url]) {
207-
throw new SyntaxError(`${GM_info.script.name} is processing "url"`);
208-
}
209-
working[url] = true;
210-
let site = new Set();
211-
let image = new Set();
212-
let text = await fetch(url).then((res) => res.text()).catch((err) => working.delete(url));
201+
function fetchTorrent(url, tr, retries = 3) {
202+
if (working[url]) {
203+
throw new SyntaxError(`${GM_info.script.name} is processing "${url}"`);
204+
}
205+
working[url] = true;
206+
let site = new Set();
207+
let image = new Set();
208+
let info;
209+
return fetch(url).then((res) => res.text()).then((text) => {
213210
let result = text.match(/<div[^>]*id=["']torrent-description["'][^>]*>([\s\S]*?)<\/div>/i)[1];
214211
let urls = result.match(/https?:\/\/[^\]&)* ]+/g);
215212
if (urls) {
216-
for (let url of urls) {
217-
url.match(/.(jpe?g|png|gif|avif|bmp|webp)/) ? image.add(url) : site.add(url);
213+
for (let u of urls) {
214+
u.match(/\.(jpe?g|png|gif|avif|bmp|webp)/i) ? image.add(u) : site.add(u);
218215
}
219216
}
220217
info = { site: [...site], image: [...image] };
221218
GM_setValue(url, info);
222219
tr.classList.add('nyaa-cached');
223220
delete working[url];
224-
}
221+
return info;
222+
}).catch((err) => {
223+
delete working[url];
224+
//if (retries === 0) throw err;
225+
return new Promise((resolve) => {
226+
setTimeout(() => {
227+
resolve(fetchTorrent(url, tr, --retries));
228+
}, 5000);
229+
});
230+
});
231+
}
232+
233+
async function getTorrentDetail(tr) {
234+
let { url, name, torrent, magnet, size } = tr.info;
235+
let info = GM_getValue(url) ?? fetchTorrent(url, tr);
225236
Object.assign(info, tr.info);
226237
return info;
227238
}
@@ -233,7 +244,7 @@ async function getClipboardInfo(tr) {
233244
${name} (${size})
234245
${i18n.preview}
235246
${image.length ? image.join('\n ') : site.length ? site.join('\n ') : 'Null'}
236-
${torrent ? `${i18n.torrent}\n ${torrent}\n` : ''}${i18n.magnet}\n ${magnet}`;
247+
${torrent ? `${i18n.torrent}\n ${torrent}\n` : ''}${i18n.magnet}\n ${magnet.slice(0, magnet.indexOf('&'))}`;
237248
}
238249

239250
// show/open preview

0 commit comments

Comments
 (0)