Skip to content

Instantly share code, notes, and snippets.

@andyfitz
Last active May 27, 2020 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyfitz/8b5b9503e52e7c285dc917e4b690d94b to your computer and use it in GitHub Desktop.
Save andyfitz/8b5b9503e52e7c285dc917e4b690d94b to your computer and use it in GitHub Desktop.
// 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