nyaa.si - Batch Download

Allows batch download of all displayed results in one single click.

  1. // ==UserScript==
  2. // @name nyaa.si - Batch Download
  3. // @description Allows batch download of all displayed results in one single click.
  4. // @version 2023.03.16.23.02
  5. // @author MetalTxus
  6. // @namespace https://github.com/jesuscc1993
  7.  
  8. // @grant GM_xmlhttpRequest
  9.  
  10. // @icon https://avatars3.githubusercontent.com/u/28658394?s=44
  11. // @match https://nyaa.si/*
  12. // @require https://code.jquery.com/jquery-3.2.1.min.js
  13. // ==/UserScript==
  14.  
  15. /* globals jQuery */
  16.  
  17. (() => {
  18. 'use strict';
  19.  
  20. const delayBetweenDownloads = 150;
  21.  
  22. let downloadButton;
  23. let magnets;
  24.  
  25. const appendBatchDownloadButton = () => {
  26. magnets = jQuery(magnetsSelector);
  27.  
  28. const fileCount = magnets.length;
  29. if (fileCount) {
  30. downloadButton = jQuery(`
  31. <a title="Download all" href class="mt-batch-download">
  32. <i class="fa fa-fw fa-magnet"></i>
  33. <span class="mt-batch-download-label">Download all (${fileCount})</span>
  34. <i class="fa fa-fw fa-magnet"></i>
  35. </a>
  36. `);
  37. downloadButton.click((event) => {
  38. event.preventDefault();
  39. downloadAll();
  40. });
  41. setButtonText();
  42.  
  43. jQuery('.torrent-list').append(downloadButton);
  44.  
  45. downloadButton
  46. .wrap(`<td colspan="9" align="center">`)
  47. .wrap(`<tr style="background: none;">`);
  48. }
  49. };
  50.  
  51. const setButtonText = () => {
  52. downloadButton
  53. .find(buttonLabelSelector)
  54. .text(`Download all (${magnets.length})`);
  55. };
  56.  
  57. const downloadAll = () => {
  58. downloadNext(magnets.toArray());
  59. };
  60.  
  61. const downloadNext = (anchors) => {
  62. const anchor = anchors.pop();
  63. const url = anchor.href.split('&dn=')[0];
  64. const magnetTab = window.open(url);
  65. setTimeout(() => magnetTab.close(), delayBetweenDownloads);
  66.  
  67. if (anchors.length) {
  68. setTimeout(() => downloadNext(anchors), delayBetweenDownloads);
  69. }
  70. };
  71.  
  72. const onMutation = () => {
  73. magnets = jQuery(magnetsSelector);
  74.  
  75. setButtonText();
  76. };
  77.  
  78. const initialize = () => {
  79. appendBatchDownloadButton();
  80.  
  81. window.onload = () => {
  82. new MutationObserver(onMutation).observe(
  83. document.querySelector('tbody'),
  84. { childList: true }
  85. );
  86. };
  87. };
  88.  
  89. const magnetsSelector = 'a[href*="magnet:"]';
  90. const buttonLabelSelector = '.mt-batch-download-label';
  91.  
  92. initialize();
  93. })();