Last active
May 31, 2024 12:53
-
-
Save erhangundogan/b0f3612af91a794a7ba38a908eb36162 to your computer and use it in GitHub Desktop.
Find all custom elements on the page
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
const allCustomElements = []; | |
function isCustomElement(el) { | |
const isAttr = el.getAttribute('is'); | |
// Check for <super-button> and <button is="super-button">. | |
return el.localName.includes('-') || isAttr && isAttr.includes('-'); | |
} | |
function findAllCustomElements(nodes) { | |
for (let i = 0, el; el = nodes[i]; ++i) { | |
if (isCustomElement(el)) { | |
allCustomElements.push(el); | |
} | |
// If the element has shadow DOM, dig deeper. | |
if (el.shadowRoot) { | |
findAllCustomElements(el.shadowRoot.querySelectorAll('*')); | |
} | |
} | |
} | |
findAllCustomElements(document.querySelectorAll('*')); | |
console.log(allCustomElements); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment