Skip to content

Instantly share code, notes, and snippets.

@seleb
Created December 28, 2025 16:55
Show Gist options
  • Select an option

  • Save seleb/a11f99aa62c6038afb47217844b8cd46 to your computer and use it in GitHub Desktop.

Select an option

Save seleb/a11f99aa62c6038afb47217844b8cd46 to your computer and use it in GitHub Desktop.
script for downloading a set of images on a page
(() => {
const SELECTOR = '#container img';
const NAME = 'prefix_{NUM}.png';
const elCanvas = document.createElement('canvas');
const ctx = elCanvas.getContext('2d');
const a = document.createElement('a');
function download(elImg, filename) {
elCanvas.width = elImg.naturalWidth;
elCanvas.height = elImg.naturalHeight;
ctx.clearRect(0, 0, elCanvas.width, elCanvas.height);
ctx.drawImage(elImg, 0, 0);
a.href = elCanvas.toDataURL();
a.download = filename;
a.click();
}
document.querySelectorAll(SELECTOR).forEach(async (elImg, idx, arr) => {
try {
download(elImg, NAME.replace('{NUM}', idx.toString(10).padStart(arr.length.toString(10).length, '0')));
return;
} catch (e) {
console.error(e);
}
});
a.remove();
elCanvas.remove();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment