downloading array of urls in chrome extension

i'm creating a chrome extension. i have an array of urls and i want to download them all one by one, one at a time (meaning the second download will start only after the first one finished etc...)

i've tried this: ("links" is the array)

function doDownloads(links, iterator, max) {
	chrome.downloads.download({
		url: links[iterator],
		saveAs: false
	}, function(downloadId) {
		if (iterator < max)
			chrome.downloads.onChanged.addListener(function goToNextDL(delta) {
				if (delta.id == downloadId)  {
					chrome.downloads.onChanged.removeListener(goToNextDL);
					if (delta.state && delta.state.current  === 'complete' && iterator + 1 < max)
						doDownloads(links, iterator + 1, max);
				}	
			});
	});
}

but it doesn't work ):

any ideas?

2 answers

  • answered 2018-01-14 01:06 user4723924

    This should work...

    var Dest=''; 
    
    for(var i=0; i<links.length; i++){
     Dest='image'+i+'.jpg';
     chrome.downloads.download({url:links[i] ,filename:Dest, conflictAction:'overwrite'});
    
    }
    

    NB: I've added simple logic for changing the destination file name.

  • answered 2018-01-14 02:01 user4723924

    How's this method?

    var Counter=0; // global var
    
    
    
    
    function DownloadURL(URL){
    
    var DL=new XMLHttpRequest();
    
    DL.onload=function(evt){var arraybuffer=DL.response;};
    
    DL.addEventListener('load', function(evt){ Counter++; if(Counter<Links.length){DownloadURL(Links[Counter]);} });  // increment to next file
    
    DL.open('GET',URL,true);
    
    DL.responseType='arraybuffer';
    
    DL.send();
    
    }
    

    Call it once to get the ball rolling...

    DownloadURL(Links[Counter]);