Advertisement
lord_ne

ComikeyDownloader.js

Nov 26th, 2021 (edited)
1,016
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // How to use:
  2. // 1) Go to a chapter page on Comikey.
  3. // 2) Open the console (generally if you open "inspect element"
  4. //    the browser console will be another tab of that).
  5. // 3) Paste this entire file into the console, and hit enter
  6. // 4) Type run() into the console, and hit enter.
  7. // 5) Navigate to another page of the chapter using the left
  8. //    and right arrow keys or the slider bar. You should see
  9. //    an alert telling you which page to go to.
  10. // 6) Repeat (4) and (5) until you see an alert which says
  11. //    "Downloaded all pages".
  12. // 7) Use 7zip or a similar program to combine all of the
  13. //    downloaded page image files into one zip file, then
  14. //    rename the zip file to have the file extension ".cbz"
  15.  
  16. // Update 2021-12-19: Fixed script to work on chapters that
  17. // use img tags instead of canvas tags (this was previously
  18. // an issue with webtoon-style continuous vertical chapters)
  19.  
  20. var make_filename = function(page_name){
  21.   return page_name.match(/\d/g).join('').padStart(4, "0");
  22. }
  23.  
  24. var img_to_canvas = function(img){
  25.   var c = document.createElement("canvas");
  26.   c.width = img.getAttribute("width");
  27.   c.height = img.getAttribute("height");
  28.   var context = c.getContext('2d');
  29.   context.drawImage(img, 0, 0);
  30.   if (!context.getImageData(0, 0, 1, 1).data[3]) {
  31.     return null;
  32.   }
  33.   return c;
  34. }
  35.  
  36. var download = function(page){
  37.   page_name = page.getAttribute("aria-label");
  38.   var link = document.createElement('a');
  39.   link.download = make_filename(page_name) + ".png";
  40.   if (page.nodeName == "IMG") {
  41.     page = img_to_canvas(page);
  42.     if (!page) {
  43.       return false;
  44.     }
  45.   }
  46.   link.href = page.toDataURL("image/png");
  47.   link.click();
  48.   return true;
  49. }
  50.  
  51. var grab_first = function(iterable) {
  52.   return iterable.values().next().value;
  53. }
  54.  
  55. var is_ready_page = function(page) {
  56.   if (page.nodeName == "CANVAS") {
  57.     return (page.getAttribute("width") && page.getAttribute("height"));
  58.   } else if (page.nodeName == "IMG") {
  59.     return (page.getAttribute("src") && page.getAttribute("src").startsWith("blob:"));
  60.   } else {
  61.     return false;
  62.   }
  63. }
  64.  
  65. if (typeof my_pages === 'undefined' || my_pages === null) {
  66.   var my_pages = new Set(document.querySelectorAll("img.page-img, canvas.page-img"));
  67.   if (my_pages.size === 0) {
  68.     alert("No pages found")
  69.   }
  70. }
  71.  
  72. var run = function(){
  73.   if (my_pages.size === 0) {
  74.     alert("You have already downloaded all pages");
  75.     return;
  76.   }
  77.  
  78.   new_pages = new Set(my_pages)
  79.   for (page of my_pages) {
  80.     if  (is_ready_page(page)) {
  81.       var rtn = download(page);
  82.       if (rtn) {
  83.         new_pages.delete(page);  
  84.       }
  85.     }
  86.   }
  87.   my_pages = new_pages
  88.  
  89.   if (my_pages.size === 0) {
  90.     alert("Downloaded all pages")
  91.   } else {
  92.     alert("Remaining pages: " + my_pages.size + " pages, including: " + grab_first(my_pages).getAttribute("aria-label"))
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement