Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isContainedWithin function for ShadowRoots #1374

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion content_scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,18 @@ function isExplicitlyRequested(element) {
element.matches(runtime.conf.clickableSelector);
}

function isContainedWithin(inner, outer) {
// check if outer element contains inner element, recursively
// (works even if inner element is inside ShadowRoots)
if (!inner) {
return false;
}
if (outer.contains(inner)) {
return true;
}
return isContainedWithin(inner.getRootNode().host, outer);
}

function filterOverlapElements(elements) {
// filter out tiny elements
elements = elements.filter(function(e) {
Expand All @@ -265,7 +277,12 @@ function filterOverlapElements(elements) {
return true;
} else {
var el = document.elementFromPoint(be.left + be.width/2, be.top + be.height/2);
return !el || el.shadowRoot && el.childElementCount === 0 || el.contains(e) || e.contains(el);
return (
!el
|| el.shadowRoot && el.childElementCount === 0
|| isContainedWithin(e, el)
|| isContainedWithin(el, e)
);
}
});

Expand Down