Last active
May 27, 2020 02:36
-
-
Save andyfitz/8b5b9503e52e7c285dc917e4b690d94b to your computer and use it in GitHub Desktop.
This file contains 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
// this takes all <img src=".svg"> | |
// and makes it inline <svg ...</svg> | |
// so that custom CSS can be applied | |
var Ajax = { | |
get: function (url, onsuccess, onerror) { | |
var xhr = new XMLHttpRequest(); | |
xhr.onload = function () { | |
if (xhr.status >= 200 && xhr.status < 300) { | |
if (onsuccess) { | |
onsuccess(xhr.responseText); | |
} | |
} else if (onerror) { | |
onerror(xhr.status); | |
} | |
}; | |
xhr.open("GET", url); | |
xhr.send(); | |
} | |
}; | |
var onImgSvgDownloaded = function(img) { | |
return function(data) { | |
var divElement = document.createElement('div'); | |
divElement.innerHTML = data; | |
var parser = new DOMParser(); | |
var doc = parser.parseFromString(data, 'image/svg+xml'); | |
var svg = doc.querySelector('svg'); | |
img.parentNode.replaceChild(svg, img); | |
var imgDownloadButton = createImgDownloadButton(onDownloadPngClick(svg), onDownloadSvgClick(svg)); | |
svg.parentNode.insertBefore(imgDownloadButton, svg.nextSibling); | |
}; | |
}; | |
var svgImages = document.querySelectorAll('[src$=".svg"]'); | |
for (var i = 0; i < svgImages.length; i++) { | |
var img = svgImages[i]; | |
var imgUrl = img.getAttribute('src'); | |
Ajax.get(imgUrl, onImgSvgDownloaded(img)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment