Skip to content

Instantly share code, notes, and snippets.

@ThomazPom
Created October 3, 2022 16:33
Show Gist options
  • Save ThomazPom/1e57e416c4b111b27e3d44d640cfb920 to your computer and use it in GitHub Desktop.
Save ThomazPom/1e57e416c4b111b27e3d44d640cfb920 to your computer and use it in GitHub Desktop.
function discoverAllShadowRoots(){
let shadowroots = []
function discover(elem){
let elemshadowroots = [...elem.querySelectorAll("*")]
elemshadowroots.push(elem)
elemshadowroots=elemshadowroots.map(z=>z.shadowRoot).filter(z=>z)
elemshadowroots.forEach(discover)
shadowroots.push(...elemshadowroots)
}
discover(document.body)
return shadowroots;
}
document.querySelectorAllShadowRoot = function(selector){
let shadowRoots = discoverAllShadowRoots()
for (let index = 0; index < shadowRoots.length; index++) {
const sr = shadowRoots[index];
srsl = sr.querySelector(selector)
if(srsl)
{
return srsl
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment