diff --git a/src/js/messaging.js b/src/js/messaging.js index 42a45fb37..5fc6513a8 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -374,10 +374,11 @@ const getElementCount = async function(tabId, what) { }); let total = 0; - results.forEach(count => { - if ( typeof count !== 'number' ) { return; } + for ( const count of results ) { + if ( typeof count !== 'number' ) { continue; } + if ( count === -1 ) { return -1; } total += count; - }); + } return total; }; diff --git a/src/js/popup.js b/src/js/popup.js index 8f99a70eb..2683ccb90 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -688,9 +688,15 @@ const renderPopupLazy = (( ) => { what: 'getHiddenElementCount', tabId: popupData.tabId, }).then(count => { - badge.textContent = (count || 0) !== 0 - ? Math.min(count, 99).toLocaleString() - : ''; + let text; + if ( (count || 0) === 0 ) { + text = ''; + } else if ( count === -1 ) { + text = '?'; + } else { + text = Math.min(count, 99).toLocaleString(); + } + badge.textContent = text; sw.classList.remove('hnSwitchBusy'); }); }; diff --git a/src/js/scriptlets/dom-survey-elements.js b/src/js/scriptlets/dom-survey-elements.js index e315b8b1a..3f36183c4 100644 --- a/src/js/scriptlets/dom-survey-elements.js +++ b/src/js/scriptlets/dom-survey-elements.js @@ -30,11 +30,12 @@ if ( typeof vAPI !== 'object' ) { return; } const t0 = Date.now(); + const tMax = t0 + 100; if ( vAPI.domSurveyElements instanceof Object === false ) { vAPI.domSurveyElements = { busy: false, - hiddenElementCount: -1, + hiddenElementCount: Number.NaN, surveyTime: t0, }; } @@ -44,11 +45,11 @@ surveyResults.busy = true; if ( surveyResults.surveyTime < vAPI.domMutationTime ) { - surveyResults.hiddenElementCount = -1; + surveyResults.hiddenElementCount = Number.NaN; } surveyResults.surveyTime = t0; - if ( surveyResults.hiddenElementCount === -1 ) { + if ( isNaN(surveyResults.hiddenElementCount) ) { surveyResults.hiddenElementCount = (( ) => { if ( vAPI.domFilterer instanceof Object === false ) { return 0; } const details = vAPI.domFilterer.getAllSelectors_(true); @@ -90,6 +91,7 @@ candidates.delete(node); matched.add(node); if ( matched.size === 99 ) { break; } + if ( Date.now() > tMax ) { return -1; } } } if ( matched.size < 99 && complexStr !== '') { @@ -97,6 +99,7 @@ if ( node.closest(complexStr) !== node ) { continue; } matched.add(node); if ( matched.size === 99 ) { break; } + if ( Date.now() > tMax ) { return -1; } } } return matched.size;