From 60d120621514a3b33cc6fe4754d4f8fed3e1f514 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 10 Sep 2022 11:55:19 -0400 Subject: [PATCH] Make popup panel reflect state of the actual blocked page Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/1954 --- src/js/messaging.js | 21 +++++++++++++++------ src/js/popup-fenix.js | 1 + src/js/tab.js | 14 ++++++-------- src/js/ublock.js | 13 +++++++++++++ 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/js/messaging.js b/src/js/messaging.js index 19dfe43a6..53bf032b1 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -299,15 +299,24 @@ const onMessage = function(request, sender, callback) { µb.openNewTab(request.details); break; - case 'reloadTab': - if ( vAPI.isBehindTheSceneTabId(request.tabId) === false ) { - vAPI.tabs.reload(request.tabId, request.bypassCache === true); - if ( request.select && vAPI.tabs.select ) { - vAPI.tabs.select(request.tabId); + // https://github.com/uBlockOrigin/uBlock-issues/issues/1954 + // In case of document-blocked page, navigate to blocked URL instead + // of forcing a reload. + case 'reloadTab': { + if ( vAPI.isBehindTheSceneTabId(request.tabId) ) { break; } + const { tabId, bypassCache, url, select } = request; + vAPI.tabs.get(tabId).then(tab => { + if ( url && tab && url !== tab.url ) { + vAPI.tabs.replace(tabId, url); + } else { + vAPI.tabs.reload(tabId, bypassCache === true); } + }); + if ( select && vAPI.tabs.select ) { + vAPI.tabs.select(tabId); } break; - + } case 'setWhitelist': µb.netWhitelist = µb.whitelistFromString(request.whitelist); µb.saveWhitelist(); diff --git a/src/js/popup-fenix.js b/src/js/popup-fenix.js index 2d1eb3fc8..555da9945 100644 --- a/src/js/popup-fenix.js +++ b/src/js/popup-fenix.js @@ -1077,6 +1077,7 @@ const reloadTab = function(ev) { messaging.send('popupPanel', { what: 'reloadTab', tabId: popupData.tabId, + url: popupData.pageURL, select: vAPI.webextFlavor.soup.has('mobile'), bypassCache: ev.ctrlKey || ev.metaKey || ev.shiftKey, }); diff --git a/src/js/tab.js b/src/js/tab.js index 9a9ac8c62..72ef95a0e 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -339,14 +339,9 @@ const onPopupUpdated = (( ) => { return; } - // If the page URL is that of our "blocked page" URL, extract the URL + // If the page URL is that of our document-blocked URL, extract the URL // of the page which was blocked. - if ( targetURL.startsWith(vAPI.getURL('document-blocked.html')) ) { - const matches = /details=([^&]+)/.exec(targetURL); - if ( matches !== null ) { - targetURL = JSON.parse(decodeURIComponent(matches[1])).url; - } - } + targetURL = µb.pageURLFromMaybeDocumentBlockedURL(targetURL); // MUST be reset before code below is called. const fctxt = µb.filteringContext.duplicate(); @@ -683,6 +678,9 @@ housekeep itself. // Update just force all properties to be updated to match the most recent // root URL. + // https://github.com/uBlockOrigin/uBlock-issues/issues/1954 + // In case of document-blocked page, use the blocked page URL as the + // context. TabContext.prototype.update = function() { this.netFilteringReadTime = 0; if ( this.stack.length === 0 ) { @@ -694,7 +692,7 @@ housekeep itself. return; } const stackEntry = this.stack[this.stack.length - 1]; - this.rawURL = stackEntry.url; + this.rawURL = µb.pageURLFromMaybeDocumentBlockedURL(stackEntry.url); this.normalURL = µb.normalizeTabURL(this.tabId, this.rawURL); this.origin = originFromURI(this.normalURL); this.rootHostname = hostnameFromURI(this.origin); diff --git a/src/js/ublock.js b/src/js/ublock.js index 06c4a5fb0..390c8783d 100644 --- a/src/js/ublock.js +++ b/src/js/ublock.js @@ -656,3 +656,16 @@ const matchBucket = function(url, hostname, bucket, start) { } /******************************************************************************/ + +µb.pageURLFromMaybeDocumentBlockedURL = function(pageURL) { + if ( pageURL.startsWith(vAPI.getURL('/document-blocked.html?')) ) { + try { + const url = new URL(pageURL); + return JSON.parse(url.searchParams.get('details')).url; + } catch(ex) { + } + } + return pageURL; +}; + +/******************************************************************************/