Skip to content

Instantly share code, notes, and snippets.

@zazaulola
Forked from Spencer-Doak/SearchInTheShadows.js
Last active August 14, 2024 01:12
Show Gist options
  • Save zazaulola/c1c2e80bdeeff8ad14399cf1724f9854 to your computer and use it in GitHub Desktop.
Save zazaulola/c1c2e80bdeeff8ad14399cf1724f9854 to your computer and use it in GitHub Desktop.
Recursively find all shadow roots in a page.
/**
* Find all elements with openned ShadowRoots
*
* @param {Node} e - An element that we should search for ShadowRoots within.
* @returns {Array<Element>} Array of Elements that holds ShadowRoot
*/
const findRoots = (e = document.documentElement) =>
[e,...e.querySelectorAll('*')]
.filter(e => e.shadowRoot)
.flatMap(e => [e, ...findRoots(e.shadowRoot)]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment