htmgo/framework/assets/js/htmxextensions/htmgo.ts
2024-09-22 10:46:38 -05:00

26 lines
No EOL
777 B
TypeScript

import htmx from "htmx.org";
const evalFuncRegex = /__eval_[A-Za-z0-9]+\(\)/gm
htmx.defineExtension("htmgo", {
// @ts-ignore
onEvent: function (name, evt) {
if(name === "htmx:beforeCleanupElement" && evt.target) {
removeAssociatedScripts(evt.target as HTMLElement);
}
},
});
function removeAssociatedScripts(element: HTMLElement) {
const attributes = Array.from(element.attributes)
for (let attribute of attributes) {
const matches = attribute.value.match(evalFuncRegex) || []
for (let match of matches) {
const id = match.replace("()", "")
const ele = document.getElementById(id)
if(ele && ele.tagName === "SCRIPT") {
ele.remove()
}
}
}
}