Last active
February 12, 2024 20:12
-
-
Save deanmcpherson/c3db37f76655528896146c9a3f4c4a3a to your computer and use it in GitHub Desktop.
super quick and dirty code embed support
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
//<script> | |
function clearBlock(el) { | |
const node = el.parentElement.parentElement; | |
node.innerHTML = ''; | |
return node; | |
} | |
const SELECTOR = 'code:not([super-embed-seen])'; | |
function setupEmbeds() { | |
document.querySelectorAll(SELECTOR).forEach((node) => { | |
node.setAttribute('super-embed-seen', 1); | |
if (node.innerText.startsWith('super-embed:')) { | |
const code = node.innerText.replace('super-embed:', ''); | |
const parentNode = clearBlock(node); | |
parentNode.innerHTML = code; | |
parentNode.querySelectorAll('script').forEach((script) => { | |
if (!script.src && script.innerText) { | |
eval(script.innerText); | |
} else { | |
const scr = document.createElement('script'); | |
scr.src = script.src; | |
document.body.appendChild(scr); | |
} | |
}) | |
} | |
}) | |
} | |
setupEmbeds(); | |
var observer = new MutationObserver(function(mutations) { | |
if (document.querySelector(SELECTOR)) { | |
setupEmbeds(); | |
} | |
}); | |
observer.observe(document, {attributes: false, childList: true, characterData: false, subtree:true}); | |
//</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment