1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-04 01:59:38 +02:00

this hopefully fixes #134

This commit is contained in:
gorhill 2015-04-26 18:31:51 -04:00
parent 3de335291e
commit af5aeeda6c
2 changed files with 24 additions and 18 deletions

View File

@ -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;

View File

@ -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);