Skip to content

Commit

Permalink
Fix issue with the button counter where adBlocker finds an ad but no …
Browse files Browse the repository at this point in the history
…other trackers on page
  • Loading branch information
IAmThePan authored and christophertino committed Jul 13, 2018
1 parent 4ff4664 commit 751efc7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ function onMessageHandler(request, sender, callback) {
} else if (name === 'getCliqzModuleData') {
const modules = { adblock: {}, antitracking: {} };
utils.getActiveTab((tab) => {
button.update();
if (conf.enable_anti_tracking) {
cliqz.modules.antitracking.background.actions.aggregatedBlockingStats(tab.id).then((data) => {
modules.antitracking = data;
Expand Down Expand Up @@ -1561,4 +1562,3 @@ function init() {

// Initialize the application.
init();

4 changes: 2 additions & 2 deletions src/classes/BrowserButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class BrowserButton {
*/
_getAntiTrackCount(tabId) {
return new Promise((resolve, reject) => {
if (!conf.enable_anti_tracking) {
if (!conf.enable_anti_tracking || !antitracking.background) {
resolve(0);
}
antitracking.background.actions.aggregatedBlockingStats(tabId).then((antiTracking) => {
Expand Down Expand Up @@ -212,7 +212,7 @@ class BrowserButton {
* @return {number} the number of trackers in an object
*/
_getAdBlockCount(tabId) {
if (!conf.enable_ad_block) {
if (!conf.enable_ad_block || !adblocker.background) {
return 0;
}
const adBlocking = adblocker.background.actions.getAdBlockInfoForTab(tabId);
Expand Down
11 changes: 11 additions & 0 deletions src/classes/EventHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class EventHandlers {
this.policy = new Policy();
this.policySmartBlock = new PolicySmartBlock();
this.purplebox = new PurpleBox();

// Use leading:false so button.update is called after requests are complete.
// Use a 1sec interval to limit calls on pages with a large number of requests.
// Don't use tabId with button.update for cases where tab is switched before throttle delay is reached.
// ToDo: Remove this function when there is an event for AdBlocker:foundAd.
this._throttleButtonUpdate = _.throttle(() => {
button.update();
}, 1000, { leading: false });
}

/**
Expand Down Expand Up @@ -362,6 +370,9 @@ class EventHandlers {

// allow if not a tracker
if (!bug_id) {
// Make a throttled call to button.update() for when there are no trackers but an ad was blocked
// ToDo: Remove this call when there is an event for AdBlocker:foundAd.
this._throttleButtonUpdate();
return { cancel: false };
}

Expand Down

0 comments on commit 751efc7

Please sign in to comment.