1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-03 17:49:39 +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",
"description": "short for 'gigabytes'"
},
"clickToLoad": {
"message": "Click to load",
"description": "Message use in frame placeholders"
},
"dummy": {
"message": "This entry must be the last one",
"description": "so we dont need to deal with comma for last entry"

View File

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

View File

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

View File

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

View File

@ -111,25 +111,16 @@ const onBeforeRequest = function(details) {
// Blocked
// https://github.com/gorhill/uBlock/issues/949
// Redirect blocked request?
// 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 };
}
if ( fctxt.redirectURL === undefined ) {
return { cancel: true };
}
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>
<link rel="stylesheet" href="../css/themes/default.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>
<body>
@ -19,7 +19,7 @@
<script src="../js/vapi-common.js"></script>
<script src="../js/vapi-client.js"></script>
<script src="../js/i18n.js"></script>
<script src="../js/click-to-load.js"></script>
<script src="../js/click2load.js"></script>
</body>
</html>