Fix on-demand no-cosmetic-filtering badge when generichide in use

Related feedback:
- c090d2fde4 (commitcomment-35767596)

Mind that there might not be selectors to match as a
result of `generichide` or `no-cosmetic-filtering`.
This commit is contained in:
Raymond Hill 2019-11-01 14:01:30 -04:00
parent 2cd0d69c28
commit 987c9c1a21
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 25 additions and 10 deletions

View File

@ -52,7 +52,12 @@
surveyResults.hiddenElementCount = (( ) => {
if ( vAPI.domFilterer instanceof Object === false ) { return 0; }
const details = vAPI.domFilterer.getAllSelectors_(true);
if ( Array.isArray(details.declarative) === false ) { return 0; }
if (
Array.isArray(details.declarative) === false ||
details.declarative.length === 0
) {
return 0;
}
const selectors = details.declarative.map(entry => entry[0]);
const simple = [], complex = [];
for ( const selectorStr of selectors ) {
@ -70,20 +75,30 @@
document.body,
NodeFilter.SHOW_ELEMENT
);
const matched = new Set();
const candidates = new Set();
for (;;) {
const node = nodeIter.nextNode();
if ( node === null ) { break; }
if ( node.offsetParent !== null ) { continue; }
if (
node.matches(simpleStr) === false &&
node.closest(complexStr) !== node
) {
continue;
if ( node.offsetParent === null ) {
candidates.add(node);
}
}
const matched = new Set();
if ( simpleStr !== '') {
for ( const node of candidates ) {
if ( node.matches(simpleStr) === false ) { continue; }
candidates.delete(node);
matched.add(node);
if ( matched.size === 99 ) { break; }
}
}
if ( matched.size < 99 && complexStr !== '') {
for ( const node of candidates ) {
if ( node.closest(complexStr) !== node ) { continue; }
matched.add(node);
if ( matched.size === 99 ) { break; }
}
}
return matched.size;
})();
}