mirror of
https://github.com/gorhill/uBlock.git
synced 2024-09-03 01:29:39 +02:00
fix #2132 + various minor code review
This commit is contained in:
parent
d62059ccc7
commit
d913eea1f6
@ -1338,9 +1338,9 @@ FilterContainer.prototype.createUserScriptRule = function(hash, hostname, select
|
|||||||
// https://github.com/gorhill/uBlock/issues/1954
|
// https://github.com/gorhill/uBlock/issues/1954
|
||||||
|
|
||||||
// 01234567890123456789
|
// 01234567890123456789
|
||||||
// script:inject(token)
|
// script:inject(token[, arg[, ...]])
|
||||||
// ^ ^
|
// ^ ^
|
||||||
// 14 -1
|
// 14 -1
|
||||||
|
|
||||||
FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
|
FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
|
||||||
if ( this.userScriptCount === 0 ) { return; }
|
if ( this.userScriptCount === 0 ) { return; }
|
||||||
@ -1377,7 +1377,7 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
|
|||||||
}
|
}
|
||||||
var i = selectors.length;
|
var i = selectors.length;
|
||||||
while ( i-- ) {
|
while ( i-- ) {
|
||||||
this._lookupUserScript(scripts, selectors[i].slice(14, -1), reng, out);
|
this._lookupUserScript(scripts, selectors[i].slice(14, -1).trim(), reng, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( out.length === 0 ) {
|
if ( out.length === 0 ) {
|
||||||
@ -1404,13 +1404,32 @@ FilterContainer.prototype.retrieveUserScripts = function(domain, hostname) {
|
|||||||
return out.join('\n');
|
return out.join('\n');
|
||||||
};
|
};
|
||||||
|
|
||||||
FilterContainer.prototype._lookupUserScript = function(dict, token, reng, out) {
|
/******************************************************************************/
|
||||||
if ( dict.has(token) ) { return; }
|
|
||||||
var content = reng.resourceContentFromName(token, 'application/javascript');
|
FilterContainer.prototype._lookupUserScript = function(dict, raw, reng, out) {
|
||||||
if ( content ) {
|
if ( dict.has(raw) ) { return; }
|
||||||
dict.set(token, out.length);
|
var token, args,
|
||||||
out.push(content);
|
pos = raw.indexOf(',');
|
||||||
|
if ( pos === -1 ) {
|
||||||
|
token = raw;
|
||||||
|
} else {
|
||||||
|
token = raw.slice(0, pos).trim();
|
||||||
|
args = raw.slice(pos + 1).trim();
|
||||||
}
|
}
|
||||||
|
var content = reng.resourceContentFromName(token, 'application/javascript');
|
||||||
|
if ( !content ) { return; }
|
||||||
|
if ( args ) {
|
||||||
|
var i = 1;
|
||||||
|
while ( args !== '' ) {
|
||||||
|
pos = args.indexOf(',');
|
||||||
|
if ( pos === -1 ) { pos = args.length; }
|
||||||
|
content = content.replace('{{' + i + '}}', args.slice(0, pos).trim());
|
||||||
|
args = args.slice(pos + 1).trim();
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dict.set(raw, out.length);
|
||||||
|
out.push(content);
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
@ -481,22 +481,21 @@ var filterRequests = function(pageStore, details) {
|
|||||||
|
|
||||||
//console.debug('messaging.js/contentscript-end.js: processing %d requests', requests.length);
|
//console.debug('messaging.js/contentscript-end.js: processing %d requests', requests.length);
|
||||||
|
|
||||||
var hostnameFromURI = µb.URI.hostnameFromURI;
|
var hostnameFromURI = µb.URI.hostnameFromURI,
|
||||||
var redirectEngine = µb.redirectEngine;
|
redirectEngine = µb.redirectEngine,
|
||||||
var punycodeURL = vAPI.punycodeURL;
|
punycodeURL = vAPI.punycodeURL;
|
||||||
|
|
||||||
// Create evaluation context
|
// Create evaluation context
|
||||||
var context = pageStore.createContextFromFrameHostname(details.pageHostname);
|
var context = pageStore.createContextFromFrameHostname(details.pageHostname),
|
||||||
|
request, r,
|
||||||
var request, r;
|
i = requests.length;
|
||||||
var i = requests.length;
|
|
||||||
while ( i-- ) {
|
while ( i-- ) {
|
||||||
request = requests[i];
|
request = requests[i];
|
||||||
context.requestURL = punycodeURL(request.url);
|
context.requestURL = punycodeURL(request.url);
|
||||||
context.requestHostname = hostnameFromURI(context.requestURL);
|
context.requestHostname = hostnameFromURI(context.requestURL);
|
||||||
context.requestType = tagNameToRequestTypeMap[request.tag];
|
context.requestType = tagNameToRequestTypeMap[request.tag];
|
||||||
r = pageStore.filterRequest(context);
|
r = pageStore.filterRequest(context);
|
||||||
if ( typeof r !== 'string' || r.charAt(1) !== 'b' ) {
|
if ( typeof r !== 'string' || r.charCodeAt(1) !== 98 /* 'b' */ ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Redirected? (We do not hide redirected resources.)
|
// Redirected? (We do not hide redirected resources.)
|
||||||
|
@ -499,22 +499,14 @@ PageStore.prototype.toggleNetFilteringSwitch = function(url, scope, state) {
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
PageStore.prototype.logLargeMedia = (function() {
|
PageStore.prototype.injectLargeMediaElementScriptlet = function() {
|
||||||
var injectScript = function() {
|
this.largeMediaTimer = null;
|
||||||
this.largeMediaTimer = null;
|
µb.scriptlets.injectDeep(
|
||||||
µb.scriptlets.injectDeep(
|
this.tabId,
|
||||||
this.tabId,
|
'load-large-media-interactive'
|
||||||
'load-large-media-interactive'
|
);
|
||||||
);
|
µb.contextMenu.update(this.tabId);
|
||||||
µb.contextMenu.update(this.tabId);
|
};
|
||||||
};
|
|
||||||
return function() {
|
|
||||||
this.largeMediaCount += 1;
|
|
||||||
if ( this.largeMediaTimer === null ) {
|
|
||||||
this.largeMediaTimer = vAPI.setTimeout(injectScript.bind(this), 500);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
})();
|
|
||||||
|
|
||||||
PageStore.prototype.temporarilyAllowLargeMediaElements = function() {
|
PageStore.prototype.temporarilyAllowLargeMediaElements = function() {
|
||||||
this.largeMediaCount = 0;
|
this.largeMediaCount = 0;
|
||||||
@ -617,7 +609,7 @@ PageStore.prototype.filterRequest = function(context) {
|
|||||||
// We want to short-term cache filtering results of collapsible types,
|
// We want to short-term cache filtering results of collapsible types,
|
||||||
// because they are likely to be reused, from network request handler and
|
// because they are likely to be reused, from network request handler and
|
||||||
// from content script handler.
|
// from content script handler.
|
||||||
if ( 'image sub_frame object'.indexOf(requestType) === -1 ) {
|
if ( 'image media object sub_frame'.indexOf(requestType) === -1 ) {
|
||||||
return this.filterRequestNoCache(context);
|
return this.filterRequestNoCache(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -628,17 +620,12 @@ PageStore.prototype.filterRequest = function(context) {
|
|||||||
|
|
||||||
var entry = this.netFilteringCache.lookup(context);
|
var entry = this.netFilteringCache.lookup(context);
|
||||||
if ( entry !== undefined ) {
|
if ( entry !== undefined ) {
|
||||||
//console.debug('cache HIT: PageStore.filterRequest("%s")', context.requestURL);
|
|
||||||
return entry.result;
|
return entry.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
var result = '';
|
|
||||||
|
|
||||||
// Dynamic URL filtering.
|
// Dynamic URL filtering.
|
||||||
if ( result === '' ) {
|
µb.sessionURLFiltering.evaluateZ(context.rootHostname, context.requestURL, requestType);
|
||||||
µb.sessionURLFiltering.evaluateZ(context.rootHostname, context.requestURL, requestType);
|
var result = µb.sessionURLFiltering.toFilterString();
|
||||||
result = µb.sessionURLFiltering.toFilterString();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dynamic hostname/type filtering.
|
// Dynamic hostname/type filtering.
|
||||||
if ( result === '' && µb.userSettings.advancedUserEnabled ) {
|
if ( result === '' && µb.userSettings.advancedUserEnabled ) {
|
||||||
@ -648,14 +635,13 @@ PageStore.prototype.filterRequest = function(context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Static filtering has lowest precedence.
|
// Static filtering: lowest filtering precedence.
|
||||||
if ( result === '' || result.charAt(1) === 'n' ) {
|
if ( result === '' || result.charCodeAt(1) === 110 /* 'n' */ ) {
|
||||||
if ( µb.staticNetFilteringEngine.matchString(context) !== undefined ) {
|
if ( µb.staticNetFilteringEngine.matchString(context) !== undefined ) {
|
||||||
result = µb.staticNetFilteringEngine.toResultString(µb.logger.isEnabled());
|
result = µb.staticNetFilteringEngine.toResultString(µb.logger.isEnabled());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//console.debug('cache MISS: PageStore.filterRequest("%s")', context.requestURL);
|
|
||||||
this.netFilteringCache.add(context, result);
|
this.netFilteringCache.add(context, result);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@ -663,6 +649,32 @@ PageStore.prototype.filterRequest = function(context) {
|
|||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
// The caller is responsible to check whether filtering is enabled or not.
|
||||||
|
|
||||||
|
PageStore.prototype.filterLargeMediaElement = function(size) {
|
||||||
|
if ( Date.now() < this.allowLargeMediaElementsUntil ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ( µb.hnSwitches.evaluateZ('no-large-media', this.tabHostname) !== true ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ( (size >>> 10) < µb.userSettings.largeMediaSize ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.largeMediaCount += 1;
|
||||||
|
if ( this.largeMediaTimer === null ) {
|
||||||
|
this.largeMediaTimer = vAPI.setTimeout(
|
||||||
|
this.injectLargeMediaElementScriptlet.bind(this),
|
||||||
|
500
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return µb.hnSwitches.toResultString();
|
||||||
|
};
|
||||||
|
|
||||||
|
/******************************************************************************/
|
||||||
|
|
||||||
PageStore.prototype.filterRequestNoCache = function(context) {
|
PageStore.prototype.filterRequestNoCache = function(context) {
|
||||||
if ( this.getNetFilteringSwitch() === false ) {
|
if ( this.getNetFilteringSwitch() === false ) {
|
||||||
return '';
|
return '';
|
||||||
@ -697,7 +709,7 @@ PageStore.prototype.filterRequestNoCache = function(context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Static filtering has lowest precedence.
|
// Static filtering has lowest precedence.
|
||||||
if ( result === '' || result.charAt(1) === 'n' ) {
|
if ( result === '' || result.charCodeAt(1) === 110 /* 'n' */ ) {
|
||||||
if ( µb.staticNetFilteringEngine.matchString(context) !== undefined ) {
|
if ( µb.staticNetFilteringEngine.matchString(context) !== undefined ) {
|
||||||
result = µb.staticNetFilteringEngine.toResultString(µb.logger.isEnabled());
|
result = µb.staticNetFilteringEngine.toResultString(µb.logger.isEnabled());
|
||||||
}
|
}
|
||||||
|
@ -521,40 +521,29 @@ var processCSP = function(details, pageStore, context) {
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
// https://github.com/gorhill/uBlock/issues/1163
|
// https://github.com/gorhill/uBlock/issues/1163
|
||||||
// "Block elements by size"
|
// "Block elements by size"
|
||||||
|
|
||||||
var foilLargeMediaElement = function(details) {
|
var foilLargeMediaElement = function(details) {
|
||||||
var µb = µBlock;
|
var µb = µBlock;
|
||||||
var tabId = details.tabId;
|
|
||||||
var pageStore = µb.pageStoreFromTabId(tabId);
|
|
||||||
if ( pageStore === null ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ( pageStore.getNetFilteringSwitch() !== true ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ( Date.now() < pageStore.allowLargeMediaElementsUntil ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ( µb.hnSwitches.evaluateZ('no-large-media', pageStore.tabHostname) !== true ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var i = headerIndexFromName('content-length', details.responseHeaders);
|
var i = headerIndexFromName('content-length', details.responseHeaders);
|
||||||
if ( i === -1 ) {
|
if ( i === -1 ) { return; }
|
||||||
return;
|
|
||||||
}
|
var tabId = details.tabId,
|
||||||
var contentLength = parseInt(details.responseHeaders[i].value, 10) || 0;
|
pageStore = µb.pageStoreFromTabId(tabId);
|
||||||
if ( (contentLength >>> 10) < µb.userSettings.largeMediaSize ) {
|
if ( pageStore === null || pageStore.getNetFilteringSwitch() === false ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pageStore.logLargeMedia();
|
var size = parseInt(details.responseHeaders[i].value, 10) || 0,
|
||||||
|
result = pageStore.filterLargeMediaElement(size);
|
||||||
|
if ( result === undefined ) { return; }
|
||||||
|
|
||||||
if ( µb.logger.isEnabled() ) {
|
if ( µb.logger.isEnabled() ) {
|
||||||
µb.logger.writeOne(
|
µb.logger.writeOne(
|
||||||
tabId,
|
tabId,
|
||||||
'net',
|
'net',
|
||||||
µb.hnSwitches.toResultString(),
|
result,
|
||||||
details.type,
|
details.type,
|
||||||
details.url,
|
details.url,
|
||||||
pageStore.tabHostname,
|
pageStore.tabHostname,
|
||||||
|
@ -455,13 +455,13 @@ var matchBucket = function(url, hostname, bucket, start) {
|
|||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
µBlock.isBlockResult = function(result) {
|
µBlock.isBlockResult = function(result) {
|
||||||
return typeof result === 'string' && result.charAt(1) === 'b';
|
return typeof result === 'string' && result.charCodeAt(1) === 98 /* 'b' */;
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
µBlock.isAllowResult = function(result) {
|
µBlock.isAllowResult = function(result) {
|
||||||
return typeof result !== 'string' || result.charAt(1) !== 'b';
|
return typeof result !== 'string' || result.charCodeAt(1) !== 98 /* 'b' */;
|
||||||
};
|
};
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
Loading…
Reference in New Issue
Block a user