MyHentaiComics Downloader

Downloads all the pages of a specific comic

  1. // ==UserScript==
  2. // @name MyHentaiComics Downloader
  3. // @namespace ElectricHum/H/
  4. // @version 0.2.1
  5. // @description Downloads all the pages of a specific comic
  6. // @author ElectricHum
  7. // @match http://myhentaicomics.com/index.php/*
  8. // @exclude http://myhentaicomics.com/index.php/*/*
  9. // @grant none
  10. // @require http://code.jquery.com/jquery-1.12.0.min.js
  11. // @compatible chrome Must allow multiple downloads
  12. // ==/UserScript==
  13.  
  14. /*
  15. Changelog:
  16. 0.2:
  17. + Delay (adjustable) after each download
  18. + Stop button
  19. ~ Visual: Added a slight tint background tint
  20. ~ Button now toggles debugging as previously only enabled it
  21. ~ Technical: replaced all 'this' with 'myDL'
  22. 0.1:
  23. ! First working version
  24. + Changelog
  25. + Download button
  26. + Debugging mode & button
  27. */
  28.  
  29. /* Allows for functions created here to still be used after script finishes e.g. when button is pressed */
  30. var myDL = window.myDL = {};
  31.  
  32. /* When debug is set to one, more useful text -for debugging- will appear */
  33. myDL.debug = 0;
  34.  
  35. /* The page which is currently being downloaded */
  36. myDL.curDL = 0;
  37.  
  38. myDL.getPagesNumber = function()
  39. {
  40. // Returns the amount of pages the comic has
  41. var pgNumber = parseInt($("#g-content .g-info").text().split(" of ")[1]);
  42. if (myDL.debug)
  43. console.log("[DEBUG]> Pages=" + pgNumber);
  44. return pgNumber;
  45. };
  46.  
  47. myDL.getFirst = function()
  48. {
  49. // Returns the starting page
  50. var thumbnailsrc = $(".g-thumbnail:first").attr("src").split("/");
  51. thumbnailsrc = thumbnailsrc[thumbnailsrc.length - 1];
  52. thumbnailsrc = thumbnailsrc.split("/")[0].split(".");
  53. var start = parseInt(thumbnailsrc[0]);
  54. if (myDL.debug)
  55. console.log("[DEBUG]> Start = " + start);
  56. return start;
  57. };
  58.  
  59. myDL.getFormat = function()
  60. {
  61. // Returns the extension of the pictures
  62. var thumbnailsrc = $(".g-thumbnail:first").attr("src").split("/");
  63. thumbnailsrc = thumbnailsrc[thumbnailsrc.length - 1];
  64. thumbnailsrc = thumbnailsrc.split("/");
  65. var format = thumbnailsrc[thumbnailsrc.length -1].split(".")[1];
  66. if (myDL.debug)
  67. console.log("[DEBUG]> Extension="+format);
  68. return format;
  69. };
  70.  
  71. myDL.getTitle = function()
  72. {
  73. // Returns the Title of the comic
  74. var title = $("#g-header .g-active").text().trim();
  75. if (myDL.debug)
  76. console.log("[DEBUG]> Title="+title);
  77. return title;
  78. };
  79.  
  80. myDL.downloadImg = function(url)
  81. {
  82. // Downloads image from url
  83. if (myDL.debug)
  84. console.log("Downloaded: " + url);
  85. $("body").append('<a class="myDL_tmp" style="display:none;" download href="' + url + '">Temp</a>');
  86. var anchor = document.getElementsByClassName("myDL_tmp")[0];
  87. anchor.click();
  88. anchor.remove();
  89. };
  90.  
  91. myDL.DownloadAllImgs = function()
  92. {
  93. // Gets delay from input, if invalid defaults to 1
  94. myDL.delay = parseFloat($("#myDL_box input").val());
  95. if(isNaN(myDL.delay))
  96. myDL.delay = 1;
  97. if(myDL.debug)
  98. console.log("[DEBUG]> Delay=" + myDL.delay);
  99. myDL.first = myDL.getFirst();
  100. myDL.last = myDL.getPagesNumber();
  101. /* The domain name + the title of the comic */
  102. myDL.baseUrl = "http://myhentaicomics.com/var/resizes/" + encodeURIComponent(myDL.getTitle()) + "/";
  103. myDL.stopDownloading();
  104. myDL.Interval = setInterval(myDL.getImage, myDL.delay * 1000);
  105. };
  106.  
  107. myDL.getImage = function()
  108. {
  109. // Check if should download
  110. if (myDL.curDL >= myDL.last)
  111. {
  112. myDL.stopDownloading();
  113. console.log("Finished Downloading");
  114. }
  115. else
  116. {
  117. var fileNumber = myDL.curDL + myDL.first;
  118. if (myDL.debug)
  119. console.log("Downloading "+ String(fileNumber));
  120. // Adds the starting 0's the site uses as a file structure before the number and extension
  121. var dlUrl = myDL.baseUrl;
  122. if(fileNumber < 10)
  123. dlUrl += "00";
  124. else if(fileNumber < 100)
  125. dlUrl += "0";
  126. dlUrl += String(fileNumber) + "." + myDL.getFormat();
  127. myDL.downloadImg(dlUrl);
  128. myDL.curDL++;
  129. }
  130. };
  131.  
  132. myDL.stopDownloading = function()
  133. {
  134. // Stops current (if any) downloading
  135. if(typeof myDL.Interval == "undefined")
  136. {
  137. if(myDL.debug)
  138. console.log("Not currently downloading");
  139. }
  140. else
  141. {
  142. // Stops the Timer
  143. window.clearInterval(myDL.Interval);
  144. if(myDL.debug)
  145. console.log("Stopped downloading");
  146. }
  147. //Resets counter
  148. myDL.curDL = 0;
  149. };
  150.  
  151. myDL.toggleDebug = function()
  152. {
  153. // Enabled console messages which MAY or may not help with debugging
  154. myDL.debug = !myDL.debug;
  155. if(myDL.debug)
  156. console.log("[DEBUG]> Enabled");
  157. else
  158. console.log("[DEBUG]> Disabled");
  159. };
  160.  
  161. myDL.init = function()
  162. {
  163. // Creates container for buttons
  164. $("#g-header").after('<div id="myDL_box" style="background-color:rgba(32,29,49,0.3);"></div>');
  165. // Creates a button to initialise the downloading
  166. $("#myDL_box").append('<button onclick="myDL.DownloadAllImgs()">Download all</button>');
  167. // Creates a button to stop downloading
  168. $("#myDL_box").append('<button style="margin:10px" onclick="myDL.stopDownloading()">Stop</button>');
  169. // Drop down menu to select delay after each download
  170. $("#myDL_box").append('Delay (seconds):<input style="display:inline;width:30px" type="text" value="1">');
  171. // Button that toggles debugging
  172. $("#myDL_box").append('<span style="color:white;padding:1px;float:right;background-color:#878787" onclick="myDL.toggleDebug()">Toggle Debug</span><br>');
  173. };
  174.  
  175. $("document").ready(function () {
  176. // When the page loads, the magic happens
  177. myDL.init();
  178. });