From 6bd356dd2cfe98287096e528877ae50c5b1f9284 Mon Sep 17 00:00:00 2001 From: gorhill Date: Sat, 6 Aug 2016 12:05:01 -0400 Subject: [PATCH] fix #1856 --- src/js/messaging.js | 81 +++++++++++++--------------- src/js/popup.js | 31 +++++------ src/js/scriptlets/cosmetic-survey.js | 2 +- 3 files changed, 55 insertions(+), 59 deletions(-) diff --git a/src/js/messaging.js b/src/js/messaging.js index 36e5cd1ac..9f7cacc03 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -22,12 +22,12 @@ /******************************************************************************/ /******************************************************************************/ +'use strict'; + // Default handler (function() { -'use strict'; - /******************************************************************************/ var µb = µBlock; @@ -192,8 +192,6 @@ vAPI.messaging.setup(onMessage); (function() { -'use strict'; - /******************************************************************************/ var µb = µBlock; @@ -356,30 +354,17 @@ var popupDataFromRequest = function(request, callback) { /******************************************************************************/ -var getPopupDataLazy = function(tabId, callback) { - var r = { - hiddenElementCount: '' - }; - var pageStore = µb.pageStoreFromTabId(tabId); - - if ( !pageStore ) { - callback(r); - return; - } - - µb.scriptlets.inject(tabId, 'cosmetic-survey', function() { - r.hiddenElementCount = pageStore.hiddenElementCount; - callback(r); - }); -}; - -/******************************************************************************/ - var onMessage = function(request, sender, callback) { + var pageStore; + // Async switch ( request.what ) { - case 'getPopupDataLazy': - getPopupDataLazy(request.tabId, callback); + case 'getPopupLazyData': + pageStore = µb.pageStoreFromTabId(request.tabId); + if ( pageStore !== null ) { + pageStore.hiddenElementCount = 0; + µb.scriptlets.injectDeep(request.tabId, 'cosmetic-survey'); + } return; case 'getPopupData': @@ -391,7 +376,6 @@ var onMessage = function(request, sender, callback) { } // Sync - var pageStore; var response; switch ( request.what ) { @@ -454,8 +438,6 @@ vAPI.messaging.listen('popupPanel', onMessage); (function() { -'use strict'; - /******************************************************************************/ var µb = µBlock; @@ -585,8 +567,6 @@ vAPI.messaging.listen('contentscript', onMessage); (function() { -'use strict'; - /******************************************************************************/ var µb = µBlock; @@ -665,8 +645,6 @@ vAPI.messaging.listen('elementPicker', onMessage); (function() { -'use strict'; - /******************************************************************************/ var µb = µBlock; @@ -727,8 +705,6 @@ vAPI.messaging.listen('cloudWidget', onMessage); (function() { -'use strict'; - /******************************************************************************/ var µb = µBlock; @@ -1031,8 +1007,6 @@ vAPI.messaging.listen('dashboard', onMessage); (function() { -'use strict'; - /******************************************************************************/ var µb = µBlock; @@ -1146,8 +1120,6 @@ vAPI.messaging.listen('loggerUI', onMessage); (function() { -'use strict'; - /******************************************************************************/ var onMessage = function(request, sender, callback) { @@ -1185,11 +1157,27 @@ vAPI.messaging.listen('documentBlocked', onMessage); (function() { -'use strict'; - /******************************************************************************/ var µb = µBlock; +var broadcastTimers = Object.create(null); + +/******************************************************************************/ + +var cosmeticallyFilteredElementCountChanged = function(tabId) { + delete broadcastTimers[tabId + '-cosmeticallyFilteredElementCountChanged']; + + var pageStore = µb.pageStoreFromTabId(tabId); + if ( pageStore === null ) { + return; + } + + vAPI.messaging.broadcast({ + what: 'cosmeticallyFilteredElementCountChanged', + tabId: tabId, + count: pageStore.hiddenElementCount + }); +}; /******************************************************************************/ @@ -1231,9 +1219,16 @@ var onMessage = function(request, sender, callback) { var response; switch ( request.what ) { - case 'liveCosmeticFilteringData': - if ( pageStore !== null ) { - pageStore.hiddenElementCount = request.filteredElementCount; + case 'cosmeticallyFilteredElementCount': + if ( pageStore !== null && request.filteredElementCount ) { + pageStore.hiddenElementCount += request.filteredElementCount; + var broadcastKey = tabId + '-cosmeticallyFilteredElementCountChanged'; + if ( broadcastTimers[broadcastKey] === undefined ) { + broadcastTimers[broadcastKey] = vAPI.setTimeout( + cosmeticallyFilteredElementCountChanged.bind(null, tabId), + 250 + ); + } } break; diff --git a/src/js/popup.js b/src/js/popup.js index 8b068a580..c42e9d114 100644 --- a/src/js/popup.js +++ b/src/js/popup.js @@ -21,12 +21,12 @@ /* global punycode, uDom */ +'use strict'; + /******************************************************************************/ (function() { -'use strict'; - /******************************************************************************/ // Ensure the popup is properly sized as soon as possible. It is assume the DOM @@ -507,23 +507,24 @@ var renderPopup = function() { /******************************************************************************/ var renderPopupLazy = function() { - var onDataReady = function(data) { - if ( !data ) { return; } - var v = data.hiddenElementCount || ''; + messaging.send('popupPanel', { what: 'getPopupLazyData', tabId: popupData.tabId }); +}; + +var onPopupMessage = function(data) { + if ( !data ) { return; } + if ( data.tabId !== popupData.tabId ) { return; } + + switch ( data.what ) { + case 'cosmeticallyFilteredElementCountChanged': + var v = data.count || ''; uDom.nodeFromSelector('#no-cosmetic-filtering > span.badge') .textContent = typeof v === 'number' ? v.toLocaleString() : v; - }; - - messaging.send( - 'popupPanel', - { - what: 'getPopupDataLazy', - tabId: popupData.tabId - }, - onDataReady - ); + break; + } }; +messaging.addChannelListener('popup', onPopupMessage); + /******************************************************************************/ var toggleNetFilteringSwitch = function(ev) { diff --git a/src/js/scriptlets/cosmetic-survey.js b/src/js/scriptlets/cosmetic-survey.js index 14f921e88..bcc1252fe 100644 --- a/src/js/scriptlets/cosmetic-survey.js +++ b/src/js/scriptlets/cosmetic-survey.js @@ -39,7 +39,7 @@ vAPI.messaging.send( 'scriptlets', { - what: 'liveCosmeticFilteringData', + what: 'cosmeticallyFilteredElementCount', pageURL: window.location.href, filteredElementCount: xpr && xpr.numberValue || 0 }