Skip to content

Commit 941077a

Browse files
committed
Support shadow-piercing combinator >>> in trusted-click-element
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.
1 parent f15f1b3 commit 941077a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

assets/resources/scriptlets.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4077,10 +4077,21 @@ function trustedClickElement(
40774077
? ((...args) => { safe.uboLog(...args); })
40784078
: (( ) => { });
40794079

4080+
const querySelectorEx = (selector, context = document) => {
4081+
const pos = selector.indexOf(' >>> ');
4082+
if ( pos === -1 ) { return context.querySelector(selector); }
4083+
const outside = selector.slice(0, pos).trim();
4084+
const inside = selector.slice(pos + 5).trim();
4085+
const elem = context.querySelector(outside);
4086+
if ( elem === null ) { return null; }
4087+
const shadowRoot = elem.shadowRoot;
4088+
return shadowRoot && querySelectorEx(inside, shadowRoot);
4089+
};
4090+
40804091
const selectorList = selectors.split(/\s*,\s*/)
40814092
.filter(s => {
40824093
try {
4083-
void document.querySelector(s);
4094+
void querySelectorEx(s);
40844095
} catch(_) {
40854096
return false;
40864097
}
@@ -4154,7 +4165,7 @@ function trustedClickElement(
41544165
if ( Date.now() < tnext ) { return next(); }
41554166
const selector = selectorList.shift();
41564167
if ( selector === undefined ) { return terminate(); }
4157-
const elem = document.querySelector(selector);
4168+
const elem = querySelectorEx(selector);
41584169
if ( elem === null ) {
41594170
selectorList.unshift(selector);
41604171
return next(true);

0 commit comments

Comments
 (0)