-
-
Save jeffreymorganio/8436232431b0e47bd46afa9a03299f07 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