Advertisement
HeyHoLetsGo615

Untitled

Feb 20th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (document.head.innerHTML.indexOf("https://cdn.jsdelivr.net/npm/filesaver.js@1.3.4/FileSaver.min.js") == -1) {
  2.     imported = document.createElement('script');
  3.     imported.src = 'https://cdn.jsdelivr.net/npm/filesaver.js@1.3.4/FileSaver.min.js';
  4.     document.head.appendChild(imported);
  5. }
  6. if (document.head.innerHTML.indexOf("https://cdn.jsdelivr.net/npm/jszip@3.2.2/dist/jszip.js") == -1) {
  7.     imported = document.createElement('script');
  8.     imported.src = 'https://cdn.jsdelivr.net/npm/jszip@3.2.2/dist/jszip.js';
  9.     document.head.appendChild(imported);
  10. }
  11.  
  12. /*function used to create canvases for each image*/
  13. function canvasChecker(cntr) {
  14.   var imported = document.createElement('canvas');
  15.   imported.className = "blobCanvas";
  16.   imported.id = "blobCanvas" + cntr;
  17.   imported.innerHTML = "";
  18.   document.body.appendChild(imported);
  19. }
  20.  
  21. var imgs = document.getElementsByTagName("img");
  22. var i = 0;
  23.  
  24. /*Declare JSZip and make a folder within the zip file called "images"*/
  25. var zip = new JSZip();
  26. var imgZip = zip.folder("images");
  27.  
  28. while (i < imgs.length) {
  29.  
  30.   /*this if statement is for a particular website to filter the images to what I'm looking for. feel free to remove it*/
  31.   if (imgs[i].src.indexOf("300x300") > -1){
  32.  
  33.       var fullImg = imgs[i];
  34.      
  35.       /*Create the next canvas for the image*/
  36.       canvasChecker(i);
  37.  
  38.      /*Get the current canvas and match the size to the image*/
  39.       var canvas = document.getElementById("blobCanvas" + i);
  40.       canvas.width = imgs[i].width;
  41.       canvas.height = imgs[i].height;
  42.       var context = canvas.getContext("2d");
  43.  
  44.       /*Draw the image to the canvas*/
  45.       try{
  46.         context.drawImage(fullImg,0,0,fullImg.width,fullImg.height);
  47.       }
  48.       catch(err){}
  49.   }
  50.   i=i+1;
  51. }
  52.  
  53. i=0;
  54.  
  55. /*Get all the newly generated canvases with the images*/
  56. var canvases = document.getElementsByClassName("blobCanvas");
  57.  
  58. while(canvases[0]) {
  59.     try{
  60.         i=i+1;
  61.  
  62.         /*This is where the problem lies. This line will not add the canvas blob to the file*/
  63.         canvases[0].toBlob(function(blob) {imgZip.file(i + ".png",blob);});
  64.  
  65.         console.log("built");
  66.     }
  67.     catch(err){console.log("meh...");}
  68.    
  69.     /*Remove the canvas elements once they're added to the file*/
  70.     canvases[0].parentNode.removeChild(canvases[0]);
  71. }
  72.  
  73. /*Save the zip file*/
  74. zip.generateAsync({type: "blob"})
  75.             .then(function(blob) {saveAs(blob, "SavedImages.zip");});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement