Skip to content

Commit

Permalink
add to gorhill#2984: fix regressions, as per feedback and code review
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Oct 23, 2017
1 parent 14109b3 commit 6e18829
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 90 deletions.
1 change: 1 addition & 0 deletions platform/chromium/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ vAPI.supportsUserStylesheets =
chrome.extensionTypes instanceof Object &&
chrome.extensionTypes.CSSOrigin instanceof Object &&
'USER' in chrome.extensionTypes.CSSOrigin;
vAPI.insertCSS = chrome.tabs.insertCSS;

var noopFunc = function(){};

Expand Down
10 changes: 5 additions & 5 deletions platform/chromium/vapi-usercss.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ vAPI.DOMFilterer = function() {
};

vAPI.DOMFilterer.prototype = {
reHideStyle: /^display: none !important;$/,
reHideStyle: /^display:none!important;$/,

// https://www.w3.org/community/webed/wiki/CSS/Selectors#Combinators
reCSSCombinators: /[ >+~]/,
Expand Down Expand Up @@ -224,7 +224,7 @@ vAPI.DOMFilterer.prototype = {
selectors;
if ( selectorsStr.length === 0 ) { return; }

vAPI.userStylesheet.add(selectorsStr + '\n{ ' + declarations + ' }');
vAPI.userStylesheet.add(selectorsStr + '\n{' + declarations + '}');
this.commit();
this.triggerListeners('declarative', selectorsStr);

Expand Down Expand Up @@ -353,9 +353,9 @@ vAPI.DOMFilterer.prototype = {
attr.length !== 0 &&
attr.charCodeAt(attr.length - 1) !== 0x3B /* ';' */
) {
attr += '; ';
attr += ';';
}
node.setAttribute('style', attr + 'display: none !important;');
node.setAttribute('style', attr + 'display:none!important;');
}
this.hiddenNodesetToProcess.clear();
},
Expand Down Expand Up @@ -384,7 +384,7 @@ vAPI.DOMFilterer.prototype = {
if ( this.hideNodeStylesheet === false ) {
this.hideNodeStylesheet = true;
vAPI.userStylesheet.add(
'[' + this.hideNodeAttr + ']\n{ display: none !important; }'
'[' + this.hideNodeAttr + ']\n{display:none!important;}'
);
}
},
Expand Down
20 changes: 15 additions & 5 deletions platform/webext/vapi-usercss.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ vAPI.DOMFilterer.prototype = {
var userStylesheet = vAPI.userStylesheet,
addedSelectors = [];
for ( var entry of this.addedCSSRules ) {
if ( this.disabled === false && entry.lazy ) {
if (
this.disabled === false &&
entry.lazy &&
entry.injected === false
) {
userStylesheet.add(
entry.selectors + '\n{' + entry.declarations + '}'
);
Expand Down Expand Up @@ -114,15 +118,21 @@ vAPI.DOMFilterer.prototype = {
? selectors.join(',\n')
: selectors;
if ( selectorsStr.length === 0 ) { return; }
if ( details === undefined ) { details = {}; }
var entry = {
selectors: selectorsStr,
declarations,
lazy: details !== undefined && details.lazy === true,
internal: details && details.internal === true
lazy: details.lazy === true,
internal: details.internal === true,
injected: details.injected === true
};
this.addedCSSRules.add(entry);
this.filterset.add(entry);
if ( this.disabled === false && entry.lazy !== true ) {
if (
this.disabled === false &&
entry.lazy !== true &&
entry.injected !== true
) {
vAPI.userStylesheet.add(selectorsStr + '\n{' + declarations + '}');
}
this.commit();
Expand Down Expand Up @@ -162,7 +172,7 @@ vAPI.DOMFilterer.prototype = {
this.hideNodeStylesheet = true;
this.addCSSRule(
'[' + this.hideNodeId + ']',
'display: none !important;',
'display:none!important;',
{ internal: true }
);
}
Expand Down
71 changes: 40 additions & 31 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ vAPI.DOMFilterer = (function() {
]);
}
this.raw = o.raw;
this.cost = 0;
this.selector = o.selector;
this.tasks = [];
var tasks = o.tasks;
Expand Down Expand Up @@ -520,7 +521,7 @@ vAPI.DOMFilterer = (function() {
if ( o.pseudoclass ) {
this.domFilterer.addCSSRule(
o.raw,
'display: none !important;'
'display:none!important;'
);
mustCommit = true;
continue;
Expand Down Expand Up @@ -575,16 +576,24 @@ vAPI.DOMFilterer = (function() {

this.addedNodes = this.removedNodes = false;

var afterResultset = new Set();
var afterResultset = new Set(),
t0 = Date.now(), t1, pselector;

for ( entry of this.selectors ) {
nodes = entry[1].exec();
pselector = entry[1];
if ( pselector.cost > 100 ) { continue; }
nodes = pselector.exec();
i = nodes.length;
while ( i-- ) {
node = nodes[i];
this.domFilterer.hideNode(node);
afterResultset.add(node);
}
t1 = Date.now();
pselector.cost += t1 - t0;
t0 = t1;
// TODO: Consider adding logging ability to report disabled
// precedural filter in the logger.
}
if ( afterResultset.size !== currentResultset.size ) {
this.addedNodesHandlerMissCount = 0;
Expand Down Expand Up @@ -982,7 +991,7 @@ vAPI.domSurveyor = (function() {
if ( Array.isArray(selectors) && selectors.length !== 0 ) {
domFilterer.addCSSRule(
selectors,
'display: none !important;',
'display:none!important;',
{ type: 'simple' }
);
mustCommit = true;
Expand All @@ -991,7 +1000,7 @@ vAPI.domSurveyor = (function() {
if ( Array.isArray(selectors) && selectors.length !== 0 ) {
domFilterer.addCSSRule(
selectors,
'display: none !important;',
'display:none!important;',
{ type: 'complex' }
);
mustCommit = true;
Expand Down Expand Up @@ -1265,6 +1274,8 @@ vAPI.domSurveyor = (function() {
return;
}

var injected = cfeDetails.rulesInjected === true;

if ( response.noCosmeticFiltering ) {
vAPI.domFilterer = null;
vAPI.domSurveyor = null;
Expand All @@ -1276,47 +1287,45 @@ vAPI.domSurveyor = (function() {
domFilterer.exceptions = cfeDetails.exceptionFilters;
domFilterer.addCSSRule(
cfeDetails.declarativeFilters,
'display: none !important;'
'display:none!important;',
{ injected: injected }
);
domFilterer.addCSSRule(
cfeDetails.highGenericHideSimple,
'display: none !important;',
{ type: 'simple', lazy: true }
'display:none!important;',
{ type: 'simple', lazy: true, injected: injected }
);
domFilterer.addCSSRule(
cfeDetails.highGenericHideComplex,
'display: none !important;',
{ type: 'complex', lazy: true }
'display:none!important;',
{ type: 'complex', lazy: true, injected: injected }
);
domFilterer.addProceduralSelectors(cfeDetails.proceduralFilters);
}

if ( cfeDetails.netFilters.length !== 0 ) {
if ( cfeDetails.netFilters.length !== 0 && injected !== true ) {
vAPI.userStylesheet.add(
cfeDetails.netFilters + '\n{ display: none !important; }');
cfeDetails.netFilters + '\n{display:none!important;}');
}

vAPI.userStylesheet.apply();

var parent = document.head || document.documentElement;
if ( parent ) {
// Library of resources is located at:
// https://github.com/gorhill/uBlock/blob/master/assets/ublock/resources.txt
if ( cfeDetails.scripts ) {
// Have the injected script tag remove itself when execution completes:
// to keep DOM as clean as possible.
var text = cfeDetails.scripts +
"\n" +
"(function() {\n" +
" var c = document.currentScript,\n" +
" p = c && c.parentNode;\n" +
" if ( p ) {\n" +
" p.removeChild(c);\n" +
" }\n" +
"})();";
vAPI.injectScriptlet(document, text);
vAPI.injectedScripts = text;
}
// Library of resources is located at:
// https://github.com/gorhill/uBlock/blob/master/assets/ublock/resources.txt
if ( cfeDetails.scripts ) {
// Have the injected script tag remove itself when execution completes:
// to keep DOM as clean as possible.
var text = cfeDetails.scripts +
"\n" +
"(function() {\n" +
" var c = document.currentScript,\n" +
" p = c && c.parentNode;\n" +
" if ( p ) {\n" +
" p.removeChild(c);\n" +
" }\n" +
"})();";
vAPI.injectScriptlet(document, text);
vAPI.injectedScripts = text;
}

// https://github.com/chrisaljoudi/uBlock/issues/587
Expand Down
68 changes: 65 additions & 3 deletions src/js/cosmetic-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ var FilterContainer = function() {
this.netSelectorCacheCountMax = netSelectorCacheHighWaterMark;
this.selectorCacheTimer = null;

this.supportsUserStylesheets = vAPI.supportsUserStylesheets;

// generic exception filters
this.genericDonthideSet = new Set();

Expand Down Expand Up @@ -1255,8 +1257,8 @@ FilterContainer.prototype.compileGenericUnhideSelector = function(parsed, writer
if ( compiled === undefined ) { return; }

// https://github.com/chrisaljoudi/uBlock/issues/497
// All generic exception filters are put in the same bucket: they are
// expected to be very rare.
// All generic exception filters are put in the same bucket: they are
// expected to be very rare.
writer.push([ 7 /* g1 */, compiled ]);
};

Expand Down Expand Up @@ -1964,6 +1966,26 @@ FilterContainer.prototype.retrieveGenericSelectors = function(request) {

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

FilterContainer.prototype.injectHideRules = function(
tabId,
frameId,
selectors,
runAt
) {
var details = {
code: '',
cssOrigin: 'user',
frameId: frameId,
runAt: runAt
};
for ( var selector of selectors ) {
details.code = selector + '\n{display:none!important;}';
vAPI.insertCSS(tabId, details);
}
};

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

FilterContainer.prototype.retrieveDomainSelectors = function(
request,
sender,
Expand All @@ -1973,6 +1995,9 @@ FilterContainer.prototype.retrieveDomainSelectors = function(

console.time('cosmeticFilteringEngine.retrieveDomainSelectors');

// TODO: consider using MRUCache to quickly lookup all the previously
// looked-up data.

var hostname = this.µburi.hostnameFromURI(request.locationURL),
domain = this.µburi.domainFromHostname(hostname) || hostname,
pos = domain.indexOf('.'),
Expand All @@ -1998,7 +2023,7 @@ FilterContainer.prototype.retrieveDomainSelectors = function(
netFilters: '',
proceduralFilters: [],
scripts: undefined,
cssRulesInjected: false
rulesInjected: false
};

if ( options.noCosmeticFiltering !== true ) {
Expand Down Expand Up @@ -2027,6 +2052,22 @@ FilterContainer.prototype.retrieveDomainSelectors = function(
if ( (bucket = this.specificFilters.get('!' + this.noDomainHash)) ) {
bucket.retrieve(hostname, exceptionSet);
}

// Specific exception procedural cosmetic filters.
if ( (bucket = this.proceduralFilters.get('!' + domainHash)) ) {
bucket.retrieve(hostname, exceptionSet);
}
// Specific entity-based exception procedural cosmetic filters.
if ( entityHash !== undefined ) {
if ( (bucket = this.proceduralFilters.get('!' + entityHash)) ) {
bucket.retrieve(entity, exceptionSet);
}
}
// Special bucket for those filters without a valid
// domain name as per PSL.
if ( (bucket = this.proceduralFilters.get('!' + this.noDomainHash)) ) {
bucket.retrieve(hostname, exceptionSet);
}
if ( exceptionSet.size !== 0 ) {
r.exceptionFilters = µb.arrayFrom(exceptionSet);
}
Expand Down Expand Up @@ -2126,6 +2167,27 @@ FilterContainer.prototype.retrieveDomainSelectors = function(
r.netFilters = netFilters.join(',\n');
}

if (
this.supportsUserStylesheets &&
sender instanceof Object &&
sender.tab instanceof Object &&
typeof sender.frameId === 'number'
) {
this.injectHideRules(
sender.tab.id,
sender.frameId,
[ r.declarativeFilters.join(',\n'), r.netFilters ],
'document_start'
);
this.injectHideRules(
sender.tab.id,
sender.frameId,
[ r.highGenericHideSimple, r.highGenericHideComplex ],
'document_end'
);
r.rulesInjected = true;
}

console.timeEnd('cosmeticFilteringEngine.retrieveDomainSelectors');

return r;
Expand Down
Loading

0 comments on commit 6e18829

Please sign in to comment.