Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
this fixes #276
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed May 21, 2014
1 parent 43294f8 commit 93725cb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
48 changes: 36 additions & 12 deletions js/abp-hide-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ var FilterContainer = function() {
this.acceptedCount = 0;
this.processedCount = 0;
this.filters = {};
this.rejected = [];
};

/******************************************************************************/
Expand All @@ -252,6 +253,9 @@ FilterContainer.prototype.reset = function() {
this.acceptedCount = 0;
this.processedCount = 0;
this.filters = {};
this.hideUnfiltered = [];
this.donthideUnfiltered = [];
this.rejected = [];
};

/******************************************************************************/
Expand Down Expand Up @@ -289,21 +293,44 @@ FilterContainer.prototype.add = function(s) {
// Content script side, the unsorted container of selectors could be used
// in a querySelectorAll() to figure which rules apply (if any), or they
// could just all be injected undiscriminately (not good).
if ( parsed.isElement() ) {
return this.addElementFilter(parsed);
if ( parsed.filterType === '#' ) {
this.hideUnfiltered.push(parsed.suffix);
} else {
this.donthideUnfiltered.push(parsed.suffix);
}
this.acceptedCount += 1;

return false;
return true;
};

/******************************************************************************/

FilterContainer.prototype.chunkify = function(selectors) {
var chunkified = [], chunk;
for (;;) {
chunk = selectors.splice(0, 10);
if ( chunk.length === 0 ) {
break;
}
chunkified.push(chunk.join(','));
}
return chunkified;
};

/******************************************************************************/

FilterContainer.prototype.freeze = function() {
this.hideUnfiltered = this.chunkify(this.hideUnfiltered);
this.donthideUnfiltered = this.chunkify(this.donthideUnfiltered);

this.filterParser.reset();

console.log('HTTPSB> adp-hide-filters.js: %d filters accepted', this.acceptedCount);
console.log('HTTPSB> adp-hide-filters.js: %d filters processed', this.processedCount);
console.log('HTTPSB> adp-hide-filters.js: coverage is %s%', (this.acceptedCount * 100 / this.processedCount).toFixed(1));
console.log('HTTPSB> adp-hide-filters.js: unfiltered hide selectors:', this.hideUnfiltered);
console.log('HTTPSB> adp-hide-filters.js: unfiltered dont hide selectors:', this.donthideUnfiltered);
console.log('HTTPSB> adp-hide-filters.js: rejected selectors:', this.rejected);

//histogram('allFilters', this.filters);
};
Expand Down Expand Up @@ -436,12 +463,12 @@ FilterContainer.prototype.addPlainFilter = function(parsed) {
/******************************************************************************/

FilterContainer.prototype.addPlainMoreFilter = function(parsed) {
var plainSelector = parsed.extractPlain();
if ( plainSelector === '' ) {
var selectorSuffix = parsed.extractPlain();
if ( selectorSuffix === '' ) {
return;
}
var f = new FilterPlainMore(parsed.suffix);
var hash = makeSuffixHash(parsed.filterType, plainSelector);
var hash = makeSuffixHash(parsed.filterType, selectorSuffix);
this.addFilterEntry(hash, f);
this.acceptedCount += 1;
};
Expand Down Expand Up @@ -469,11 +496,6 @@ FilterContainer.prototype.addHostnameFilter = function(parsed) {

/******************************************************************************/

FilterContainer.prototype.addElementFilter = function(parsed) {
};

/******************************************************************************/

FilterContainer.prototype.addFilterEntry = function(hash, f) {
var bucket = this.filters[hash];
if ( bucket === undefined ) {
Expand Down Expand Up @@ -543,7 +565,9 @@ FilterContainer.prototype.retrieve = function(url, inSelectors) {

return {
hide: hideSelectors,
donthide: donthideSelectors
donthide: donthideSelectors,
hideUnfiltered: this.hideUnfiltered,
donthideUnfiltered: this.donthideUnfiltered
};
};

Expand Down
22 changes: 19 additions & 3 deletions js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ CosmeticFiltering.prototype.retrieve = function() {
selectors = selectors.concat(this.idSelectors);
}
if ( selectors.length > 0 ) {
// console.log('HTTPSB> ABP cosmetic filters: retrieving CSS rules using %d selectors (%o)', selectors.length, selectors);
//console.log('HTTPSB> ABP cosmetic filters: retrieving CSS rules using %d selectors', selectors.length);
chrome.runtime.sendMessage({
what: 'retrieveABPHideSelectors',
selectors: selectors,
Expand All @@ -68,16 +68,18 @@ CosmeticFiltering.prototype.retrieveHandler = function(selectors) {
return;
}
var styleText = [];
this.filterUnfiltered(selectors.hideUnfiltered, selectors.hide);
this.reduce(selectors.hide, this.injectedSelectors);
if ( selectors.hide.length ) {
var hideStyleText = '{{hideSelectors}} {display:none;}'
var hideStyleText = '{{hideSelectors}} {display:none; !important}'
.replace('{{hideSelectors}}', selectors.hide.join(','));
styleText.push(hideStyleText);
//console.log('HTTPSB> ABP cosmetic filters: injecting %d CSS rules:', selectors.hide.length, hideStyleText);
}
this.filterUnfiltered(selectors.donthideUnfiltered, selectors.donthide);
this.reduce(selectors.donthide, this.injectedSelectors);
if ( selectors.donthide.length ) {
var dontHideStyleText = '{{donthideSelectors}} {display:initial;}'
var dontHideStyleText = '{{donthideSelectors}} {display:initial; !important}'
.replace('{{donthideSelectors}}', selectors.donthide.join(','));
styleText.push(dontHideStyleText);
//console.log('HTTPSB> ABP cosmetic filters: injecting %d CSS rules:', selectors.donthide.length, dontHideStyleText);
Expand All @@ -89,6 +91,20 @@ CosmeticFiltering.prototype.retrieveHandler = function(selectors) {
}
};

CosmeticFiltering.prototype.filterUnfiltered = function(inSelectors, outSelectors) {
var i = inSelectors.length;
var selector;
while ( i-- ) {
selector = inSelectors[i];
if ( this.injectedSelectors[selector] ) {
continue;
}
if ( document.querySelector(selector) !== null ) {
outSelectors.push(selector);
}
}
};

CosmeticFiltering.prototype.reduce = function(selectors, dict) {
var first = dict.httpsb === undefined;
var i = selectors.length, selector, end;
Expand Down

0 comments on commit 93725cb

Please sign in to comment.