Skip to content

Commit 2e5d32e

Browse files
committed
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.
1 parent c54fb03 commit 2e5d32e

File tree

8 files changed

+52
-46
lines changed

8 files changed

+52
-46
lines changed

src/_locales/en/messages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,10 @@
10631063
"message": "GB",
10641064
"description": "short for 'gigabytes'"
10651065
},
1066+
"clickToLoad": {
1067+
"message": "Click to load",
1068+
"description": "Message use in frame placeholders"
1069+
},
10661070
"dummy": {
10671071
"message": "This entry must be the last one",
10681072
"description": "so we dont need to deal with comma for last entry"
File renamed without changes.
File renamed without changes.

src/js/filtering-context.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
this.tabOrigin = undefined;
4545
this.tabHostname = undefined;
4646
this.tabDomain = undefined;
47+
this.redirectURL = undefined;
4748
this.filter = undefined;
4849
};
4950

@@ -104,6 +105,7 @@
104105
} else {
105106
this.setDocOrigin(this.tabOrigin);
106107
}
108+
this.redirectURL = undefined;
107109
this.filter = undefined;
108110
return this;
109111
},
@@ -122,6 +124,7 @@
122124
this.tabOrigin = other.tabOrigin;
123125
this.tabHostname = other.tabHostname;
124126
this.tabDomain = other.tabDomain;
127+
this.redirectURL = other.redirectURL;
125128
this.filter = undefined;
126129
return this;
127130
},

src/js/pagestore.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,21 @@ const NetFilteringResultCache = class {
5454
return this;
5555
}
5656

