make me a javascript code to automatically download an image in a website. the link will…
- User
make me a javascript code to automatically download an image in a website. the link will be contained in an element as such:
" " filter it so that the only image beingdownload begins with "https://static.wikia.nocookie.net/leagueoflegends/images/" and ends with .jpg, ignooflegends/images/" and ends with .jpg, ignoring "/revision/latest/scale-to-width-down/1000?cb=20200917183858" part
- Assistant
You can use JavaScript to achieve this by looping through all the image elements and checking if the
srcattribute of each image meets the specified criteria. Here's an example code snippet that demonstrates this:javascript// Find all the image elements on the page var images = document.getElementsByTagName('img'); // Loop through each image for (var i = 0; i < images.length; i++) { var image = images[i]; // Check if the source URL meets the specified criteria if (image.src.startsWith('https://static.wikia.nocookie.net/leagueoflegends/images/') && image.src.endsWith('.jpg')) { // Create a link and set the download attribute to force download var link = document.createElement('a'); link.href = image.src; link.download = 'image' + i + '.jpg'; // Append the link to the body and click it programmatically to trigger download document.body.appendChild(link); link.click(); // Remove the link from the body document.body.removeChild(link); } }