Advertisement
zetlnd

Untitled

Aug 17th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. const script=document.createElement('script');script.src='https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js';document.head.appendChild(script);script.onload=()=>{console.log('JSZip loaded successfully.');const preElement=document.querySelector('pre');const urls=preElement.textContent.split('\n').filter(Boolean);const zip=new JSZip();const extractFilenameFromUrl=url=>url.substring(url.lastIndexOf('/')+1);const downloadFile=async(url,index)=>{try{const originalFilename=extractFilenameFromUrl(url);const newFilename=`image ${index+1} - ${originalFilename}`;console.log(`Downloading ${newFilename} from ${url}...`);const response=await fetch(url);if(!response.ok)throw new Error(`Failed to fetch ${url}`);const blob=await response.blob();zip.file(newFilename,blob);console.log(`${newFilename} downloaded and added to ZIP.`);}catch(error){console.error(`Error downloading ${url}: ${error}`);}};const delay=ms=>new Promise(resolve=>setTimeout(resolve,ms));const updateProgressBar=(current,total)=>{const percent=Math.floor((current/total)*100);const progressBar='='.repeat(percent/2)+'>'.padEnd(50-(percent/2),' ');console.log(`Progress: [${progressBar}] ${percent}% (${current}/${total})`);};const downloadAndZipAllFiles=async()=>{console.log('Starting download of files...');for(let i=0;i<urls.length;i++){const url=urls[i];await downloadFile(url,i);await delay(1);updateProgressBar(i+1,urls.length);}try{console.log('Creating ZIP archive...');const zipBlob=await zip.generateAsync({type:"blob"});const a=document.createElement('a');a.href=URL.createObjectURL(zipBlob);a.download='images.zip';document.body.appendChild(a);a.click();URL.revokeObjectURL(a.href);document.body.removeChild(a);console.log('ZIP archive created and download initiated.');console.log('All files have been successfully downloaded and saved as a ZIP archive.');}catch(error){console.error(`Error creating ZIP: ${error}`);}};downloadAndZipAllFiles();};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement