Skip to content

Instantly share code, notes, and snippets.

@luisaugusto
Last active March 10, 2024 11:13
Show Gist options
  • Save luisaugusto/ca14460632eb0f73ce1e3cefb1c18082 to your computer and use it in GitHub Desktop.
Save luisaugusto/ca14460632eb0f73ce1e3cefb1c18082 to your computer and use it in GitHub Desktop.
Convert IMG tags with SVG files into inline SVG tags
const convertImages = (query, callback) => {
const images = document.querySelectorAll(query);
images.forEach(image => {
fetch(image.src)
.then(res => res.text())
.then(data => {
const parser = new DOMParser();
const svg = parser.parseFromString(data, 'image/svg+xml').querySelector('svg');
if (image.id) svg.id = image.id;
if (image.className) svg.classList = image.classList;
image.parentNode.replaceChild(svg, image);
})
.then(callback)
.catch(error => console.error(error))
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment