Alex

Alex / Nyaa - Personal Tweaks

Last active 4 months ago

Like 0

Miscelleanous tweaks applied to Nyaa.si

Alex revised this gist 1 year ago · 5c617be

No changes

Alex revised this gist 1 year ago · a0f477a

1 file changed, 64 insertions, 6 deletions

nyaa-shit.user.js
@@ -3,7 +3,7 @@
3 3 // @namespace github.com/Decicus
4 4 // @match https://nyaa.si/*
5 5 // @grant GM_setClipboard
6 - // @version 1.4.1
6 + // @version 1.5.0
7 7 // @author Decicus
8 8 // @description Adds some extra functionality to Nyaa.
9 9 // ==/UserScript==
@@ -194,16 +194,21 @@ const titleReplacements = [
194 194 search: / \"[A-z0-9-!\s]+\"$/,
195 195 replacement: '',
196 196 },
197 - {
198 - comment: 'Move release group to the end',
199 - search: /^\[([-\w ]+)\] (.+)$/,
200 - replacement: '$2 [$1]',
201 - },
197 + // {
198 + // comment: 'Move release group to the end',
199 + // search: /^\[([-\w ]+)\] (.+)$/,
200 + // replacement: '$2 [$1]',
201 + // },
202 202 {
203 203 comment: 'Colon space to space dash space',
204 204 search: /: /g,
205 205 replacement: ' - ',
206 206 },
207 + {
208 + comment: 'Ensure seasons have two numbers',
209 + search: / \(Season (\d)\)/g,
210 + replacement: ' (Season 0$1)',
211 + },
207 212 {
208 213 comment: 'Season naming so Sonarr doesnt get confused because its dumb',
209 214 search: / \(Season (\d+)\)/g,
@@ -294,6 +299,58 @@ function titleHandler()
294 299 titleParent.insertAdjacentElement('afterbegin', buttonParent);
295 300 }
296 301
302 + /**
303 + * Used by `copyFilenameHandler()` to do the actual copying
304 + */
305 + function copyFilenameTrigger(ev)
306 + {
307 + const { target } = ev;
308 +
309 + // Trust no one, not even yourself
310 + if (!target) {
311 + return;
312 + }
313 +
314 + // We clone it here because I want to remove the file size that's normally at the end
315 + // If I simply do a `remove()` on the usual element, it will actually remove it from the DOM.
316 + const parent = target.parentElement.cloneNode(true);
317 + const fileSize = parent.querySelector('.file-size');
318 +
319 + if (fileSize) {
320 + fileSize.remove();
321 + }
322 +
323 + const filename = parent.textContent.trim();
324 +
325 + target.classList.remove('fa-file');
326 + target.classList.add('fa-check');
327 +
328 + GM_setClipboard(filename);
329 +
330 + setTimeout(() => {
331 + target.classList.remove('fa-check');
332 + target.classList.add('fa-file');
333 + }, 1500);
334 + }
335 +
336 + /**
337 + * Clicking the file icon in front of each file will allow you to copy the filename
338 + */
339 + async function copyFilenameHandler()
340 + {
341 + const fileList = document.querySelector('.torrent-file-list');
342 + if (!fileList) {
343 + return;
344 + }
345 +
346 + const fileIcons = fileList.querySelectorAll('.fa-file');
347 +
348 + for (const icon of fileIcons)
349 + {
350 + icon.addEventListener('click', copyFilenameTrigger);
351 + }
352 + }
353 +
297 354 function bbcodeConverter(html) {
298 355 html = html.replace(/<pre(.*?)>(.*?)<\/pre>/gmi, "[code]$2[/code]");
299 356 html = html.replace(/<h[1-7](.*?)>(.*?)<\/h[1-7]>/, "\n[h]$2[/h]\n");
@@ -559,6 +616,7 @@ function init() {
559 616 changeToCopy();
560 617 titleHandler();
561 618 descriptionHandler();
619 + copyFilenameHandler();
562 620 torrentListViewHandler();
563 621 calculateTotalSize();
564 622 widerContainers();

Alex revised this gist 2 years ago · 0a19aac

1 file changed, 25 insertions, 15 deletions

nyaa-shit.user.js
@@ -3,7 +3,7 @@
3 3 // @namespace github.com/Decicus
4 4 // @match https://nyaa.si/*
5 5 // @grant GM_setClipboard
6 - // @version 1.4.0
6 + // @version 1.4.1
7 7 // @author Decicus
8 8 // @description Adds some extra functionality to Nyaa.
9 9 // ==/UserScript==
@@ -83,6 +83,9 @@ function calculateTotalSize()
83 83 return;
84 84 }
85 85
86 + const sampleTorrent = "https://nyaa.si/download/222409.torrent";
87 + const sampleMagnet = "magnet:?xt=urn:btih:db0ffe8174317b0b0ee4beb7b54f558bb9089746&dn=%5Beoy%5D%20dark%20dragoon%20-%2001.txt&tr=http%3A%2F%2Fnyaa.tracker.wf%3A7777%2Fannounce&tr=udp%3A%2F%2Fopen.stealth.si%3A80%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337%2Fannounce&tr=udp%3A%2F%2Fexodus.desync.com%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.torrent.eu.org%3A451%2Fannounce";
88 +
86 89 const html = `<tr class="default" id="all-torrents-size">
87 90 <td class="text-center">
88 91 <a href="#all-torrents-size" title="Total size">
@@ -92,7 +95,12 @@ function calculateTotalSize()
92 95 <td colspan="2">
93 96 <a href="#all-torrents-size">Total size</a>
94 97 </td>
95 - <td class="text-center"><!-- Download --></td>
98 + <td class="text-center">
99 + <!-- These elements purely exist to avoid any issues with extensions or userscripts that would normally expect valid torrent/magnet URIs -->
100 + <!-- In my case I only found an issue with NyaaBlue from SeaDex -->
101 + <a href="${sampleTorrent}" style="display: none;"><!-- Download torrent --></a>
102 + <a href="${sampleMagnet}" style="display: none;"><!-- Magnet URI --></a>
103 + </td>
96 104 <td class="text-center">${totalSize}</td>
97 105 <td class="text-center"><!-- Timestamp --></td>
98 106 <td class="text-center"><!-- Seeders --></td>
@@ -528,21 +536,23 @@ function torrentListViewHandler()
528 536 parentElement.insertAdjacentElement('beforebegin', amountCopyLinks);
529 537 }
530 538
531 - function changeContainers()
539 + /**
540 + * This previously changed some `.container` classes to `.container-fluid`,
541 + * but since that breaks other userscripts, such as NyaaBlue from SeaDex,
542 + * I figured it'd be better to simply override the CSS width that .container usually applies.
543 + */
544 + function widerContainers()
532 545 {
533 - const selectors = ['.navbar-static-top > .container', 'body > .container'];
534 - for (const selector of selectors)
535 - {
536 - const element = document.querySelector(selector);
537 - if (!element) {
538 - console.error('Could not find any element with selector', selector);
539 - continue;
546 + const styleDoc = document.createElement('style');
547 + styleDoc.setAttribute('type', 'text/css');
548 +
549 + styleDoc.textContent = `
550 + .container {
551 + width: auto !important;
540 552 }
553 + `;
541 554
542 - element.classList.remove('container');
543 - element.classList.add('container-fluid');
544 - console.log('Element updated with container-fluid', selector, element);
545 - }
555 + document.head.insertAdjacentElement('beforeend', styleDoc);
546 556 }
547 557
548 558 function init() {
@@ -551,7 +561,7 @@ function init() {
551 561 descriptionHandler();
552 562 torrentListViewHandler();
553 563 calculateTotalSize();
554 - changeContainers();
564 + widerContainers();
555 565 }
556 566
557 567 init();

Alex revised this gist 2 years ago · 0c3eb88

1 file changed, 19 insertions, 2 deletions

nyaa-shit.user.js
@@ -3,7 +3,7 @@
3 3 // @namespace github.com/Decicus
4 4 // @match https://nyaa.si/*
5 5 // @grant GM_setClipboard
6 - // @version 1.3.1
6 + // @version 1.4.0
7 7 // @author Decicus
8 8 // @description Adds some extra functionality to Nyaa.
9 9 // ==/UserScript==
@@ -60,7 +60,6 @@ function calculateTotalSize()
60 60 const torrentBytes = sizeNumber * multiplier;
61 61
62 62 totalBytes += torrentBytes;
63 - console.log(totalBytes);
64 63 }
65 64
66 65 const multiplierKeys = Object.keys(multipliers).reverse();
@@ -529,12 +528,30 @@ function torrentListViewHandler()
529 528 parentElement.insertAdjacentElement('beforebegin', amountCopyLinks);
530 529 }
531 530
531 + function changeContainers()
532 + {
533 + const selectors = ['.navbar-static-top > .container', 'body > .container'];
534 + for (const selector of selectors)
535 + {
536 + const element = document.querySelector(selector);
537 + if (!element) {
538 + console.error('Could not find any element with selector', selector);
539 + continue;
540 + }
541 +
542 + element.classList.remove('container');
543 + element.classList.add('container-fluid');
544 + console.log('Element updated with container-fluid', selector, element);
545 + }
546 + }
547 +
532 548 function init() {
533 549 changeToCopy();
534 550 titleHandler();
535 551 descriptionHandler();
536 552 torrentListViewHandler();
537 553 calculateTotalSize();
554 + changeContainers();
538 555 }
539 556
540 557 init();

Alex revised this gist 3 years ago · 0b187b9

1 file changed, 5 insertions, 1 deletion

nyaa-shit.user.js
@@ -3,7 +3,7 @@
3 3 // @namespace github.com/Decicus
4 4 // @match https://nyaa.si/*
5 5 // @grant GM_setClipboard
6 - // @version 1.3.0
6 + // @version 1.3.1
7 7 // @author Decicus
8 8 // @description Adds some extra functionality to Nyaa.
9 9 // ==/UserScript==
@@ -80,6 +80,10 @@ function calculateTotalSize()
80 80 }
81 81
82 82 const torrentList = document.querySelector('.torrent-list tbody');
83 + if (!torrentList) {
84 + return;
85 + }
86 +
83 87 const html = `<tr class="default" id="all-torrents-size">
84 88 <td class="text-center">
85 89 <a href="#all-torrents-size" title="Total size">

Alex revised this gist 3 years ago · e5a6bf1

1 file changed, 68 insertions, 68 deletions

nyaa-shit.user.js
@@ -81,21 +81,21 @@ function calculateTotalSize()
81 81
82 82 const torrentList = document.querySelector('.torrent-list tbody');
83 83 const html = `<tr class="default" id="all-torrents-size">
84 - <td class="text-center">
85 - <a href="#all-torrents-size" title="Total size">
86 - 💽
87 - </a>
88 - </td>
89 - <td colspan="2">
84 + <td class="text-center">
85 + <a href="#all-torrents-size" title="Total size">
86 + 💽
87 + </a>
88 + </td>
89 + <td colspan="2">
90 90 <a href="#all-torrents-size">Total size</a>
91 - </td>
92 - <td class="text-center"><!-- Download --></td>
93 - <td class="text-center">${totalSize}</td>
94 - <td class="text-center"><!-- Timestamp --></td>
95 - <td class="text-center"><!-- Seeders --></td>
96 - <td class="text-center"><!-- Leechers --></td>
97 - <td class="text-center"><!-- Completed --></td>
98 - </tr>`;
91 + </td>
92 + <td class="text-center"><!-- Download --></td>
93 + <td class="text-center">${totalSize}</td>
94 + <td class="text-center"><!-- Timestamp --></td>
95 + <td class="text-center"><!-- Seeders --></td>
96 + <td class="text-center"><!-- Leechers --></td>
97 + <td class="text-center"><!-- Completed --></td>
98 + </tr>`;
99 99
100 100 torrentList.insertAdjacentHTML('beforeend', html);
101 101 }
@@ -285,63 +285,63 @@ function titleHandler()
285 285
286 286 function bbcodeConverter(html) {
287 287 html = html.replace(/<pre(.*?)>(.*?)<\/pre>/gmi, "[code]$2[/code]");
288 - html = html.replace(/<h[1-7](.*?)>(.*?)<\/h[1-7]>/, "\n[h]$2[/h]\n");
289 - //paragraph handling:
290 - //- if a paragraph opens on the same line as another one closes, insert an extra blank line
291 - //- opening tag becomes two line breaks
292 - //- closing tags are just removed
293 - // html += html.replace(/<\/p><p/<\/p>\n<p/gi;
294 - // html += html.replace(/<p[^>]*>/\n\n/gi;
295 - // html += html.replace(/<\/p>//gi;
288 + html = html.replace(/<h[1-7](.*?)>(.*?)<\/h[1-7]>/, "\n[h]$2[/h]\n");
289 + //paragraph handling:
290 + //- if a paragraph opens on the same line as another one closes, insert an extra blank line
291 + //- opening tag becomes two line breaks
292 + //- closing tags are just removed
293 + // html += html.replace(/<\/p><p/<\/p>\n<p/gi;
294 + // html += html.replace(/<p[^>]*>/\n\n/gi;
295 + // html += html.replace(/<\/p>//gi;
296 296
297 297 html = html.replace(/<\/p>\n<p>/gi, "\n\n\n");
298 298
299 - html = html.replace(/<br(.*?)>/gi, "\n");
300 - html = html.replace(/<textarea(.*?)>(.*?)<\/textarea>/gmi, "\[code]$2\[\/code]");
301 - html = html.replace(/<b>/gi, "[b]");
302 - html = html.replace(/<i>/gi, "[i]");
303 - html = html.replace(/<u>/gi, "[u]");
304 - html = html.replace(/<\/b>/gi, "[/b]");
305 - html = html.replace(/<\/i>/gi, "[/i]");
306 - html = html.replace(/<\/u>/gi, "[/u]");
307 - html = html.replace(/<em>/gi, "[b]");
308 - html = html.replace(/<\/em>/gi, "[/b]");
309 - html = html.replace(/<strong>/gi, "[b]");
310 - html = html.replace(/<\/strong>/gi, "[/b]");
311 - html = html.replace(/<cite>/gi, "[i]");
312 - html = html.replace(/<\/cite>/gi, "[/i]");
313 - html = html.replace(/<font color="(.*?)">(.*?)<\/font>/gmi, "[color=$1]$2[/color]");
314 - html = html.replace(/<font color=(.*?)>(.*?)<\/font>/gmi, "[color=$1]$2[/color]");
315 - html = html.replace(/<link(.*?)>/gi, "");
316 - html = html.replace(/<li(.*?)>(.*?)<\/li>/gi, "[*]$2");
317 - html = html.replace(/<ul(.*?)>/gi, "[list]");
318 - html = html.replace(/<\/ul>/gi, "[/list]");
319 - html = html.replace(/<div>/gi, "\n");
320 - html = html.replace(/<\/div>/gi, "\n");
321 - html = html.replace(/<td(.*?)>/gi, " ");
322 - html = html.replace(/<tr(.*?)>/gi, "\n");
323 -
324 - html = html.replace(/<img(.*?)src="(.*?)"(.*?)>/gi, "[img]$2[/img]");
325 - html = html.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, "[url=$2]$4[/url]");
326 -
327 - html = html.replace(/<head>(.*?)<\/head>/gmi, "");
328 - html = html.replace(/<object>(.*?)<\/object>/gmi, "");
329 - html = html.replace(/<script(.*?)>(.*?)<\/script>/gmi, "");
330 - html = html.replace(/<style(.*?)>(.*?)<\/style>/gmi, "");
331 - html = html.replace(/<title>(.*?)<\/title>/gmi, "");
332 - html = html.replace(/<!--(.*?)-->/gmi, "\n");
333 -
334 - html = html.replace(/\/\//gi, "/");
335 - html = html.replace(/http:\//gi, "http://");
336 -
337 - html = html.replace(/<(?:[^>'"]*|(['"]).*?\1)*>/gmi, "");
338 - html = html.replace(/\r\r/gi, "");
339 - html = html.replace(/\[img]\//gi, "[img]");
340 - html = html.replace(/\[url=\//gi, "[url=");
341 -
342 - html = html.replace(/(\S)\n/gi, "$1 ");
343 -
344 - return html;
299 + html = html.replace(/<br(.*?)>/gi, "\n");
300 + html = html.replace(/<textarea(.*?)>(.*?)<\/textarea>/gmi, "\[code]$2\[\/code]");
301 + html = html.replace(/<b>/gi, "[b]");
302 + html = html.replace(/<i>/gi, "[i]");
303 + html = html.replace(/<u>/gi, "[u]");
304 + html = html.replace(/<\/b>/gi, "[/b]");
305 + html = html.replace(/<\/i>/gi, "[/i]");
306 + html = html.replace(/<\/u>/gi, "[/u]");
307 + html = html.replace(/<em>/gi, "[b]");
308 + html = html.replace(/<\/em>/gi, "[/b]");
309 + html = html.replace(/<strong>/gi, "[b]");
310 + html = html.replace(/<\/strong>/gi, "[/b]");
311 + html = html.replace(/<cite>/gi, "[i]");
312 + html = html.replace(/<\/cite>/gi, "[/i]");
313 + html = html.replace(/<font color="(.*?)">(.*?)<\/font>/gmi, "[color=$1]$2[/color]");
314 + html = html.replace(/<font color=(.*?)>(.*?)<\/font>/gmi, "[color=$1]$2[/color]");
315 + html = html.replace(/<link(.*?)>/gi, "");
316 + html = html.replace(/<li(.*?)>(.*?)<\/li>/gi, "[*]$2");
317 + html = html.replace(/<ul(.*?)>/gi, "[list]");
318 + html = html.replace(/<\/ul>/gi, "[/list]");
319 + html = html.replace(/<div>/gi, "\n");
320 + html = html.replace(/<\/div>/gi, "\n");
321 + html = html.replace(/<td(.*?)>/gi, " ");
322 + html = html.replace(/<tr(.*?)>/gi, "\n");
323 +
324 + html = html.replace(/<img(.*?)src="(.*?)"(.*?)>/gi, "[img]$2[/img]");
325 + html = html.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, "[url=$2]$4[/url]");
326 +
327 + html = html.replace(/<head>(.*?)<\/head>/gmi, "");
328 + html = html.replace(/<object>(.*?)<\/object>/gmi, "");
329 + html = html.replace(/<script(.*?)>(.*?)<\/script>/gmi, "");
330 + html = html.replace(/<style(.*?)>(.*?)<\/style>/gmi, "");
331 + html = html.replace(/<title>(.*?)<\/title>/gmi, "");
332 + html = html.replace(/<!--(.*?)-->/gmi, "\n");
333 +
334 + html = html.replace(/\/\//gi, "/");
335 + html = html.replace(/http:\//gi, "http://");
336 +
337 + html = html.replace(/<(?:[^>'"]*|(['"]).*?\1)*>/gmi, "");
338 + html = html.replace(/\r\r/gi, "");
339 + html = html.replace(/\[img]\//gi, "[img]");
340 + html = html.replace(/\[url=\//gi, "[url=");
341 +
342 + html = html.replace(/(\S)\n/gi, "$1 ");
343 +
344 + return html;
345 345 }
346 346
347 347 function descriptionHandler()

Alex revised this gist 3 years ago · 744fbf0

1 file changed, 74 insertions, 1 deletion

nyaa-shit.user.js
@@ -3,12 +3,21 @@
3 3 // @namespace github.com/Decicus
4 4 // @match https://nyaa.si/*
5 5 // @grant GM_setClipboard
6 - // @version 1.2.0
6 + // @version 1.3.0
7 7 // @author Decicus
8 8 // @description Adds some extra functionality to Nyaa.
9 9 // ==/UserScript==
10 10
11 11 let copyAmount = 5;
12 + const multipliers = {
13 + Bytes: 1,
14 + KiB: 1024,
15 + MiB: 1024 * 1024,
16 + GiB: 1024 * 1024 * 1024,
17 + TiB: 1024 * 1024 * 1024 * 1024,
18 + };
19 +
20 + const sizesRegex = /(Bytes|KiB|MiB|GiB|TiB)/g;
12 21
13 22 function getElements(linkType = 'download')
14 23 {
@@ -28,6 +37,69 @@ function getMagnets()
28 37 return getElements('magnet');
29 38 }
30 39
40 + /**
41 + * Calculate the approximate total size of all torrents listed in the table and append it as an information row at the bottom of the table
42 + */
43 + function calculateTotalSize()
44 + {
45 + const timestamps = document.querySelectorAll('.torrent-list td[data-timestamp]');
46 + let totalBytes = 0;
47 + for (const timestamp of timestamps)
48 + {
49 + const sizeCell = timestamp.previousElementSibling;
50 + const size = sizeCell.textContent.trim();
51 + let suffix = size.match(sizesRegex);
52 + if (!suffix || suffix.length < 1) {
53 + console.error('Could not find size in element', sizeCell);
54 + continue;
55 + }
56 +
57 + suffix = suffix[0];
58 + const multiplier = multipliers[suffix];
59 + const sizeNumber = parseFloat(size.replace(` ${suffix}`, '').trim());
60 + const torrentBytes = sizeNumber * multiplier;
61 +
62 + totalBytes += torrentBytes;
63 + console.log(totalBytes);
64 + }
65 +
66 + const multiplierKeys = Object.keys(multipliers).reverse();
67 + let totalSize = `${totalBytes} Bytes`;
68 + for (const key of multiplierKeys)
69 + {
70 + if (totalBytes === 0) {
71 + break;
72 + }
73 +
74 + const divideBy = multipliers[key];
75 + const result = totalBytes / divideBy;
76 + if (result > 1) {
77 + totalSize = `${result.toFixed(2)} ${key}`;
78 + break;
79 + }
80 + }
81 +
82 + const torrentList = document.querySelector('.torrent-list tbody');
83 + const html = `<tr class="default" id="all-torrents-size">
84 + <td class="text-center">
85 + <a href="#all-torrents-size" title="Total size">
86 + 💽
87 + </a>
88 + </td>
89 + <td colspan="2">
90 + <a href="#all-torrents-size">Total size</a>
91 + </td>
92 + <td class="text-center"><!-- Download --></td>
93 + <td class="text-center">${totalSize}</td>
94 + <td class="text-center"><!-- Timestamp --></td>
95 + <td class="text-center"><!-- Seeders --></td>
96 + <td class="text-center"><!-- Leechers --></td>
97 + <td class="text-center"><!-- Completed --></td>
98 + </tr>`;
99 +
100 + torrentList.insertAdjacentHTML('beforeend', html);
101 + }
102 +
31 103 /**
32 104 * Handler for copying download links (torrents)
33 105 */
@@ -458,6 +530,7 @@ function init() {
458 530 titleHandler();
459 531 descriptionHandler();
460 532 torrentListViewHandler();
533 + calculateTotalSize();
461 534 }
462 535
463 536 init();

Alex revised this gist 3 years ago · 5942866

1 file changed, 463 insertions

nyaa-shit.user.js (file created)

Diff is too large to be shown