Download images with JavaScript

1

Well, I'm needing a script to automatically download images to me.

For example the url f% of the image starts at link /the_gamer/the_gamer035-01.jpg to link , so I need a% com_of% to download all the images, if it is possible I need the name of the photo is kept as the original.

    
asked by anonymous 03.10.2017 / 13:35

3 answers

2

A possible solution would be to create a tag and take the images through them.

// Função para baixar a imagem...
function downloadImage(src) {
var img = document.createElement("img");
img.src = src;

    return img;
}

// Imagens baixadas:
var images = [];

// Loop para baixar as imagens...
for(var i = 1; i < 35; i++) {
    // Primeiro pegamos o valor de "i" e transformamos em uma string...
    var n = i.toString();

    // Colocar um zero a mais caso seja necessário...
    if(n.length < 2) {
        n = "0" + n;
    }

    // Agora é só criar uma tag <img> e ir colocando na Array...
    images.push(downloadImage("http:///the_gamer/the_gamer035-" + n + ".jpg"));
}

Then I would just go get what was stored in the Array.

    
03.10.2017 / 13:57
3

Taking advantage of the idea of the Luiz Santos loop and the brother script this post

for(var x = 1;x <= 25; x++ ){
if( x < 10)
var url = "0" + x + ".jpg";
else
var url = x + ".jpg";

var a = document.createElement('a');
a.href = "http:///the_gamer/the_gamer035-"+url;
a.download = "035-"+url;
a.click();

}

In Google Chrome Version 61.0.3163.100 a "Download multiple files" question "Allow" is asked "Lock". The browser must be configured to download files automatically. Otherwise you will be waiting for confirmation of each download.

To automatically set downloads on Google Chrome

1 - On your computer, open Google Chrome.

2 - In the upper right corner, click Settings

3 - At the bottom of the page, click Advanced

4 - In the "Downloads" section, adjust your download settings

  

The question link does not seem to work http:///the_gamer/the_gamer035-01.jpg

    
03.10.2017 / 14:21
2

Javascript for downloading an image is as follows:

var a = $("<a>")
    .attr("href", "http:///the_gamer/the_gamer035-01.jpg")
    .attr("download", "img.png")
    .appendTo("body");

a[0].click();

a.remove();

Knowing this is enough to make a for the number of the item you want:

var url = "http:///the_gamer/the_gamer035-"

for(var x = 1;x <= 25; x++ ){
if( x < 10)
var url = url + "0" + x + ".jpg";
else
var url = url + x + ".jpg";

var a = $("<a>")
    .attr("href", "http:///the_gamer/the_gamer035-01.jpg")
    .attr("download", "img.png")
    .appendTo("body");

a[0].click();

a.remove();
}

It will download all images up to the x number stipulated for output from the for.

Note: As said by Renan in the comments:

  

Remember that to function in a comfortable way the browser   must be set to download files automatically. Case   otherwise it will wait for the user's confirmation on where to save   each file

    
03.10.2017 / 13:46