Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Last active November 22, 2021 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/90385ad41b4bfcc34902fe8fd28d48f1 to your computer and use it in GitHub Desktop.
Save jonathantneal/90385ad41b4bfcc34902fe8fd28d48f1 to your computer and use it in GitHub Desktop.
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