2021-11-02 17:20:43 +00:00
|
|
|
const apply = (func, node) => {
|
|
|
|
if (typeof func == "function") {
|
|
|
|
func(node)
|
|
|
|
} else if ("then" in func) {
|
|
|
|
func.then(result => apply(result, node))
|
|
|
|
} else if ("default" in func) {
|
2021-12-30 22:23:38 +00:00
|
|
|
apply(func.default, node)
|
2021-11-02 17:20:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const use = node => {
|
|
|
|
const code = Function("return (" + node.getAttribute("use") + ")")
|
2022-07-01 07:24:14 +00:00
|
|
|
node.removeAttribute("use")
|
2021-11-02 17:20:43 +00:00
|
|
|
const func = code()
|
|
|
|
apply(func, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const observe = (root = document) => {
|
|
|
|
const observer = new MutationObserver(mutations => {
|
|
|
|
mutations.forEach(mutation => {
|
|
|
|
mutation.addedNodes.forEach(node => {
|
|
|
|
if (node.hasAttribute("use")) {
|
|
|
|
use(node)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
observer.observe(root, {subtree: true, childList: true})
|
|
|
|
return observer
|
|
|
|
}
|
|
|
|
|
|
|
|
export const run = (root = document) => {
|
|
|
|
root.querySelectorAll("[use]").forEach(use)
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:51:17 +00:00
|
|
|
export const whenReady = () => {
|
|
|
|
if (document.readyState == "complete")
|
|
|
|
run(document)
|
|
|
|
else
|
|
|
|
document.addEventListener("readystatechange", whenReady, {once: true})
|
|
|
|
}
|
|
|
|
|
2021-11-02 17:20:43 +00:00
|
|
|
export const install = () => {
|
|
|
|
observe(document)
|
|
|
|
run(document)
|
|
|
|
}
|
|
|
|
export default install
|