Skip to content

Instantly share code, notes, and snippets.

@lances101
Created September 3, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lances101/6ac1405dcacc48f596ecc4667bb57ceb to your computer and use it in GitHub Desktop.
Save lances101/6ac1405dcacc48f596ecc4667bb57ceb to your computer and use it in GitHub Desktop.
querySelector and querySelectorAll that looks in the ShadowDOM
// quick and ugly implementation to be able to query both dom and shadowdom
function find(elem, selector) {
for(var i = 0; i < elem.children.length; i++){
let child = elem.children[i];
if(child.matches(selector))
return child;
found = find(child, selector);
if(found)
return found;
if(child.shadowRoot) {
found = find(child.shadowRoot, selector)
if(found)
return found;
}
}
return null;
}
function findAll(elem, selector, acc) {
if(!acc) {
acc = [];
}
for(var i = 0; i < elem.children.length; i++){
let child = elem.children[i];
var found = null;
if(child.matches(selector))
acc.push(child);
found = findAll(child, selector, acc);
if(child.shadowRoot) {
findAll(child.shadowRoot, selector, acc);
}
}
return acc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment