Skip to content

Instantly share code, notes, and snippets.

@t27
Last active February 3, 2018 20:17
Show Gist options
  • Save t27/4754e4772380534313a8caed0223deca to your computer and use it in GitHub Desktop.
Save t27/4754e4772380534313a8caed0223deca to your computer and use it in GitHub Desktop.
Converts all the external images in a webpage to inline base64 format - which makes it easier to then "Save" the whole document without worrying about missing images
// Convert all img tags to use base64 src
function toDataUrl(url, elem) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
//callback(reader.result);
elem.src = reader.result
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}
imgs=document.getElementsByTagName('img');
for(var i =0;i<imgs.length;i++){
origUrl = imgs[i].src;
toDataUrl(origUrl,imgs[i])
}
// To run this, you can just copy and past the above at the developer console of your browser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment