1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-18 08:52:26 +02:00
This commit is contained in:
Raymond Hill 2017-12-17 08:09:47 -05:00
parent f753952adc
commit dec0b80a72
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 71 additions and 50 deletions

View File

@ -121,7 +121,7 @@ var µBlock = (function() { // jshint ignore:line
// read-only // read-only
systemSettings: { systemSettings: {
compiledMagic: 'vrgorlgelgws', compiledMagic: 'vrgorlgelgws',
selfieMagic: 'vrgorlgelgws' selfieMagic: 'pxpclstriajk'
}, },
restoreBackupSettings: { restoreBackupSettings: {

View File

@ -673,6 +673,7 @@ var FilterContainer = function() {
this.setRegister0 = new Set(); this.setRegister0 = new Set();
this.setRegister1 = new Set(); this.setRegister1 = new Set();
this.setRegister2 = new Set(); this.setRegister2 = new Set();
this.mapRegister0 = new Map();
this.reset(); this.reset();
}; };
@ -722,7 +723,6 @@ FilterContainer.prototype.reset = function() {
this.scriptTagFilters = {}; this.scriptTagFilters = {};
this.scriptTagFilterCount = 0; this.scriptTagFilterCount = 0;
this.userScripts.clear(); this.userScripts.clear();
this.userScriptCount = 0;
}; };
/******************************************************************************/ /******************************************************************************/
@ -1603,8 +1603,12 @@ FilterContainer.prototype.retrieveScriptTagRegex = function(domain, hostname) {
// userScripts{hash} => FilterHostname | FilterBucket // userScripts{hash} => FilterHostname | FilterBucket
FilterContainer.prototype.createUserScriptRule = function(hash, hostname, selector) { FilterContainer.prototype.createUserScriptRule = function(
var filter = new FilterHostname(selector, hostname); hash,
hostname,
selector
) {
var filter = new FilterHostname(selector.slice(14, -1).trim(), hostname);
var bucket = this.userScripts.get(hash); var bucket = this.userScripts.get(hash);
if ( bucket === undefined ) { if ( bucket === undefined ) {
this.userScripts.set(hash, filter); this.userScripts.set(hash, filter);
@ -1613,7 +1617,6 @@ FilterContainer.prototype.createUserScriptRule = function(hash, hostname, select
} else { } else {
this.userScripts.set(hash, new FilterBucket(bucket, filter)); this.userScripts.set(hash, new FilterBucket(bucket, filter));
} }
this.userScriptCount += 1;
}; };
/******************************************************************************/ /******************************************************************************/
@ -1625,29 +1628,34 @@ FilterContainer.prototype.createUserScriptRule = function(hash, hostname, select
// ^ ^ // ^ ^
// 14 -1 // 14 -1
FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) { FilterContainer.prototype.retrieveUserScripts = function(
if ( this.userScriptCount === 0 ) { return; } domain,
hostname,
details
) {
if ( this.userScripts.size === 0 ) { return; }
if ( µb.hiddenSettings.ignoreScriptInjectFilters === true ) { return; } if ( µb.hiddenSettings.ignoreScriptInjectFilters === true ) { return; }
var reng = µb.redirectEngine; var reng = µb.redirectEngine;
if ( !reng ) { return; } if ( !reng ) { return; }
var out = [], this.mapRegister0.clear();
scripts = new Map(),
var toInject = this.mapRegister0,
pos = domain.indexOf('.'), pos = domain.indexOf('.'),
entity = pos !== -1 ? domain.slice(0, pos) + '.*' : ''; entity = pos !== -1 ? domain.slice(0, pos) + '.*' : '';
// Implicit // Implicit
var hn = hostname; var hn = hostname;
for (;;) { for (;;) {
this._lookupUserScript(scripts, hn + '.js', reng, out); this._lookupUserScript(hn + '.js', reng, toInject);
if ( hn === domain ) { break; } if ( hn === domain ) { break; }
pos = hn.indexOf('.'); pos = hn.indexOf('.');
if ( pos === -1 ) { break; } if ( pos === -1 ) { break; }
hn = hn.slice(pos + 1); hn = hn.slice(pos + 1);
} }
if ( entity !== '' ) { if ( entity !== '' ) {
this._lookupUserScript(scripts, entity + '.js', reng, out); this._lookupUserScript(entity + '.js', reng, toInject);
} }
// Explicit (hash is domain). // Explicit (hash is domain).
@ -1660,12 +1668,10 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
bucket.retrieve(entity, selectors); bucket.retrieve(entity, selectors);
} }
for ( var selector of selectors ) { for ( var selector of selectors ) {
this._lookupUserScript(scripts, selector.slice(14, -1).trim(), reng, out); this._lookupUserScript(selector, reng, toInject);
} }
if ( out.length === 0 ) { if ( toInject.size === 0 ) { return; }
return;
}
// https://github.com/gorhill/uBlock/issues/2835 // https://github.com/gorhill/uBlock/issues/2835
// Do not inject scriptlets if the site is under an `allow` rule. // Do not inject scriptlets if the site is under an `allow` rule.
@ -1678,26 +1684,43 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
// Exceptions should be rare, so we check for exception only if there are // Exceptions should be rare, so we check for exception only if there are
// scriptlets returned. // scriptlets returned.
var exceptions = new Set(), var exceptions = new Set();
j, token;
if ( (bucket = this.userScripts.get('!' + domain)) ) { if ( (bucket = this.userScripts.get('!' + domain)) ) {
bucket.retrieve(hostname, exceptions); bucket.retrieve(hostname, exceptions);
} }
if ( entity !== '' && (bucket = this.userScripts.get('!' + entity)) ) { if ( entity !== '' && (bucket = this.userScripts.get('!' + entity)) ) {
bucket.retrieve(hostname, exceptions); bucket.retrieve(hostname, exceptions);
} }
for ( var exception of exceptions ) {
token = exception.slice(14, -1); // Return an array of scriptlets, and log results if needed.
if ( (j = scripts.get(token)) !== undefined ) { var out = [],
out[j] = '// User script "' + token + '" excepted.\n'; logger = µb.logger.isEnabled() ? µb.logger : null,
isException;
for ( var entry of toInject ) {
if ( (isException = exceptions.has(entry[0])) === false ) {
out.push(entry[1]);
} }
if ( logger === null ) { continue; }
logger.writeOne(
details.tabId,
'cosmetic',
{
source: 'cosmetic',
raw: (isException ? '#@#' : '##') + 'script:inject(' + entry[0] + ')'
},
'dom',
details.locationURL,
null,
hostname
);
} }
return out.join('\n'); return out.join('\n');
}; };
FilterContainer.prototype._lookupUserScript = function(dict, raw, reng, out) { FilterContainer.prototype._lookupUserScript = function(raw, reng, toInject) {
if ( dict.has(raw) ) { return; } if ( toInject.has(raw) ) { return; }
var token, args, var token, args,
pos = raw.indexOf(','); pos = raw.indexOf(',');
if ( pos === -1 ) { if ( pos === -1 ) {
@ -1712,8 +1735,7 @@ FilterContainer.prototype._lookupUserScript = function(dict, raw, reng, out) {
content = this._fillupUserScript(content, args); content = this._fillupUserScript(content, args);
if ( !content ) { return; } if ( !content ) { return; }
} }
dict.set(raw, out.length); toInject.set(raw, content);
out.push(content);
}; };
// Fill template placeholders. Return falsy if: // Fill template placeholders. Return falsy if:
@ -1762,8 +1784,7 @@ FilterContainer.prototype.toSelfie = function() {
genericDonthideArray: µb.arrayFrom(this.genericDonthideSet), genericDonthideArray: µb.arrayFrom(this.genericDonthideSet),
scriptTagFilters: this.scriptTagFilters, scriptTagFilters: this.scriptTagFilters,
scriptTagFilterCount: this.scriptTagFilterCount, scriptTagFilterCount: this.scriptTagFilterCount,
userScripts: selfieFromMap(this.userScripts), userScripts: selfieFromMap(this.userScripts)
userScriptCount: this.userScriptCount
}; };
}; };
@ -1798,7 +1819,6 @@ FilterContainer.prototype.fromSelfie = function(selfie) {
this.scriptTagFilters = selfie.scriptTagFilters; this.scriptTagFilters = selfie.scriptTagFilters;
this.scriptTagFilterCount = selfie.scriptTagFilterCount; this.scriptTagFilterCount = selfie.scriptTagFilterCount;
this.userScripts = mapFromSelfie(selfie.userScripts); this.userScripts = mapFromSelfie(selfie.userScripts);
this.userScriptCount = selfie.userScriptCount;
this.frozen = true; this.frozen = true;
}; };
@ -1911,10 +1931,7 @@ FilterContainer.prototype.randomAlphaToken = function() {
/******************************************************************************/ /******************************************************************************/
FilterContainer.prototype.retrieveGenericSelectors = function( FilterContainer.prototype.retrieveGenericSelectors = function(request) {
request,
sender
) {
if ( this.acceptedCount === 0 ) { return; } if ( this.acceptedCount === 0 ) { return; }
if ( !request.ids && !request.classes ) { return; } if ( !request.ids && !request.classes ) { return; }
@ -1995,9 +2012,8 @@ FilterContainer.prototype.retrieveGenericSelectors = function(
// cosmetic filters now. // cosmetic filters now.
if ( if (
this.supportsUserStylesheets && this.supportsUserStylesheets &&
sender instanceof Object && request.tabId !== undefined &&
sender.tab instanceof Object && request.frameId !== undefined
typeof sender.frameId === 'number'
) { ) {
var injected = []; var injected = [];
if ( out.simple.length !== 0 ) { if ( out.simple.length !== 0 ) {
@ -2009,10 +2025,10 @@ FilterContainer.prototype.retrieveGenericSelectors = function(
out.complex = []; out.complex = [];
} }
out.injected = injected.join(',\n'); out.injected = injected.join(',\n');
vAPI.insertCSS(sender.tab.id, { vAPI.insertCSS(request.tabId, {
code: out.injected + '\n{display:none!important;}', code: out.injected + '\n{display:none!important;}',
cssOrigin: 'user', cssOrigin: 'user',
frameId: sender.frameId, frameId: request.frameId,
runAt: 'document_start' runAt: 'document_start'
}); });
} }
@ -2030,7 +2046,6 @@ FilterContainer.prototype.retrieveGenericSelectors = function(
FilterContainer.prototype.retrieveDomainSelectors = function( FilterContainer.prototype.retrieveDomainSelectors = function(
request, request,
sender,
options options
) { ) {
if ( !request.locationURL ) { return; } if ( !request.locationURL ) { return; }
@ -2041,7 +2056,8 @@ FilterContainer.prototype.retrieveDomainSelectors = function(
domain = this.µburi.domainFromHostname(hostname) || hostname, domain = this.µburi.domainFromHostname(hostname) || hostname,
pos = domain.indexOf('.'), pos = domain.indexOf('.'),
entity = pos === -1 ? '' : domain.slice(0, pos - domain.length) + '.*', entity = pos === -1 ? '' : domain.slice(0, pos - domain.length) + '.*',
cacheEntry = this.selectorCache.get(hostname); cacheEntry = this.selectorCache.get(hostname),
entry;
// https://github.com/chrisaljoudi/uBlock/issues/587 // https://github.com/chrisaljoudi/uBlock/issues/587
// out.ready will tell the content script the cosmetic filtering engine is // out.ready will tell the content script the cosmetic filtering engine is
@ -2177,7 +2193,7 @@ FilterContainer.prototype.retrieveDomainSelectors = function(
if ( options.noGenericCosmeticFiltering !== true ) { if ( options.noGenericCosmeticFiltering !== true ) {
var exceptionHash = out.exceptionFilters.join(); var exceptionHash = out.exceptionFilters.join();
for ( var type in this.highlyGeneric ) { for ( var type in this.highlyGeneric ) {
var entry = this.highlyGeneric[type]; entry = this.highlyGeneric[type];
var str = entry.mru.lookup(exceptionHash); var str = entry.mru.lookup(exceptionHash);
if ( str === undefined ) { if ( str === undefined ) {
str = { s: entry.str }; str = { s: entry.str };
@ -2206,8 +2222,9 @@ FilterContainer.prototype.retrieveDomainSelectors = function(
} }
// Scriptlet injection. // Scriptlet injection.
out.scripts = this.retrieveUserScripts(domain, hostname); out.scripts = this.retrieveUserScripts(domain, hostname, request);
// CSS selectors for collapsible blocked elements
if ( cacheEntry ) { if ( cacheEntry ) {
var networkFilters = []; var networkFilters = [];
cacheEntry.retrieve('net', networkFilters); cacheEntry.retrieve('net', networkFilters);
@ -2219,9 +2236,8 @@ FilterContainer.prototype.retrieveDomainSelectors = function(
// cosmetic filters now. // cosmetic filters now.
if ( if (
this.supportsUserStylesheets && this.supportsUserStylesheets &&
sender instanceof Object && request.tabId !== undefined &&
sender.tab instanceof Object && request.frameId !== undefined
typeof sender.frameId === 'number'
) { ) {
var injectedHideFilters = []; var injectedHideFilters = [];
if ( out.declarativeFilters.length !== 0 ) { if ( out.declarativeFilters.length !== 0 ) {
@ -2244,16 +2260,16 @@ FilterContainer.prototype.retrieveDomainSelectors = function(
var details = { var details = {
code: '', code: '',
cssOrigin: 'user', cssOrigin: 'user',
frameId: sender.frameId, frameId: request.frameId,
runAt: 'document_start' runAt: 'document_start'
}; };
if ( out.injectedHideFilters.length !== 0 ) { if ( out.injectedHideFilters.length !== 0 ) {
details.code = out.injectedHideFilters + '\n{display:none!important;}'; details.code = out.injectedHideFilters + '\n{display:none!important;}';
vAPI.insertCSS(sender.tab.id, details); vAPI.insertCSS(request.tabId, details);
} }
if ( out.networkFilters.length !== 0 ) { if ( out.networkFilters.length !== 0 ) {
details.code = out.networkFilters + '\n{display:none!important;}'; details.code = out.networkFilters + '\n{display:none!important;}';
vAPI.insertCSS(sender.tab.id, details); vAPI.insertCSS(request.tabId, details);
out.networkFilters = ''; out.networkFilters = '';
} }
} }

View File

@ -464,11 +464,12 @@ var onMessage = function(request, sender, callback) {
// Sync // Sync
var µb = µBlock, var µb = µBlock,
response, response,
tabId, tabId, frameId,
pageStore; pageStore;
if ( sender && sender.tab ) { if ( sender && sender.tab ) {
tabId = sender.tab.id; tabId = sender.tab.id;
frameId = sender.frameId;
pageStore = µb.pageStoreFromTabId(tabId); pageStore = µb.pageStoreFromTabId(tabId);
} }
@ -497,9 +498,11 @@ var onMessage = function(request, sender, callback) {
noGenericCosmeticFiltering: noGenericCosmeticFiltering:
pageStore.noGenericCosmeticFiltering === true pageStore.noGenericCosmeticFiltering === true
}; };
request.tabId = tabId;
request.frameId = frameId;
response.specificCosmeticFilters = response.specificCosmeticFilters =
µb.cosmeticFilteringEngine µb.cosmeticFilteringEngine
.retrieveDomainSelectors(request, sender, response); .retrieveDomainSelectors(request, response);
if ( request.isRootFrame && µb.logger.isEnabled() ) { if ( request.isRootFrame && µb.logger.isEnabled() ) {
µb.logCosmeticFilters(tabId); µb.logCosmeticFilters(tabId);
} }
@ -508,9 +511,11 @@ var onMessage = function(request, sender, callback) {
case 'retrieveGenericCosmeticSelectors': case 'retrieveGenericCosmeticSelectors':
if ( pageStore && pageStore.getGenericCosmeticFilteringSwitch() ) { if ( pageStore && pageStore.getGenericCosmeticFilteringSwitch() ) {
request.tabId = tabId;
request.frameId = frameId;
response = { response = {
result: µb.cosmeticFilteringEngine result: µb.cosmeticFilteringEngine
.retrieveGenericSelectors(request, sender) .retrieveGenericSelectors(request)
}; };
} }
break; break;