From 65985343fcb4f2215c47f53f2b9cd771264b6a9c Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 18 Oct 2021 09:43:41 -0400 Subject: [PATCH] Create new page store if not found in tab event Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/757 Sometimes a tab event may fire for a tab which is not yet known to uBO. In such case, bind the tab internally so that it can be processed properly in the future. --- src/js/tab.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/js/tab.js b/src/js/tab.js index f239f25e2..7e6640c4f 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -63,7 +63,11 @@ import { if ( tabId < 0 ) { return 'http://behind-the-scene/'; } - tabURLNormalizer.href = tabURL; + try { + tabURLNormalizer.href = tabURL; + } catch(ex) { + return tabURL; + } const protocol = tabURLNormalizer.protocol.slice(0, -1); if ( protocol === 'https' || protocol === 'http' ) { return tabURLNormalizer.href; @@ -869,11 +873,18 @@ housekeep itself. vAPI.Tabs = class extends vAPI.Tabs { onActivated(details) { + const { tabId } = details; + if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; } + // https://github.com/uBlockOrigin/uBlock-issues/issues/757 + const pageStore = µb.pageStoreFromTabId(tabId); + if ( pageStore === null ) { + this.onNewTab(tabId); + return; + } super.onActivated(details); - if ( vAPI.isBehindTheSceneTabId(details.tabId) ) { return; } // https://github.com/uBlockOrigin/uBlock-issues/issues/680 - µb.updateToolbarIcon(details.tabId); - contextMenu.update(details.tabId); + µb.updateToolbarIcon(tabId); + contextMenu.update(tabId); } onClosed(tabId) { @@ -923,10 +934,19 @@ vAPI.Tabs = class extends vAPI.Tabs { } } + async onNewTab(tabId) { + const tab = await vAPI.tabs.get(tabId); + if ( tab === null ) { return; } + const { id, url = '' } = tab; + if ( url === '' ) { return; } + µb.tabContextManager.commit(id, url); + µb.bindTabToPageStore(id, 'tabUpdated', tab); + contextMenu.update(id); + } + // It may happen the URL in the tab changes, while the page's document // stays the same (for instance, Google Maps). Without this listener, // the extension icon won't be properly refreshed. - onUpdated(tabId, changeInfo, tab) { super.onUpdated(tabId, changeInfo, tab); if ( !tab.url || tab.url === '' ) { return; }