From 7ad66fdb2c49f18152a7d67cba8898de73317342 Mon Sep 17 00:00:00 2001 From: gorhill Date: Sun, 26 Apr 2015 18:48:21 -0400 Subject: [PATCH] this fixes https://github.com/gorhill/uBlock/issues/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 9e2b4d588..8a860b14b 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -832,13 +832,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 @@ -886,7 +894,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; @@ -898,19 +906,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); } }; @@ -923,7 +925,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 cbc825077..618761170 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);