From 3b3850f988012f38534bcb47ffca92611eab01ae Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 16 Jul 2014 10:43:34 -0400 Subject: [PATCH] report filter count accurately, better prevent duplicates for cosmetic filters --- js/abp-filters.js | 19 +++++++++++++------ js/storage.js | 18 ++++++++---------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/js/abp-filters.js b/js/abp-filters.js index 1274aa6d3..9effda05b 100644 --- a/js/abp-filters.js +++ b/js/abp-filters.js @@ -1015,9 +1015,10 @@ var FilterContainer = function() { this.tokenEnd = 0; this.filterParser = new FilterParser(); this.processedFilterCount = 0; - this.supportedFilterCount = 0; + this.acceptedCount = 0; this.allowFilterCount = 0; this.blockFilterCount = 0; + this.duplicateCount = 0; // This is for hostname-based-any-request filters this.blockedAnyPartyHostnames = new µBlock.LiquidDict(); @@ -1051,9 +1052,11 @@ FilterContainer.prototype.makeCategoryKey = function(category) { FilterContainer.prototype.addAnyPartyHostname = function(hostname) { if ( this.blockedAnyPartyHostnames.add(hostname) ) { - this.blockFilterCount += 1; + this.acceptedCount++; + this.blockFilterCount++; return true; } + this.duplicateCount++; return false; }; @@ -1061,9 +1064,11 @@ FilterContainer.prototype.addAnyPartyHostname = function(hostname) { FilterContainer.prototype.add3rdPartyHostname = function(hostname) { if ( this.blocked3rdPartyHostnames.add(hostname) ) { - this.blockFilterCount += 1; + this.acceptedCount++; + this.blockFilterCount++; return true; } + this.duplicateCount++; return false; }; @@ -1090,6 +1095,7 @@ FilterContainer.prototype.add = function(s) { } if ( this.duplicates[s] ) { + this.duplicateCount++; return false; } this.duplicates[s] = true; @@ -1102,13 +1108,13 @@ FilterContainer.prototype.add = function(s) { return false; } - this.supportedFilterCount += 1; - // Ignore optionless hostname rules, these will be taken care of by µBlock. if ( parsed.hostname && parsed.fopts === '' && parsed.action === BlockAction && reHostnameRule.test(parsed.f) ) { return false; } + this.acceptedCount += 1; + // Pure third-party hostnames, use more efficient liquid dict if ( reHostnameRule.test(parsed.f) && parsed.hostname && parsed.action === BlockAction ) { if ( parsed.fopts === 'third-party' ) { @@ -1231,9 +1237,10 @@ FilterContainer.prototype.addToCategory = function(category, tokenKey, filter) { FilterContainer.prototype.reset = function() { this.processedFilterCount = 0; - this.supportedFilterCount = 0; + this.acceptedCount = 0; this.allowFilterCount = 0; this.blockFilterCount = 0; + this.duplicateCount = 0; this.categories = {}; this.blockedAnyPartyHostnames.reset(); this.blocked3rdPartyHostnames.reset(); diff --git a/js/storage.js b/js/storage.js index c67813f42..6d99be233 100644 --- a/js/storage.js +++ b/js/storage.js @@ -261,8 +261,8 @@ // https://adblockplus.org/en/filters var abpFilters = this.abpFilters; var abpHideFilters = this.userSettings.parseAllABPHideFilters ? this.abpHideFilters : null; - var thisListCount = 0; - var thisListUsedCount = 0; + var duplicateCount = abpFilters.duplicateCount + abpHideFilters.duplicateCount; + var acceptedCount = abpFilters.acceptedCount + abpHideFilters.acceptedCount; var reLocalhost = /(^|\s)(localhost\.localdomain|localhost|local|broadcasthost|0\.0\.0\.0|127\.0\.0\.1|::1|fe80::1%lo0)(?=\s|$)/g; var reAdblockFilter = /^[^a-z0-9:]|[^a-z0-9]$|[^a-z0-9_:.-]/; var reAdblockHostFilter = /^\|\|([a-z0-9.-]+[a-z0-9])\^?$/; @@ -334,8 +334,6 @@ if ( reAdblockFilter.test(line) ) { if ( abpFilters !== null ) { if ( abpFilters.add(line) ) { - thisListCount++; - thisListUsedCount++; continue; } } @@ -352,16 +350,16 @@ continue; } - thisListCount++; - if ( abpFilters.addAnyPartyHostname(line) ) { - thisListUsedCount++; - } + abpFilters.addAnyPartyHostname(line); } // For convenience, store the number of entries for this // blacklist, user might be happy to know this information. - this.remoteBlacklists[details.path].entryCount = thisListCount; - this.remoteBlacklists[details.path].entryUsedCount = thisListUsedCount; + duplicateCount = abpFilters.duplicateCount + abpHideFilters.duplicateCount - duplicateCount; + acceptedCount = abpFilters.acceptedCount + abpHideFilters.acceptedCount - acceptedCount; + + this.remoteBlacklists[details.path].entryCount = acceptedCount + duplicateCount; + this.remoteBlacklists[details.path].entryUsedCount = acceptedCount; }; /******************************************************************************/