Skip to content

Commit

Permalink
Improve trusted-click-element scriptlet
Browse files Browse the repository at this point in the history
Use `openOrClosedShadowRoot` to lookup shadow root.

Related issue:
AdguardTeam/AdguardFilters#178995
  • Loading branch information
gorhill committed May 14, 2024
1 parent e738eaa commit ee67cd6
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4482,14 +4482,28 @@ function trustedClickElement(
}
}

const getShadowRoot = elem => {
// Firefox
if ( elem.openOrClosedShadowRoot ) {
return elem.openOrClosedShadowRoot;
}
// Chromium
if ( typeof chrome === 'object' ) {
if ( chrome.dom && chrome.dom.openOrClosedShadowRoot ) {
return chrome.dom.openOrClosedShadowRoot(elem);
}
}
return null;
};

const querySelectorEx = (selector, context = document) => {
const pos = selector.indexOf(' >>> ');
if ( pos === -1 ) { return context.querySelector(selector); }
const outside = selector.slice(0, pos).trim();
const inside = selector.slice(pos + 5).trim();
const elem = context.querySelector(outside);
if ( elem === null ) { return null; }
const shadowRoot = elem.shadowRoot;
const shadowRoot = getShadowRoot(elem);
return shadowRoot && querySelectorEx(inside, shadowRoot);
};

Expand Down

0 comments on commit ee67cd6

Please sign in to comment.