From af5aeeda6c432dd098d098c0cfe5914579094b74 Mon Sep 17 00:00:00 2001 From: gorhill Date: Sun, 26 Apr 2015 18:31:51 -0400 Subject: [PATCH] this hopefully fixes #134 --- src/js/static-net-filtering.js | 24 +++++++++++++----------- src/js/storage.js | 18 +++++++++++------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index 670a92dc7..53552dc9c 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -833,13 +833,21 @@ FilterHostnameDict.prototype.meltBucket = function(len, bucket) { } else { var offset = 0; while ( offset < bucket.length ) { - map[bucket.substring(offset, len)] = true; + map[bucket.substr(offset, len)] = true; offset += len; } } return map; }; +FilterHostnameDict.prototype.freezeBucket = function(bucket) { + var hostnames = Object.keys(bucket); + if ( hostnames[0].length * hostnames.length < this.cutoff ) { + return ' ' + hostnames.join(' ') + ' '; + } + return hostnames.sort().join(''); +}; + // How the key is derived dictates the number and size of buckets: // - more bits = more buckets = higher memory footprint // - less bits = less buckets = lower memory footprint @@ -887,7 +895,7 @@ FilterHostnameDict.prototype.add = function(hn) { return true; } if ( typeof bucket === 'string' ) { - bucket = this.dict[key] = this.meltBucket(hn.len, bucket); + bucket = this.dict[key] = this.meltBucket(hn.length, bucket); } if ( bucket.hasOwnProperty(hn) ) { return false; @@ -899,19 +907,13 @@ FilterHostnameDict.prototype.add = function(hn) { FilterHostnameDict.prototype.freeze = function() { var buckets = this.dict; - var bucket, hostnames, len; + var bucket; for ( var key in buckets ) { bucket = buckets[key]; if ( typeof bucket !== 'object' ) { continue; } - hostnames = Object.keys(bucket); - len = hostnames[0].length * hostnames.length; - if ( hostnames[0].length * hostnames.length < this.cutoff ) { - buckets[key] = ' ' + hostnames.join(' ') + ' '; - } else { - buckets[key] = hostnames.sort().join(''); - } + buckets[key] = this.freezeBucket(bucket); } }; @@ -924,7 +926,7 @@ FilterHostnameDict.prototype.matchesExactly = function(hn) { return false; } if ( typeof bucket === 'object' ) { - return bucket.hasOwnProperty(hn); + bucket = this.dict[key] = this.freezeBucket(bucket); } if ( bucket.charAt(0) === ' ' ) { return bucket.indexOf(' ' + hn + ' ') !== -1; diff --git a/src/js/storage.js b/src/js/storage.js index 943427cb8..5a4652625 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -152,22 +152,26 @@ /******************************************************************************/ -µBlock.appendUserFilters = function(filter) { - if ( filter.length === 0 ) { +µBlock.appendUserFilters = function(filters) { + if ( filters.length === 0 ) { return; } var µb = this; - var onCompiledListLoaded = function(details) { + var onCompiledListLoaded = function() { + var compiledFilters = µb.compileFilters(filters); var snfe = µb.staticNetFilteringEngine; var cfe = µb.cosmeticFilteringEngine; var acceptedCount = snfe.acceptedCount + cfe.acceptedCount; var duplicateCount = snfe.duplicateCount + cfe.duplicateCount; - µb.applyCompiledFilters(details.content); + µb.applyCompiledFilters(compiledFilters); var entry = µb.remoteBlacklists[µb.userFiltersPath]; - entry.entryCount = snfe.acceptedCount + cfe.acceptedCount - acceptedCount; - entry.entryUsedCount = entry.entryCount - snfe.duplicateCount - cfe.duplicateCount + duplicateCount; + var deltaEntryCount = snfe.acceptedCount + cfe.acceptedCount - acceptedCount; + var deltaEntryUsedCount = deltaEntryCount - (snfe.duplicateCount + cfe.duplicateCount - duplicateCount); + entry.entryCount += deltaEntryCount; + entry.entryUsedCount += deltaEntryUsedCount; + vAPI.storage.set({ 'remoteBlacklists': µb.remoteBlacklists }); µb.staticNetFilteringEngine.freeze(); µb.cosmeticFilteringEngine.freeze(); }; @@ -187,7 +191,7 @@ // If we reached this point, the filter quite probably needs to be // added for sure: do not try to be too smart, trying to avoid // duplicates at this point may lead to more issues. - µb.saveUserFilters(details.content.trim() + '\n\n' + filter.trim(), onSaved); + µb.saveUserFilters(details.content.trim() + '\n\n' + filters.trim(), onSaved); }; this.loadUserFilters(onLoaded);