Last active
February 3, 2018 20:17
-
-
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
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
// 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