Improve `trusted-click-element` scriptlet

Use `openOrClosedShadowRoot` to lookup shadow root.

Related issue:
https://github.com/AdguardTeam/AdguardFilters/issues/178995
This commit is contained in:
Raymond Hill 2024-05-14 11:36:17 -04:00
parent e738eaa447
commit ee67cd6284
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 15 additions and 1 deletions

View File

@ -4482,6 +4482,20 @@ 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 querySelectorEx = (selector, context = document) => {
const pos = selector.indexOf(' >>> '); const pos = selector.indexOf(' >>> ');
if ( pos === -1 ) { return context.querySelector(selector); } if ( pos === -1 ) { return context.querySelector(selector); }
@ -4489,7 +4503,7 @@ function trustedClickElement(
const inside = selector.slice(pos + 5).trim(); const inside = selector.slice(pos + 5).trim();
const elem = context.querySelector(outside); const elem = context.querySelector(outside);
if ( elem === null ) { return null; } if ( elem === null ) { return null; }
const shadowRoot = elem.shadowRoot; const shadowRoot = getShadowRoot(elem);
return shadowRoot && querySelectorEx(inside, shadowRoot); return shadowRoot && querySelectorEx(inside, shadowRoot);
}; };