Download all images from current page with JavaScript

August 18th, 2014 by Alex Leave a reply »
// get possible elements, init variables
var elems = document.querySelectorAll('*'), elems2 = document.querySelectorAll('img');
var images = {}, a = 0, href = '';
// search for CSS background-image
for (a = 0; a < elems.length; a++) {
  style = elems[a].currentStyle || window.getComputedStyle(elems[a], false);
  var bg = style.backgroundImage.slice(4, -1);
  if (bg) {
    images[bg] = bg;
  }
}
// search for <img src="" />
for (a = 0; a < elems2.length; a++) {
  if (href = elems2[a].getAttribute('src'))
    images[href] = href;
}
// generate <a> and force download
for (var i in images) {
  var link = document.createElement('a');
  link.setAttribute('download', '');
  link.setAttribute('href', i);
  link.click();
  delete link;
}

This page can be found by searching for:

image download with javascriptjavascript download all images on page



Comments are closed.