www.xbdev.net
xbdev - software development
Thursday January 2, 2025
Home | Contact | Support | JavaScript... so much power in such a few lines of code..
     
 

JavaScript...

so much power in such a few lines of code..

 



Download All The Images From A Website Using Javascript (NOT One At A Time - Single Big File)



Download All The Images From A Website Using Javascript (All NOT One At A Time)
Download All The Images From A Website Using Javascript (All NOT One At A Time)


This is short script that you can run on any page so you can download all images and/or additional information. Instead of trying to download images one at a time and save them (you can have a script popup and show you a download dialog - but you want all the images put together).

You want a single download with everything! How to do this? Just put all the images in a JSON file - download the JSON file then you can do what you want! Store the images in the JSON file as URL encoded strings.

In fact, you might think the file size is crazy large - but it isn't that bad!


For this example, I was grabbing all the 'h4' elements and getting the information aournd that element.

let hh document.getElementsByTagName('h4');

let jsondata = [];

for (
let g=0g<hh.lengthg++)
{
    
console.log('g:');
    
let tit    hh[g].innerHTML;
    
let p      hh[g].parentElement.children[1].innerHTML;
    
let imgurl hh[g].parentElement.parentElement.children[0].children[0].src;
    
let img    hh[g].parentElement.parentElement.children[0].children[0];
    
    
// If we want to reload/check the img url
    //const imgurl = imgurls[i];
    //const img = document.createElement("img");
    //img.crossOrigin="anonymous"
    //img.src = imgurl;
    //await img.decode(); // wait for it to finish loading

    
console.log('img.naturalHeight:'img.naturalHeight );
    
    
let canvas document.createElement('canvas');
    
let context canvas.getContext('2d');
    
canvas.height img.naturalHeight;
    
canvas.width  img.naturalWidth;
    
context.drawImage(img00);
    
let imgdata canvas.toDataURL("image/jpeg"1.0); // alt jpeg or png

    // we could use the original name
    //let imgfilename = imgurl.split('/').pop();
    
    
let imgfilename = `img${g}.jpg`; // imgurl.split('/').pop();

    
console.log('processed image:',  imgfilename);   

   
// You're generating a data uri, which has schema, mime-type etc before the actual base64 data data:[<MIME-type>][;charset=<encoding>][;base64],<data>. You'll have to remove all the content before the data then use it.

   
ii = { 'imgdata'imgdata,
          
'title':   tit
          
'desc':    };
   
   
jsondata.pushii );
}


let jsonfile JSON.stringify(jsondata);

console.log('creating single jsonfile..');

console.log('adding link element');
var 
document.createElement("a");
document.body.appendChild);
a.id              'downloadlink';
a.style.position  'absolute';
a.style.bottom    '100px';
a.style.left      '100px';
a.style.fontSize  '100pt';
a.innerHTML 'Click to Download';
 
console.log('building blob');
var 
file             = new Blob([ jsonfile ], {type'text/plain'});
a.href               URL.createObjectURLfile );
a.download           'images.json';
a.style['font-size'] = '20pt';

a.click();

console.log('finished');



Processing the Download JSON Data


After you've downloaded the JSON file - you can either process it and convert it to a zip or load it and put the content on a website.

var script document.createElement('script');
script.type 'text/javascript';
script.src 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.6.0/jszip.min.js';
document.head.appendChild(script); 

let tt '';

(
async()=>{

let b document.createElement('button');
b.innerHTML 'load json';
document.body.appendChild);
b.onclick = ()=>{
  
let input document.createElement('input');
  
input.type 'file';
  
input.onchange => {
      
// you can use this method to get file and perform respective operations
      
let files =   Array.from(input.files);
      
console.log(files);

      
// get the first file (file[0]
      
let file files[0];
      var 
reader = new FileReader();
      
reader.readAsText(file"UTF-8");
      
reader.onload async function (evt) {
         
console.log('loaded');
         
let div document.createElement('div');
         
document.body.appendChilddiv );
         
div.innerHTML 'filesize:' evt.target.result.length;

         
let json JSON.parseevt.target.result );
         
console.log('num data:'json.length );

         
let myzip = new JSZip();

         for (
let k=0k<json.lengthk++)
         {
            
let title json[k].title;
            
let desc  json[k].desc;
            
let imgbase64 json[k].imgdata;
            
// console.log( json[k].title );
            
let imgfilename = `thumb${k}.jpg`

            
myzip.file(imgfilenameimgbase64 .substr(imgbase64 .indexOf(',')+1), {base64true});
            
myzip.file(`h${k}.txt`, title);
            
myzip.file(`d${k}.txt`, desc);

            
tt += `${title}\n\n`;
            
tt += `${desc}\n`;
            
tt += `<img src='./thumb${k}.jpg>\n`;
         }

         
myzip.file(`summary.txt`, tt);


         
console.log('creating zip..');
         
let content await myzip.generateAsync({type"blob"});
         
         
console.log('adding link element');
         var 
document.createElement("a");
         
document.body.appendChild);
         
a.id 'downloadlink';
         
a.style.position 'absolute';
         
a.style.top'20px';
         
a.style.left  '20px';
         
a.style.fontSize  '20pt';
         
a.innerHTML 'Click to Download';
 
         
console.log('building blob');
         var 
file = new Blob([ content ], {type'octet/stream'});
         
a.href URL.createObjectURLfile );
         
a.download 'images.zip';
         
a.style['font-size'] = '20pt';
         
         
a.click();
         
         
console.log('finished');
      }
      
reader.onerror = function (evt) {
         
let div document.createElement('div');
         
document.body.appendChilddiv );
         
div .innerHTML "error reading file";
      }
  };
  
input.click();
}
})()

































 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.