Download multiple files on a script of JS

Viewed 57

I'm trying to download a M3U8 resource inserting a script of JS in the web browser.

I defined a function to trigger the download using this method (Auto download file from a link using javascript):

function downloadResource(filename, url) {
  tag = document.createElement("a");
  document.body.appendChild(tag);
  tag.href = url;
  tag.download = filename;
  tag.click();
};

And then I'm interpreting the file where I can get all the parts of the resource and calling downloadResource on each one:

downloadResource("manifest.m3u8", playlistURL);
fetch(playlistURL)
  .then(response => response.blob())
  .then(response => response.text())
  .then(response => fetch(response.match(/https.*720.*/)[0])
                      .then(response => response.blob())
                      .then(response => response.text())
                      .then(response => {
                              videoUrls = response.match(/https[^"\n]*(?:"|\n)/g);
                              downloadResource("key.bin", videoUrls[0]);
                              videoUrls = videoUrls.splice(1);
                              for (var i = 0; i < videoUrls.length; i++) {
                                downloadResource("videoPart." + i + ".ts", videoUrls[i]);
                              }
                            }));

But this is only downloads "manifest.m3u8" and the last videoPart.i.ts from the download resource. I checked that all a tags were added to the DOM, and they were all added correctly.

Is this a problem with click() or something like that, or how can I download multiple files?

0 Answers
Related