1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-15 15:32:28 +02:00

Fine tune code related to click-to-load feature

The redirectable resource has been renamed
`click2load.html`, so as to avoid uses of dash
characters and to also allow for future different
click-to-load resources.
This commit is contained in:
Raymond Hill 2020-10-10 08:36:30 -04:00
parent c54fb03414
commit 2e5d32e967
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
8 changed files with 52 additions and 46 deletions

View File

@ -1063,6 +1063,10 @@
"message": "GB", "message": "GB",
"description": "short for 'gigabytes'" "description": "short for 'gigabytes'"
}, },
"clickToLoad": {
"message": "Click to load",
"description": "Message use in frame placeholders"
},
"dummy": { "dummy": {
"message": "This entry must be the last one", "message": "This entry must be the last one",
"description": "so we dont need to deal with comma for last entry" "description": "so we dont need to deal with comma for last entry"

View File

@ -44,6 +44,7 @@
this.tabOrigin = undefined; this.tabOrigin = undefined;
this.tabHostname = undefined; this.tabHostname = undefined;
this.tabDomain = undefined; this.tabDomain = undefined;
this.redirectURL = undefined;
this.filter = undefined; this.filter = undefined;
}; };
@ -104,6 +105,7 @@
} else { } else {
this.setDocOrigin(this.tabOrigin); this.setDocOrigin(this.tabOrigin);
} }
this.redirectURL = undefined;
this.filter = undefined; this.filter = undefined;
return this; return this;
}, },
@ -122,6 +124,7 @@
this.tabOrigin = other.tabOrigin; this.tabOrigin = other.tabOrigin;
this.tabHostname = other.tabHostname; this.tabHostname = other.tabHostname;
this.tabDomain = other.tabDomain; this.tabDomain = other.tabDomain;
this.redirectURL = other.redirectURL;
this.filter = undefined; this.filter = undefined;
return this; return this;
}, },

View File

@ -54,18 +54,21 @@ const NetFilteringResultCache = class {
return this; return this;
} }
// https://github.com/gorhill/uBlock/issues/3619
// Don't collapse redirected resources
rememberResult(fctxt, result) { rememberResult(fctxt, result) {
if ( fctxt.tabId <= 0 ) { return; } if ( fctxt.tabId <= 0 ) { return; }
if ( this.results.size === 0 ) { if ( this.results.size === 0 ) {
this.pruneAsync(); this.pruneAsync();
} }
const key = fctxt.getDocHostname() + ' ' + fctxt.type + ' ' + fctxt.url; const key = `${fctxt.getDocHostname()} ${fctxt.type} ${fctxt.url}`;
this.results.set(key, { this.results.set(key, {
result: result, result,
redirectURL: fctxt.redirectURL,
logData: fctxt.filter, logData: fctxt.filter,
tstamp: Date.now() tstamp: Date.now()
}); });
if ( result !== 1 ) { return; } if ( result !== 1 || fctxt.redirectURL !== undefined ) { return; }
const now = Date.now(); const now = Date.now();
this.blocked.set(key, now); this.blocked.set(key, now);
this.hash = now; this.hash = now;
@ -76,16 +79,17 @@ const NetFilteringResultCache = class {
if ( this.blocked.size === 0 ) { if ( this.blocked.size === 0 ) {
this.pruneAsync(); this.pruneAsync();
} }
if ( fctxt.redirectURL !== undefined ) { return; }
const now = Date.now(); const now = Date.now();
this.blocked.set( this.blocked.set(
fctxt.getDocHostname() + ' ' + fctxt.type + ' ' + fctxt.url, `${fctxt.getDocHostname()} ${fctxt.type} ${fctxt.url}`,
now now
); );
this.hash = now; this.hash = now;
} }
forgetResult(fctxt) { forgetResult(docHostname, type, url) {
const key = `${fctxt.getDocHostname()} ${fctxt.type} ${fctxt.url}`; const key = `${docHostname} ${type} ${url}`;
this.results.delete(key); this.results.delete(key);
this.blocked.delete(key); this.blocked.delete(key);
} }
@ -171,7 +175,7 @@ const FrameStore = class {
init(frameURL) { init(frameURL) {
this.t0 = Date.now(); this.t0 = Date.now();
this.exceptCname = undefined; this.exceptCname = undefined;
this.clickToLoad = 0; this.clickToLoad = false;
this.rawURL = frameURL; this.rawURL = frameURL;
if ( frameURL !== undefined ) { if ( frameURL !== undefined ) {
this.hostname = vAPI.hostnameFromURI(frameURL); this.hostname = vAPI.hostnameFromURI(frameURL);
@ -559,6 +563,7 @@ const PageStore = class {
if ( cacheableResult ) { if ( cacheableResult ) {
const entry = this.netFilteringCache.lookupResult(fctxt); const entry = this.netFilteringCache.lookupResult(fctxt);
if ( entry !== undefined ) { if ( entry !== undefined ) {
fctxt.redirectURL = entry.redirectURL;
fctxt.filter = entry.logData; fctxt.filter = entry.logData;
return entry.result; return entry.result;
} }
@ -607,11 +612,11 @@ const PageStore = class {
} }
} }
// Click-to-load: // Click-to-load?
// When frameId is not -1, the resource is always sub_frame. // When frameId is not -1, the resource is always sub_frame.
if ( result === 1 && fctxt.frameId !== -1 ) { if ( result === 1 && fctxt.frameId !== -1 ) {
const docStore = this.getFrameStore(fctxt.frameId); const frameStore = this.getFrameStore(fctxt.frameId);
if ( docStore !== null && docStore.clickToLoad !== 0 ) { if ( frameStore !== null && frameStore.clickToLoad ) {
result = 2; result = 2;
if ( µb.logger.enabled ) { if ( µb.logger.enabled ) {
fctxt.setFilter({ fctxt.setFilter({
@ -623,13 +628,20 @@ const PageStore = class {
} }
} }
// https://github.com/gorhill/uBlock/issues/949
// Redirect blocked request?
if ( result === 1 && µb.hiddenSettings.ignoreRedirectFilters !== true ) {
const redirectURL = µb.redirectEngine.toURL(fctxt);
if ( redirectURL !== undefined ) {
fctxt.redirectURL = redirectURL;
this.internalRedirectionCount += 1;
}
}
if ( cacheableResult ) { if ( cacheableResult ) {
this.netFilteringCache.rememberResult(fctxt, result); this.netFilteringCache.rememberResult(fctxt, result);
} else if ( } else if ( result === 1 && this.collapsibleResources.has(requestType) ) {
result === 1 && this.netFilteringCache.rememberBlock(fctxt);
this.collapsibleResources.has(requestType)
) {
this.netFilteringCache.rememberBlock(fctxt, true);
} }
return result; return result;
@ -727,7 +739,12 @@ const PageStore = class {
if ( frameStore === null ) { if ( frameStore === null ) {
frameStore = this.setFrameURL(frameId, frameURL); frameStore = this.setFrameURL(frameId, frameURL);
} }
frameStore.clickToLoad = Date.now(); this.netFilteringCache.forgetResult(
this.tabHostname,
'sub_frame',
frameURL
);
frameStore.clickToLoad = true;
} }
shouldExceptCname(fctxt) { shouldExceptCname(fctxt) {
@ -776,12 +793,9 @@ const PageStore = class {
// content script-side (i.e. `iframes` -- unlike `img`). // content script-side (i.e. `iframes` -- unlike `img`).
if ( Array.isArray(resources) && resources.length !== 0 ) { if ( Array.isArray(resources) && resources.length !== 0 ) {
for ( const resource of resources ) { for ( const resource of resources ) {
const result = this.filterRequest( this.filterRequest(
fctxt.setType(resource.type).setURL(resource.url) fctxt.setType(resource.type).setURL(resource.url)
); );
if ( result === 1 && µb.redirectEngine.toURL(fctxt) ) {
this.forgetBlockedResource(fctxt);
}
} }
} }
if ( this.netFilteringCache.hash === response.hash ) { return; } if ( this.netFilteringCache.hash === response.hash ) { return; }
@ -789,11 +803,6 @@ const PageStore = class {
response.blockedResources = response.blockedResources =
this.netFilteringCache.lookupAllBlocked(fctxt.getDocHostname()); this.netFilteringCache.lookupAllBlocked(fctxt.getDocHostname());
} }
forgetBlockedResource(fctxt) {
if ( this.collapsibleResources.has(fctxt.type) === false ) { return; }
this.netFilteringCache.forgetResult(fctxt);
}
}; };
PageStore.prototype.cacheableResults = new Set([ PageStore.prototype.cacheableResults = new Set([

View File

@ -67,8 +67,7 @@ const redirectableResources = new Map([
[ 'chartbeat.js', { [ 'chartbeat.js', {
alias: 'static.chartbeat.com/chartbeat.js', alias: 'static.chartbeat.com/chartbeat.js',
} ], } ],
[ 'click-to-load.html', { [ 'click2load.html', {
alias: 'clicktoload',
params: [ 'url' ], params: [ 'url' ],
} ], } ],
[ 'doubleclick_instream_ad_status.js', { [ 'doubleclick_instream_ad_status.js', {

View File

@ -111,25 +111,16 @@ const onBeforeRequest = function(details) {
// Blocked // Blocked
// https://github.com/gorhill/uBlock/issues/949 if ( fctxt.redirectURL === undefined ) {
// Redirect blocked request? return { cancel: true };
// https://github.com/gorhill/uBlock/issues/3619
// Don't collapse redirected resources
if ( µb.hiddenSettings.ignoreRedirectFilters !== true ) {
const url = µb.redirectEngine.toURL(fctxt);
if ( url !== undefined ) {
pageStore.internalRedirectionCount += 1;
pageStore.forgetBlockedResource(fctxt);
if ( µb.logger.enabled ) {
fctxt.setRealm('redirect')
.setFilter({ source: 'redirect', raw: µb.redirectEngine.resourceNameRegister })
.toLogger();
}
return { redirectUrl: url };
}
} }
return { cancel: true }; if ( µb.logger.enabled ) {
fctxt.setRealm('redirect')
.setFilter({ source: 'redirect', raw: µb.redirectEngine.resourceNameRegister })
.toLogger();
}
return { redirectUrl: fctxt.redirectURL };
}; };
/******************************************************************************/ /******************************************************************************/

View File

@ -6,7 +6,7 @@
<title>uBlock Origin Click-to-Load</title> <title>uBlock Origin Click-to-Load</title>
<link rel="stylesheet" href="../css/themes/default.css"> <link rel="stylesheet" href="../css/themes/default.css">
<link rel="stylesheet" href="../css/common.css"> <link rel="stylesheet" href="../css/common.css">
<link rel="stylesheet" href="../css/click-to-load.css"> <link rel="stylesheet" href="../css/click2load.css">
</head> </head>
<body> <body>
@ -19,7 +19,7 @@
<script src="../js/vapi-common.js"></script> <script src="../js/vapi-common.js"></script>
<script src="../js/vapi-client.js"></script> <script src="../js/vapi-client.js"></script>
<script src="../js/i18n.js"></script> <script src="../js/i18n.js"></script>
<script src="../js/click-to-load.js"></script> <script src="../js/click2load.js"></script>
</body> </body>
</html> </html>