57+
// https://github.com/gorhill/uBlock/issues/3619
58+
// Don't collapse redirected resources
5759
rememberResult(fctxt, result) {
5860
if ( fctxt.tabId <= 0 ) { return; }
5961
if ( this.results.size === 0 ) {
6062
this.pruneAsync();
6163
}
62-
const key = fctxt.getDocHostname() + ' ' + fctxt.type + ' ' + fctxt.url;
64+
const key = `${fctxt.getDocHostname()} ${fctxt.type} ${fctxt.url}`;
6365
this.results.set(key, {
64-
result: result,
66+
result,
67+
redirectURL: fctxt.redirectURL,
6568
logData: fctxt.filter,
6669
tstamp: Date.now()
6770
});
68-
if ( result !== 1 ) { return; }
71+
if ( result !== 1 || fctxt.redirectURL !== undefined ) { return; }
6972
const now = Date.now();
7073
this.blocked.set(key, now);
7174
this.hash = now;
@@ -76,16 +79,17 @@ const NetFilteringResultCache = class {
7679
if ( this.blocked.size === 0 ) {
7780
this.pruneAsync();
7881
}
82+
if ( fctxt.redirectURL !== undefined ) { return; }
7983
const now = Date.now();
8084
this.blocked.set(
81-
fctxt.getDocHostname() + ' ' + fctxt.type + ' ' + fctxt.url,
85+
`${fctxt.getDocHostname()} ${fctxt.type} ${fctxt.url}`,
8286
now
8387
);
8488
this.hash = now;
8589
}
8690

87-
forgetResult(fctxt) {
88-
const key = `${fctxt.getDocHostname()} ${fctxt.type} ${fctxt.url}`;
91+
forgetResult(docHostname, type, url) {
92+
const key = `${docHostname} ${type} ${url}`;
8993
this.results.delete(key);
9094
this.blocked.delete(key);
9195
}
@@ -171,7 +175,7 @@ const FrameStore = class {
171175
init(frameURL) {
172176
this.t0 = Date.now();
173177
this.exceptCname = undefined;
174-
this.clickToLoad = 0;
178+
this.clickToLoad = false;
175179
this.rawURL = frameURL;
176180
if ( frameURL !== undefined ) {
177181
this.hostname = vAPI.hostnameFromURI(frameURL);
@@ -559,6 +563,7 @@ const PageStore = class {
559563
if ( cacheableResult ) {
560564
const entry = this.netFilteringCache.lookupResult(fctxt);
561565
if ( entry !== undefined ) {
566+
fctxt.redirectURL = entry.redirectURL;
562567
fctxt.filter = entry.logData;
563568
return entry.result;
564569
}
@@ -607,11 +612,11 @@ const PageStore = class {
607612
}
608613
}
609614

610-
// Click-to-load:
615+
// Click-to-load?
611616
// When frameId is not -1, the resource is always sub_frame.
612617
if ( result === 1 && fctxt.frameId !== -1 ) {
613-
const docStore = this.getFrameStore(fctxt.frameId);
614-
if ( docStore !== null && docStore.clickToLoad !== 0 ) {
618+
const frameStore = this.getFrameStore(fctxt.frameId);
619+
if ( frameStore !== null && frameStore.clickToLoad ) {
615620
result = 2;
616621
if ( µb.logger.enabled ) {
617622
fctxt.setFilter({
@@ -623,13 +628,20 @@ const PageStore = class {
623628
}
624629
}
625630

631+
// https://github.com/gorhill/uBlock/issues/949
632+
// Redirect blocked request?
633+
if ( result === 1 && µb.hiddenSettings.ignoreRedirectFilters !== true ) {
634+
const redirectURL = µb.redirectEngine.toURL(fctxt);
635+
if ( redirectURL !== undefined ) {
636+
fctxt.redirectURL = redirectURL;
637+
this.internalRedirectionCount += 1;
638+
}
639+
}
640+
626641
if ( cacheableResult ) {
627642
this.netFilteringCache.rememberResult(fctxt, result);
628-
} else if (
629-
result === 1 &&
630-
this.collapsibleResources.has(requestType)
631-
) {
632-
this.netFilteringCache.rememberBlock(fctxt, true);
643+
} else if ( result === 1 && this.collapsibleResources.has(requestType) ) {
644+
this.netFilteringCache.rememberBlock(fctxt);
633645
}
634646

635647
return result;
@@ -727,7 +739,12 @@ const PageStore = class {
727739
if ( frameStore === null ) {
728740
frameStore = this.setFrameURL(frameId, frameURL);
729741
}
730-
frameStore.clickToLoad = Date.now();
742+
this.netFilteringCache.forgetResult(
743+
this.tabHostname,
744+
'sub_frame',
745+
frameURL
746+
);
747+
frameStore.clickToLoad = true;
731748
}
732749

733750
shouldExceptCname(fctxt) {
@@ -776,24 +793,16 @@ const PageStore = class {
776793
// content script-side (i.e. `iframes` -- unlike `img`).
777794
if ( Array.isArray(resources) && resources.length !== 0 ) {
778795
for ( const resource of resources ) {
779-
const result = this.filterRequest(
796+
this.filterRequest(
780797
fctxt.setType(resource.type).setURL(resource.url)
781798
);
782-
if ( result === 1 && µb.redirectEngine.toURL(fctxt) ) {
783-
this.forgetBlockedResource(fctxt);
784-
}
785799
}
786800
}
787801
if ( this.netFilteringCache.hash === response.hash ) { return; }
788802
response.hash = this.netFilteringCache.hash;
789803
response.blockedResources =
790804
this.netFilteringCache.lookupAllBlocked(fctxt.getDocHostname());
791805
}
792-
793-
forgetBlockedResource(fctxt) {
794-
if ( this.collapsibleResources.has(fctxt.type) === false ) { return; }
795-
this.netFilteringCache.forgetResult(fctxt);
796-
}
797806
};
798807

799808
PageStore.prototype.cacheableResults = new Set([

src/js/redirect-engine.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ const redirectableResources = new Map([
6767
[ 'chartbeat.js', {
6868
alias: 'static.chartbeat.com/chartbeat.js',
6969
} ],
70-
[ 'click-to-load.html', {
71-
alias: 'clicktoload',
70+
[ 'click2load.html', {
7271
params: [ 'url' ],
7372
} ],
7473
[ 'doubleclick_instream_ad_status.js', {

src/js/traffic.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,16 @@ const onBeforeRequest = function(details) {
111111

112112
// Blocked
113113

114-
// https://github.com/gorhill/uBlock/issues/949
115-
// Redirect blocked request?
116-
// https://github.com/gorhill/uBlock/issues/3619
117-
// Don't collapse redirected resources
118-
if ( µb.hiddenSettings.ignoreRedirectFilters !== true ) {
119-
const url = µb.redirectEngine.toURL(fctxt);
120-
if ( url !== undefined ) {
121-
pageStore.internalRedirectionCount += 1;
122-
pageStore.forgetBlockedResource(fctxt);
123-
if ( µb.logger.enabled ) {
124-
fctxt.setRealm('redirect')
125-
.setFilter({ source: 'redirect', raw: µb.redirectEngine.resourceNameRegister })
126-
.toLogger();
127-
}
128-
return { redirectUrl: url };
129-
}
114+
if ( fctxt.redirectURL === undefined ) {
115+
return { cancel: true };
130116
}
131117

132-
return { cancel: true };
118+
if ( µb.logger.enabled ) {
119+
fctxt.setRealm('redirect')
120+
.setFilter({ source: 'redirect', raw: µb.redirectEngine.resourceNameRegister })
121+
.toLogger();
122+
}
123+
return { redirectUrl: fctxt.redirectURL };
133124
};
134125

135126
/******************************************************************************/

src/web_accessible_resources/click-to-load.html renamed to src/web_accessible_resources/click2load.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>uBlock Origin Click-to-Load</title>
77
<link rel="stylesheet" href="../css/themes/default.css">
88
<link rel="stylesheet" href="../css/common.css">
9-
<link rel="stylesheet" href="../css/click-to-load.css">
9+
<link rel="stylesheet" href="../css/click2load.css">
1010
</head>
1111

1212
<body>
@@ -19,7 +19,7 @@
1919
<script src="../js/vapi-common.js"></script>
2020
<script src="../js/vapi-client.js"></script>
2121
<script src="../js/i18n.js"></script>
22-
<script src="../js/click-to-load.js"></script>
22+
<script src="../js/click2load.js"></script>
2323

2424
</body>
2525
</html>

0 commit comments

Comments
 (0)