-
-
Save sfrdmn/8834747 to your computer and use it in GitHub Desktop.
;(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); |
How to execute this .js file on popup.html? Just trying to implement this into Chrome Extension :)
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.
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.
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 :)