mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-23 02:42:41 +01:00
more refactoring of content script: better modularization of various components
This commit is contained in:
parent
01da277886
commit
6fd0bb4291
@ -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 = {
|
||||
start: function(){},
|
||||
stop: function(){}
|
||||
|
@ -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 = {
|
||||
start: function(){},
|
||||
stop: function(){}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1550,16 +1550,17 @@ FilterContainer.prototype.retrieveGenericSelectors = function(request) {
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
FilterContainer.prototype.retrieveDomainSelectors = function(request) {
|
||||
FilterContainer.prototype.retrieveDomainSelectors = function(request, noCosmeticFiltering) {
|
||||
if ( !request.locationURL ) {
|
||||
return;
|
||||
}
|
||||
|
||||
//quickProfiler.start('FilterContainer.retrieve()');
|
||||
|
||||
var hostname = this.µburi.hostnameFromURI(request.locationURL);
|
||||
var domain = this.µburi.domainFromHostname(hostname) || hostname;
|
||||
var pos = domain.indexOf('.');
|
||||
var hostname = this.µburi.hostnameFromURI(request.locationURL),
|
||||
domain = this.µburi.domainFromHostname(hostname) || hostname,
|
||||
pos = domain.indexOf('.'),
|
||||
cacheEntry = this.selectorCache[hostname];
|
||||
|
||||
// https://github.com/chrisaljoudi/uBlock/issues/587
|
||||
// r.ready will tell the content script the cosmetic filtering engine is
|
||||
@ -1572,58 +1573,59 @@ FilterContainer.prototype.retrieveDomainSelectors = function(request) {
|
||||
ready: this.frozen,
|
||||
domain: domain,
|
||||
entity: pos === -1 ? domain : domain.slice(0, pos - domain.length),
|
||||
skipCosmeticSurveying: false,
|
||||
skipCosmeticFiltering: this.acceptedCount === 0,
|
||||
noDOMSurveying: false,
|
||||
cosmeticHide: [],
|
||||
cosmeticDonthide: this.genericDonthide.slice(),
|
||||
cosmeticDonthide: [],
|
||||
netHide: [],
|
||||
netCollapse: µb.userSettings.collapseBlocked,
|
||||
scripts: this.retrieveScriptTags(domain, hostname)
|
||||
};
|
||||
|
||||
var hash, bucket;
|
||||
hash = makeHash(0, domain, this.domainHashMask);
|
||||
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
|
||||
if ( (bucket = this.hostnameFilters[this.type0NoDomainHash]) ) {
|
||||
bucket.retrieve(hostname, r.cosmeticHide);
|
||||
if ( !noCosmeticFiltering ) {
|
||||
var hash, bucket;
|
||||
hash = makeHash(0, domain, this.domainHashMask);
|
||||
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
|
||||
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
|
||||
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];
|
||||
// Collapsible blocked resources.
|
||||
if ( cacheEntry ) {
|
||||
cacheEntry.retrieve('cosmetic', r.cosmeticHide);
|
||||
cacheEntry.retrieve('net', r.netHide);
|
||||
r.skipCosmeticSurveying = cacheEntry.cosmeticSurveyingMissCount > cosmeticSurveyingMissCountMax;
|
||||
}
|
||||
|
||||
//quickProfiler.stop();
|
||||
|
||||
//console.log(
|
||||
// 'µBlock> abp-hide-filters.js: "%s" => %d selectors out',
|
||||
// request.locationURL,
|
||||
// r.cosmeticHide.length + r.cosmeticDonthide.length
|
||||
//);
|
||||
|
||||
return r;
|
||||
};
|
||||
|
||||
|
@ -518,12 +518,18 @@ var onMessage = function(request, sender, callback) {
|
||||
}
|
||||
|
||||
switch ( request.what ) {
|
||||
case 'retrieveDomainCosmeticSelectors':
|
||||
case 'retrieveContentScriptParameters':
|
||||
if ( pageStore && pageStore.getNetFilteringSwitch() ) {
|
||||
response = µb.cosmeticFilteringEngine.retrieveDomainSelectors(request);
|
||||
if ( response && response.skipCosmeticFiltering !== true ) {
|
||||
response.skipCosmeticFiltering = !pageStore.getSpecificCosmeticFilteringSwitch();
|
||||
}
|
||||
response = {
|
||||
loggerEnabled: µb.logger.isEnabled(),
|
||||
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;
|
||||
|
||||
|
@ -310,26 +310,42 @@ PageStore.prototype.init = function(tabId) {
|
||||
this.largeMediaTimer = null;
|
||||
this.netFilteringCache = NetFilteringResultCache.factory();
|
||||
|
||||
// Support `elemhide` filter option. Called at this point so the required
|
||||
// context is all setup at this point.
|
||||
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`.
|
||||
this.noCosmeticFiltering = µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) === true;
|
||||
if ( µb.logger.isEnabled() && this.noCosmeticFiltering ) {
|
||||
µb.logger.writeOne(
|
||||
tabId,
|
||||
'net',
|
||||
µb.staticNetFilteringEngine.toResultString(true),
|
||||
'elemhide',
|
||||
'cosmetic',
|
||||
µb.hnSwitches.toResultString(),
|
||||
'dom',
|
||||
tabContext.rawURL,
|
||||
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;
|
||||
};
|
||||
|
||||
@ -458,16 +474,14 @@ PageStore.prototype.getNetFilteringSwitch = function() {
|
||||
/******************************************************************************/
|
||||
|
||||
PageStore.prototype.getSpecificCosmeticFilteringSwitch = function() {
|
||||
var tabContext = µb.tabContextManager.lookup(this.tabId);
|
||||
return tabContext !== null &&
|
||||
µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) !== true;
|
||||
return this.noCosmeticFiltering !== true;
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
PageStore.prototype.getGenericCosmeticFilteringSwitch = function() {
|
||||
return this.skipCosmeticFiltering !== true &&
|
||||
this.getSpecificCosmeticFilteringSwitch();
|
||||
return this.noGenericCosmeticFiltering !== true &&
|
||||
this.noCosmeticFiltering !== true;
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -27,7 +27,7 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) {
|
||||
if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
/******************************************************************************/
|
||||
|
||||
(function() {
|
||||
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) {
|
||||
if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
/******************************************************************************/
|
||||
|
||||
(function() {
|
||||
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) {
|
||||
if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@
|
||||
/******************************************************************************/
|
||||
|
||||
(function() {
|
||||
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) {
|
||||
if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -28,7 +28,7 @@
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
if ( typeof vAPI !== 'object' || typeof vAPI.domFilterer !== 'object' ) {
|
||||
if ( typeof vAPI !== 'object' || !vAPI.domFilterer ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user