Last active
November 22, 2021 01:33
-
-
Save jonathantneal/90385ad41b4bfcc34902fe8fd28d48f1 to your computer and use it in GitHub Desktop.
HTMLElement with contentAvailableCallback (483 bytes / 317 gzip / 252 brotli, CC0-1.0)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{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