Skip to content

Instantly share code, notes, and snippets.

@sfrdmn
Last active January 8, 2024 03:39
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save sfrdmn/8834747 to your computer and use it in GitHub Desktop.
Save sfrdmn/8834747 to your computer and use it in GitHub Desktop.
Bookmarklet to download all images on a page
;(function() {
var images = [].slice.call(document.querySelectorAll('img'))
try {
images.forEach(function(img) {
downloadImage(img)
})
} catch (e) {
alert("Download failed.");
console.log('Download failed.', e);
}
function downloadImage(img) {
var link = document.createElement('a')
link.setAttribute('href', img.src)
link.setAttribute('download', '')
link.click()
}
}).call(window);
javascript:;(function() {var images = [].slice.call(document.querySelectorAll('img'));try {images.forEach(function(img){downloadImage(img)})} catch (e) {alert('Download failed.');console.log('Download failed.', e)}function downloadImage(img) {var link = document.createElement('a');link.setAttribute('href', img.src);link.setAttribute('download', '');link.click()}}).call(window);
@lucidBrot
Copy link

Thx!
Did not work everywhere because of cross-origin loads (https://stackoverflow.com/questions/49474775/chrome-65-blocks-cross-origin-a-download-client-side-workaround-to-force-down) so I created a working version based on your snippet :)

@iyanriana
Copy link

iyanriana commented Jun 22, 2018

How to execute this .js file on popup.html? Just trying to implement this into Chrome Extension :)

@lcdsantos1310
Copy link

lcdsantos1310 commented Sep 30, 2018

Works fine, but some pictures are not included into the download batch. I have to repeat the process several times, getting dup pictures, to the download the full set.

@kenlane33
Copy link

I adjusted this to wait a half second between requests and it runs a bit more reliably. I also include an example selector with a class specified. In var sel-'img.image-large', you can narrow your downloads to just those images with a particular class. Make sure to change the class name after the . to meet your specific case:

(function() {var sel='img.image-large',i=0; var images = [].slice.call(document.querySelectorAll(sel));try {images.forEach(function(img){downloadImage(img,i++)})} catch (e) {alert('Download failed.');console.log('Download failed.', e)}function downloadImage(img) {var link = document.createElement('a');link.setAttribute('href', img.src);link.setAttribute('download', ''); setTimeout( ()=>{link.click()}, i*500 ) }}).call(window);

A variant of the code above removing filtering by class... With var sel='img', all images will download:

(function() {var sel='img',i=0; var images = [].slice.call(document.querySelectorAll(sel));try {images.forEach(function(img){downloadImage(img,i++)})} catch (e) {alert('Download failed.');console.log('Download failed.', e)}function downloadImage(img) {var link = document.createElement('a');link.setAttribute('href', img.src);link.setAttribute('download', ''); setTimeout( ()=>{link.click()}, i*500 ) }}).call(window);

To be clear, just paste one of these "one-liners" into the console of a browser page and watch all the images of that page download in (fairly) rapid succession, one every 0.5 seconds to let the browser "breathe" a bit between downloads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment