Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HTMLElement with contentAvailableCallback (483 bytes / 317 gzip / 252 brotli, CC0-1.0)
{
let NativeHTMLElement = globalThis.HTMLElement, isContentAvailable = (element) => {
if (element.ownerDocument.readyState !== 'loading') return true
while (element) {
if (element.nextSibling) return true
element = element.parentElement
}
}
(
(
globalThis.HTMLElement = Object.setPrototypeOf(function HTMLElement() {
let host = Reflect.construct(NativeHTMLElement, [], new.target), { contentAvailableCallback } = host
if (typeof contentAvailableCallback === 'function') {
if (isContentAvailable(host)) contentAvailableCallback.call(host)
else new MutationObserver((record, observer) => {
if (isContentAvailable(host)) {
observer.disconnect()
contentAvailableCallback.call(host)
}
}).observe(host, { childList: true })
}
return host
}, Element)
).prototype = NativeHTMLElement.prototype
).constructor = HTMLElement
}
{let e=globalThis.HTMLElement,t=e=>{if("loading"!==e.ownerDocument.readyState)return!0;for(;e;){if(e.nextSibling)return!0;e=e.parentElement}};((globalThis.HTMLElement=Object.setPrototypeOf((function HTMLElement(){let n=Reflect.construct(e,[],new.target),{contentAvailableCallback:l}=n;return"function"==typeof l&&(t(n)?l.call(n):new MutationObserver(((e,o)=>{t(n)&&(o.disconnect(),l.call(n))})).observe(n,{childList:!0})),n}),Element)).prototype=e.prototype).constructor=HTMLElement}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment