Skip to content

Commit

Permalink
Support shadow-piercing combinator >>> in trusted-click-element
Browse files Browse the repository at this point in the history
Related issue:
uBlockOrigin/uBlock-issues#2971

Example usage:

...##+js(trusted-trusted-click-element, #cmpwrapper >>> .cmpboxbtnyes)

The substring before ` >>> ` must select an element with a non-null
shadow root, in which case the substring after ` >>> ` will be used
to find the element in the targeted shadow root. ` >>> ` can be used
recursively when multiple shadow root must be pierced.
  • Loading branch information
gorhill committed Dec 4, 2023
1 parent f15f1b3 commit 941077a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4077,10 +4077,21 @@ function trustedClickElement(
? ((...args) => { safe.uboLog(...args); })
: (( ) => { });

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;
return shadowRoot && querySelectorEx(inside, shadowRoot);
};

const selectorList = selectors.split(/\s*,\s*/)
.filter(s => {
try {
void document.querySelector(s);
void querySelectorEx(s);
} catch(_) {
return false;
}
Expand Down Expand Up @@ -4154,7 +4165,7 @@ function trustedClickElement(
if ( Date.now() < tnext ) { return next(); }
const selector = selectorList.shift();
if ( selector === undefined ) { return terminate(); }
const elem = document.querySelector(selector);
const elem = querySelectorEx(selector);
if ( elem === null ) {
selectorList.unshift(selector);
return next(true);
Expand Down

0 comments on commit 941077a

Please sign in to comment.