Skip to content

Instantly share code, notes, and snippets.

@jadeallencook
Created August 13, 2018 01:22
Show Gist options
  • Save jadeallencook/085a34203128bce70c6247978006fc85 to your computer and use it in GitHub Desktop.
Save jadeallencook/085a34203128bce70c6247978006fc85 to your computer and use it in GitHub Desktop.
Download all the images on the current page.
var images = [],
nodes = document.getElementsByTagName('*');
// loop over all nodes in dom
for (var x = 0, max = nodes.length; x < max; x++) {
// look for nodes with bg imgs
var node = nodes[x],
image = node.style.backgroundImage;
// if no bg img, check for img node type
if (!image && node.nodeName === 'IMG') image = node.src;
else image = image.replace('url("', '').replace('")', '');
if (image && image !== 'none') {
// dl img
var link = window.location.href;
// format link
if (link.indexOf('#') !== -1) link = link.substring(0, link.indexOf('#'));
if (image.indexOf('https://') !== -1 || image.indexOf('data:image') !== -1) link = image;
else link += image;
// download img
var download = document.createElement('a');
download.href = link;
download.download = image.substr(image.lastIndexOf('/') + 1);
download.target = '_blank';
document.body.appendChild(download);
download.click();
// cache img link for output
images.push(link);
}
}
console.log('Downloaded ' + images.length + ' images: ', images);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment