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

more refactoring of content script: better modularization of various components

This commit is contained in:
gorhill 2016-08-12 08:55:35 -04:00
parent 01da277886
commit 6fd0bb4291
11 changed files with 790 additions and 705 deletions

View File

@ -67,6 +67,23 @@ if ( vAPI.sessionId ) {
/******************************************************************************/ /******************************************************************************/
var referenceCounter = 0;
vAPI.lock = function() {
referenceCounter += 1;
};
vAPI.unlock = function() {
referenceCounter -= 1;
if ( referenceCounter === 0 ) {
// Eventually there will be code here to flush the javascript code
// from this file out of memory when it ends up unused.
}
};
/******************************************************************************/
vAPI.executionCost = { vAPI.executionCost = {
start: function(){}, start: function(){},
stop: function(){} stop: function(){}

View File

@ -56,6 +56,18 @@ var vAPI = self.vAPI = self.vAPI || {};
/******************************************************************************/ /******************************************************************************/
var referenceCounter = 0;
vAPI.lock = function() {
referenceCounter += 1;
};
vAPI.unlock = function() {
referenceCounter -= 1;
};
/******************************************************************************/
vAPI.executionCost = { vAPI.executionCost = {
start: function(){}, start: function(){},
stop: function(){} stop: function(){}

File diff suppressed because it is too large Load Diff

View File

@ -1550,16 +1550,17 @@ FilterContainer.prototype.retrieveGenericSelectors = function(request) {
/******************************************************************************/ /******************************************************************************/
FilterContainer.prototype.retrieveDomainSelectors = function(request) { FilterContainer.prototype.retrieveDomainSelectors = function(request, noCosmeticFiltering) {
if ( !request.locationURL ) { if ( !request.locationURL ) {
return; return;
} }
//quickProfiler.start('FilterContainer.retrieve()'); //quickProfiler.start('FilterContainer.retrieve()');
var hostname = this.µburi.hostnameFromURI(request.locationURL); var hostname = this.µburi.hostnameFromURI(request.locationURL),
var domain = this.µburi.domainFromHostname(hostname) || hostname; domain = this.µburi.domainFromHostname(hostname) || hostname,
var pos = domain.indexOf('.'); pos = domain.indexOf('.'),
cacheEntry = this.selectorCache[hostname];
// https://github.com/chrisaljoudi/uBlock/issues/587 // https://github.com/chrisaljoudi/uBlock/issues/587
// r.ready will tell the content script the cosmetic filtering engine is // r.ready will tell the content script the cosmetic filtering engine is
@ -1572,58 +1573,59 @@ FilterContainer.prototype.retrieveDomainSelectors = function(request) {
ready: this.frozen, ready: this.frozen,
domain: domain, domain: domain,
entity: pos === -1 ? domain : domain.slice(0, pos - domain.length), entity: pos === -1 ? domain : domain.slice(0, pos - domain.length),
skipCosmeticSurveying: false, noDOMSurveying: false,
skipCosmeticFiltering: this.acceptedCount === 0,
cosmeticHide: [], cosmeticHide: [],
cosmeticDonthide: this.genericDonthide.slice(), cosmeticDonthide: [],
netHide: [], netHide: [],
netCollapse: µb.userSettings.collapseBlocked,
scripts: this.retrieveScriptTags(domain, hostname) scripts: this.retrieveScriptTags(domain, hostname)
}; };
var hash, bucket; if ( !noCosmeticFiltering ) {
hash = makeHash(0, domain, this.domainHashMask); var hash, bucket;
if ( (bucket = this.hostnameFilters[hash]) ) { hash = makeHash(0, domain, this.domainHashMask);
bucket.retrieve(hostname, r.cosmeticHide); if ( (bucket = this.hostnameFilters[hash]) ) {
} bucket.retrieve(hostname, r.cosmeticHide);
// https://github.com/chrisaljoudi/uBlock/issues/188 }
// Special bucket for those filters without a valid domain name as per PSL // https://github.com/chrisaljoudi/uBlock/issues/188
if ( (bucket = this.hostnameFilters[this.type0NoDomainHash]) ) { // Special bucket for those filters without a valid domain name as per PSL
bucket.retrieve(hostname, r.cosmeticHide); if ( (bucket = this.hostnameFilters[this.type0NoDomainHash]) ) {
bucket.retrieve(hostname, r.cosmeticHide);
}
// entity filter buckets are always plain js array
if ( this.entityFilters.hasOwnProperty(r.entity) ) {
r.cosmeticHide = r.cosmeticHide.concat(this.entityFilters[r.entity]);
}
// cached cosmetic filters.
if ( cacheEntry ) {
cacheEntry.retrieve('cosmetic', r.cosmeticHide);
r.noDOMSurveying = cacheEntry.cosmeticSurveyingMissCount > cosmeticSurveyingMissCountMax;
}
// Exception cosmetic filters.
r.cosmeticDonthide = this.genericDonthide.slice();
hash = makeHash(1, domain, this.domainHashMask);
if ( (bucket = this.hostnameFilters[hash]) ) {
bucket.retrieve(hostname, r.cosmeticDonthide);
}
// https://github.com/chrisaljoudi/uBlock/issues/188
// Special bucket for those filters without a valid domain name as per PSL
if ( (bucket = this.hostnameFilters[this.type1NoDomainHash]) ) {
bucket.retrieve(hostname, r.cosmeticDonthide);
}
// No entity exceptions as of now
} }
// entity filter buckets are always plain js array // Collapsible blocked resources.
if ( this.entityFilters.hasOwnProperty(r.entity) ) {
r.cosmeticHide = r.cosmeticHide.concat(this.entityFilters[r.entity]);
}
// No entity exceptions as of now
hash = makeHash(1, domain, this.domainHashMask);
if ( (bucket = this.hostnameFilters[hash]) ) {
bucket.retrieve(hostname, r.cosmeticDonthide);
}
// https://github.com/chrisaljoudi/uBlock/issues/188
// Special bucket for those filters without a valid domain name as per PSL
if ( (bucket = this.hostnameFilters[this.type1NoDomainHash]) ) {
bucket.retrieve(hostname, r.cosmeticDonthide);
}
var cacheEntry = this.selectorCache[hostname];
if ( cacheEntry ) { if ( cacheEntry ) {
cacheEntry.retrieve('cosmetic', r.cosmeticHide);
cacheEntry.retrieve('net', r.netHide); cacheEntry.retrieve('net', r.netHide);
r.skipCosmeticSurveying = cacheEntry.cosmeticSurveyingMissCount > cosmeticSurveyingMissCountMax;
} }
//quickProfiler.stop(); //quickProfiler.stop();
//console.log(
// 'µBlock> abp-hide-filters.js: "%s" => %d selectors out',
// request.locationURL,
// r.cosmeticHide.length + r.cosmeticDonthide.length
//);
return r; return r;
}; };

View File

@ -518,12 +518,18 @@ var onMessage = function(request, sender, callback) {
} }
switch ( request.what ) { switch ( request.what ) {
case 'retrieveDomainCosmeticSelectors': case 'retrieveContentScriptParameters':
if ( pageStore && pageStore.getNetFilteringSwitch() ) { if ( pageStore && pageStore.getNetFilteringSwitch() ) {
response = µb.cosmeticFilteringEngine.retrieveDomainSelectors(request); response = {
if ( response && response.skipCosmeticFiltering !== true ) { loggerEnabled: µb.logger.isEnabled(),
response.skipCosmeticFiltering = !pageStore.getSpecificCosmeticFilteringSwitch(); collapseBlocked: µb.userSettings.collapseBlocked,
} noCosmeticFiltering: µb.cosmeticFilteringEngine.acceptedCount === 0 || pageStore.noCosmeticFiltering === true,
noGenericCosmeticFiltering: pageStore.noGenericCosmeticFiltering === true
};
response.specificCosmeticFilters = µb.cosmeticFilteringEngine.retrieveDomainSelectors(
request,
response.noCosmeticFiltering
);
} }
break; break;

View File

@ -310,26 +310,42 @@ PageStore.prototype.init = function(tabId) {
this.largeMediaTimer = null; this.largeMediaTimer = null;
this.netFilteringCache = NetFilteringResultCache.factory(); this.netFilteringCache = NetFilteringResultCache.factory();
// Support `elemhide` filter option. Called at this point so the required this.noCosmeticFiltering = µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) === true;
// context is all setup at this point. if ( µb.logger.isEnabled() && this.noCosmeticFiltering ) {
this.skipCosmeticFiltering = µb.staticNetFilteringEngine.matchStringExactType(
this.createContextFromPage(),
tabContext.normalURL,
'elemhide'
) === false;
if ( this.skipCosmeticFiltering && µb.logger.isEnabled() ) {
// https://github.com/gorhill/uBlock/issues/370
// Log using `cosmetic-filtering`, not `elemhide`.
µb.logger.writeOne( µb.logger.writeOne(
tabId, tabId,
'net', 'cosmetic',
µb.staticNetFilteringEngine.toResultString(true), µb.hnSwitches.toResultString(),
'elemhide', 'dom',
tabContext.rawURL, tabContext.rawURL,
this.tabHostname, this.tabHostname,
this.tabHostname this.tabHostname
); );
} }
// Support `generichide` filter option.
this.noGenericCosmeticFiltering = this.noCosmeticFiltering;
if ( this.noGenericCosmeticFiltering !== true ) {
this.noGenericCosmeticFiltering = µb.staticNetFilteringEngine.matchStringExactType(
this.createContextFromPage(),
tabContext.normalURL,
'elemhide'
) === false;
if ( µb.logger.isEnabled() && this.noGenericCosmeticFiltering ) {
// https://github.com/gorhill/uBlock/issues/370
// Log using `cosmetic-filtering`, not `elemhide`.
µb.logger.writeOne(
tabId,
'net',
µb.staticNetFilteringEngine.toResultString(true),
'elemhide',
tabContext.rawURL,
this.tabHostname,
this.tabHostname
);
}
}
return this; return this;
}; };
@ -458,16 +474,14 @@ PageStore.prototype.getNetFilteringSwitch = function() {
/******************************************************************************/ /******************************************************************************/
PageStore.prototype.getSpecificCosmeticFilteringSwitch = function() { PageStore.prototype.getSpecificCosmeticFilteringSwitch = function() {
var tabContext = µb.tabContextManager.lookup(this.tabId); return this.noCosmeticFiltering !== true;
return tabContext !== null &&
µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) !== true;
}; };
/******************************************************************************/ /******************************************************************************/
PageStore.prototype.getGenericCosmeticFilteringSwitch = function() { PageStore.prototype.getGenericCosmeticFilteringSwitch = function() {
return this.skipCosmeticFiltering !== true && return this.noGenericCosmeticFiltering !== true &&
this.getSpecificCosmeticFilteringSwitch(); this.noCosmeticFiltering !== true;
}; };
/******************************************************************************/ /******************************************************************************/

View File

@ -27,7 +27,7 @@
/******************************************************************************/ /******************************************************************************/
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) { if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
return; return;
} }

View File

@ -24,7 +24,7 @@
/******************************************************************************/ /******************************************************************************/
(function() { (function() {
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) { if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
return; return;
} }

View File

@ -24,7 +24,7 @@
/******************************************************************************/ /******************************************************************************/
(function() { (function() {
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) { if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
return; return;
} }

View File

@ -24,7 +24,7 @@
/******************************************************************************/ /******************************************************************************/
(function() { (function() {
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) { if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
return; return;
} }

View File

@ -28,7 +28,7 @@
/******************************************************************************/ /******************************************************************************/
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) { if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
return; return;
} }