From 2e5d32e96798dd55f3fae66d7091645ff7ad3784 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 10 Oct 2020 08:36:30 -0400 Subject: [PATCH] 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. --- src/_locales/en/messages.json | 4 ++ src/css/{click-to-load.css => click2load.css} | 0 src/js/{click-to-load.js => click2load.js} | 0 src/js/filtering-context.js | 3 + src/js/pagestore.js | 59 +++++++++++-------- src/js/redirect-engine.js | 3 +- src/js/traffic.js | 25 +++----- .../{click-to-load.html => click2load.html} | 4 +- 8 files changed, 52 insertions(+), 46 deletions(-) rename src/css/{click-to-load.css => click2load.css} (100%) rename src/js/{click-to-load.js => click2load.js} (100%) rename src/web_accessible_resources/{click-to-load.html => click2load.html} (84%) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index 79dea0903..54170c36a 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -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" diff --git a/src/css/click-to-load.css b/src/css/click2load.css similarity index 100% rename from src/css/click-to-load.css rename to src/css/click2load.css diff --git a/src/js/click-to-load.js b/src/js/click2load.js similarity index 100% rename from src/js/click-to-load.js rename to src/js/click2load.js diff --git a/src/js/filtering-context.js b/src/js/filtering-context.js index 17f46b122..cad4e1684 100644 --- a/src/js/filtering-context.js +++ b/src/js/filtering-context.js @@ -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; }, diff --git a/src/js/pagestore.js b/src/js/pagestore.js index f5cd7dd9c..7301f992b 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -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([ diff --git a/src/js/redirect-engine.js b/src/js/redirect-engine.js index 71ac4b0aa..378183f6d 100644 --- a/src/js/redirect-engine.js +++ b/src/js/redirect-engine.js @@ -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', { diff --git a/src/js/traffic.js b/src/js/traffic.js index fb1a4071b..481cd8916 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -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 }; }; /******************************************************************************/ diff --git a/src/web_accessible_resources/click-to-load.html b/src/web_accessible_resources/click2load.html similarity index 84% rename from src/web_accessible_resources/click-to-load.html rename to src/web_accessible_resources/click2load.html index f8cfa99b0..57c806869 100644 --- a/src/web_accessible_resources/click-to-load.html +++ b/src/web_accessible_resources/click2load.html @@ -6,7 +6,7 @@ uBlock Origin Click-to-Load - + @@ -19,7 +19,7 @@ - +