Last active
March 10, 2024 11:13
-
-
Save luisaugusto/ca14460632eb0f73ce1e3cefb1c18082 to your computer and use it in GitHub Desktop.
Convert IMG tags with SVG files into inline SVG tags
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
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