Download All The Images From A Website Using Javascript (NOT One At A Time - Single Big File)
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=0; g<hh.length; g++) { console.log('g:', 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
let canvas = document.createElement('canvas'); let context = canvas.getContext('2d'); canvas.height = img.naturalHeight; canvas.width = img.naturalWidth; context.drawImage(img, 0, 0); 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': p };
jsondata.push( ii ); }
let jsonfile = JSON.stringify(jsondata);
console.log('creating single jsonfile..');
console.log('adding link element'); var a = document.createElement("a"); document.body.appendChild( a ); a.id = 'downloadlink'; a.style.position = 'absolute'; a.style.bottom = '100px'; a.style.left = '100px'; a.style.fontSize = '100pt'; a.innerHTML = 'Click to Download';