Bulk download of images using JavaScript

I need a piece of JavaScript code that gets an array of image links and downloads them one by one. I know there are tools for this, but due to some access limitations it needs to be JavaScript code that runs on a page in my browser. I get to write the whole page myself, and browse it as a local file on my computer.

As my input I will have a JavaScript array:

var links = ['https://.../img1.jpg','https://.../img2.jpg','https://.../img3.jpg',...]

I will be using a fairly recent build of Firefox. I have figured out that I can get rid of the Save As dialog if I set the browser to always save to a certain location, and I can get rid of the confirmation dialog by checking “Do this for all files of this type”. I can do the foreach myself, I just need the function I can feed the links to.

You’d think there would be many examples how to do this, and there are, but so far I haven’t found a single one that actually works for me. Either they assume you have a static image on your page and want to initiate download by clicking on it, or they run smack into CORS policy error. I even found one complete example that seemed to be working, but once I downloaded and tried it on my machine, it started producing 4.0 KB garbled downloaded files.

How can I do this?