Last active
January 12, 2025 18:55
-
-
Save lucidBrot/432d2c6184a188a060e58dbb36bd2084 to your computer and use it in GitHub Desktop.
Bookmarklet to download all images on a page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// as one-liner for bookmarklet | |
javascript:;(function(){var images=[].slice.call(document.querySelectorAll('img'));try{images.forEach(function(img){downloadResource(img.src)})}catch(e){alert("Download failed.");console.log('Download failed.',e)}function forceDownload(blob,filename){var a=document.createElement('a');a.download=filename;a.href=blob;a.click()}function downloadResource(url,filename){if(!filename)filename=url.split('\\').pop().split('/').pop();fetch(url,{headers:new Headers({'Origin':location.origin}),mode:'cors'}).then(response=>response.blob()).then(blob=>{let blobUrl=window.URL.createObjectURL(blob);forceDownload(blobUrl,filename)}).catch(e=>console.error(e))}}).call(window); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Bookmarklet to download all images in the current page, including images from other origins. | |
Sources: | |
https://gist.github.com/sfrdmn/8834747 by sfrdmn, unlicenced but I hope you won't sue me :3 | |
https://stackoverflow.com/a/49500465/2550406 by Leeroy, CC-BY-SA | |
*/ | |
;(function() { | |
var images = [].slice.call(document.querySelectorAll('img')); | |
try { | |
images.forEach(function(img) { | |
downloadResource(img.src); | |
}) | |
} catch (e) { | |
alert("Download failed."); | |
console.log('Download failed.', e); | |
} | |
function forceDownload(blob, filename) { | |
var a = document.createElement('a'); | |
a.download = filename; | |
a.href = blob; | |
a.click(); | |
} | |
// Current blob size limit is around 500MB for browsers | |
function downloadResource( url, filename) { | |
if (!filename) filename = url.split('\\').pop().split('/').pop(); | |
fetch(url, { | |
headers: new Headers({ | |
'Origin': location.origin | |
}), | |
mode: 'cors' | |
}) | |
.then(response => response.blob()) | |
.then(blob => { | |
let blobUrl = window.URL.createObjectURL(blob); | |
forceDownload(blobUrl, filename); | |
}) | |
.catch(e => console.error(e)); | |
} | |
}).call(window); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// if the images are only thumbnails and contained directly within an <a/> element, run this first to replace the images with their quality versions... or edit the code above to call | |
//downloadResource(img.parentElement.href) | |
// instead of img.src | |
var images = [].slice.call(document.querySelectorAll('img')); | |
images.forEach(function(img){img.src = img.parentElement.href;} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this bookmarklet! This seems to be the best solution for web browsers like Edge that don't have an extension for this yet. I'm using your bookmarklet code with img.parentElement.href to avoid thumbnails.