Skip to content

Instantly share code, notes, and snippets.

@knowler
Last active January 13, 2025 21:29
Show Gist options
  • Save knowler/4c846f854a1cdb4dc5a24d06f6cd755c to your computer and use it in GitHub Desktop.
Save knowler/4c846f854a1cdb4dc5a24d06f6cd755c to your computer and use it in GitHub Desktop.
Demonstration of how to patch the custom element registry to add a `connected` event.
const originalDefine = CustomElementRegistry.prototype.define;
CustomElementRegistry.prototype.define = function(_name, constructor) {
const originalConnectedCallback = constructor.prototype.connectedCallback;
constructor.prototype.connectedCallback = function() {
// Maybe use a prefixed event name?
// Maybe configure some options (i.e. whether it bubbles)
this.dispatchEvent(new CustomEvent("connected"));
originalConnectedCallback?.call(this, ...arguments);
}
originalDefine.call(this, ...arguments);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment