AnimeTosho Eng Subtitle Downloader

Automatically downloads English subtitles on AnimeTosho

  1. // ==UserScript==
  2. // @author nht.ctn
  3. // @name AnimeTosho Eng Subtitle Downloader
  4. // @namespace https://github.com/nhtctn
  5. // @version 1.0
  6. // @description Automatically downloads English subtitles on AnimeTosho
  7. // @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAYdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuOWwzfk4AAADrSURBVDhPtVKxDcJADLSERMEqIEU0tEyARGo6qOjCGlRhEhIxACOgtIxARUVDccTmnbw/nyJCnPRS4rfPd/bT34B7hYRmoFHSnnGG0wNwKXHgXSGjJWhSoHza5IZ0c4uTaMLh3N9FG5icCi/4F4o8JWvBqcK1BK0KcJ0QSPYlFWkCRzY/tl3k3hWx0inlrcVvwV4GxCh2defAJ8eUkPO3hsCTFPPIpGxHY10FTr58M1mwLi5Y0LqJdWcQEgQrDGO+HYEhCFap//6AjX9G6EkIdXW11HCd5g0oYpMfBJ1+7AkPgpGv51fSfhB9ADUGVK7f/SFaAAAAAElFTkSuQmCC
  8.  
  9. // @match *://animetosho.org/search?q=*
  10. // @match *://animetosho.org/view/*
  11. // @match *://animetosho.org/series/*
  12.  
  13. // @grant GM_getValue
  14. // @grant GM_setValue
  15.  
  16. // @run-at document-idle
  17. // ==/UserScript==
  18.  
  19. (function() {
  20. 'use strict';
  21.  
  22. const tabCloseTime = 6000; // 6 seconds
  23.  
  24. var pageUrl = window.location.href;
  25. var a;
  26.  
  27. if (GM_getValue("autoDown") == null) {
  28. GM_setValue("autoDown", true);
  29. }
  30.  
  31. var autoCheckButton = '<div id="autoSubDownload_container" style="font-size: smaller; margin-top: 1em; text-align: center;"><input id="autoSubDownload" type="checkbox" style="margin: 0; vertical-align: bottom;"> Auto Subtitle Download</div>';
  32. var checkArea = document.getElementById("topbar_time");
  33. if (checkArea != null) {
  34. checkArea.insertAdjacentHTML( "afterend", autoCheckButton );
  35. var checkBox = document.getElementById("autoSubDownload");
  36. checkBox.onclick = function() {autoDownloadCheck();};
  37. GM_getValue("autoDown") == true ? checkBox.checked = true : '';
  38. }
  39.  
  40. function autoDownloadCheck() {
  41. if (checkBox.checked == true){
  42. GM_setValue("autoDown", true);
  43. } else {
  44. GM_setValue("autoDown", false);
  45. }
  46. }
  47.  
  48. // OPEN TORRENT PAGES
  49. if (pageUrl.search( /animetosho\.org\/search\?q=/ ) >= 0 && GM_getValue("autoDown") == true)
  50. {
  51. var torrentLinks = [];
  52. var torrents = document.querySelectorAll( 'div[class="link"] > a' );
  53. for ( a = 0; a < torrents.length; a++ ) {torrentLinks[a] = torrents[a].href;}
  54.  
  55. var button = '<button id="engSubDown" class="feeddd" style="margin-right: 10px;">Download All Eng Subtitles</button>';
  56. var butonArea = document.querySelector( '.feeddd' );
  57. butonArea.insertAdjacentHTML( "afterend", button );
  58. var myButton = document.getElementById("engSubDown");
  59. myButton.onclick = function() {openAll();};
  60. }
  61.  
  62. function openAll( )
  63. {
  64. if(GM_getValue("autoDown") == true) {
  65. for ( var a = 0; a < torrentLinks.length; a++ ) {window.open(torrentLinks[a]);}
  66. }
  67. }
  68.  
  69. // DOWNLOAD ENG SUBS
  70. if (pageUrl.search( /animetosho\.org\/view\// ) >= 0 ) {
  71. var errorCheck = document.querySelector('body > center > h1') != null;
  72. if (errorCheck) {
  73. setTimeout(function() {location.reload();}, 5000);
  74. }
  75. else {
  76. if(GM_getValue("autoDown") == true) {
  77.  
  78. var subButtons = document.querySelectorAll( '[href*="animetosho.org/storage/attach/"]' );
  79. var engSubLinks = [];
  80. for ( a = 0; a < subButtons.length; a++ )
  81. {
  82. var subsLangs = subButtons[a].textContent;
  83. if (subsLangs.search(/eng/i) >= 0)
  84. {
  85. engSubLinks[a] = subButtons[a].href;
  86. }
  87. }
  88. // If there is one eng sub, download. Else wait.
  89. if (engSubLinks.length == 1)
  90. {
  91. document.location = engSubLinks[0];
  92. setTimeout(function() {
  93. if (GM_getValue("autoDown") == true) {
  94. window.close();
  95. }
  96. }, tabCloseTime);
  97. }
  98. // If there is no eng sub but one undefined sub, download.
  99. else if (subButtons.length == 1)
  100. {
  101. if (subButtons[0].textContent.search(/und/i) >= 0)
  102. {
  103. var engSubLink = subButtons[0].href;
  104. }
  105. document.location = engSubLink;
  106. setTimeout(function() {
  107. if (GM_getValue("autoDown") == true) {
  108. window.close();
  109. }
  110. }, tabCloseTime);
  111. }
  112. console.log(subButtons);
  113. }
  114. }
  115. }
  116.  
  117. })();