Skip to content

Instantly share code, notes, and snippets.

@micartey
Created February 6, 2026 13:15
Show Gist options
  • Select an option

  • Save micartey/d512e300d1da0fa219865ca8570b32cf to your computer and use it in GitHub Desktop.

Select an option

Save micartey/d512e300d1da0fa219865ca8570b32cf to your computer and use it in GitHub Desktop.
document.querySelectorAll('img[src$=".svg"]').forEach(async img => {
try {
const response = await fetch(img.src);
const text = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(text, 'image/svg+xml');
const svg = doc.documentElement;
// styling
svg.style.userSelect = 'text';
svg.style.cursor = 'text';
svg.querySelectorAll('text, tspan').forEach(t => {
t.style.userSelect = 'text';
t.style.cursor = 'text';
});
// Identify if the image is wrapped in a link
const parentLink = img.closest('a');
const targetToReplace = parentLink ? parentLink : img;
targetToReplace.replaceWith(svg);
} catch (e) {
console.error('Conversion failed:', img.src);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment