Skip to content

Commit

Permalink
Fine tune code related to click-to-load feature
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gorhill committed Oct 10, 2020
1 parent c54fb03 commit 2e5d32e
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 46 deletions.
4 changes: 4 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions src/js/filtering-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
this.tabOrigin = undefined;
this.tabHostname = undefined;
this.tabDomain = undefined;
this.redirectURL = undefined;
this.filter = undefined;
};

Expand Down Expand Up @@ -104,6 +105,7 @@
} else {
this.setDocOrigin(this.tabOrigin);
}
this.redirectURL = undefined;
this.filter = undefined;
return this;
},
Expand All @@ -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;
},
Expand Down
59 changes: 34 additions & 25 deletions src/js/pagestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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({
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -776,24 +793,16 @@ 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; }
response.hash = this.netFilteringCache.hash;
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([
Expand Down
3 changes: 1 addition & 2 deletions src/js/redirect-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
25 changes: 8 additions & 17 deletions src/js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};

/******************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>

0 comments on commit 2e5d32e

Please sign in to comment.