Add mutation observer to BetterHTMLElement

This commit is contained in:
Talia 2021-05-22 14:58:07 +02:00
parent 0448cd2742
commit be31d1fd44
No known key found for this signature in database
GPG key ID: AD727AD22802D0D6

View file

@ -1,4 +1,18 @@
export class BetterHTMLElement extends HTMLElement {
#observer
constructor() {
super()
let object = this
this.#observer = new MutationObserver((list, observer) => {
list.forEach(m => {
m.target.dispatchEvent(new CustomEvent("mutation", {bubbles: true, detail: m, cancelable: true}))
})
})
}
observe(options) {
if (this.#observer) this.#observer.observe(this, options)
}
attributeChangedCallback(attr, old, value) {
let name = attr.replace(/-([a-z])/, (_, l) => l.toUpperCase())
if (name+"Changed" in this) this[name+"Changed"](value, old)