From 3e21a792eb403dfd573c7fd05f24f2fb010844e7 Mon Sep 17 00:00:00 2001 From: Kevin Kuehler Date: Wed, 22 Sep 2021 01:12:53 -0700 Subject: [PATCH] Inject on webNavigation.onCommitted() We inject if the following conditions are met: 1. WDP pref is enabled 2. Not loading a chrome:// resource 3. Page is not loaded from bfcache (avoids double inject) --- .../background/webDiscoveryProject.ts | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/components/brave_extension/extension/brave_extension/background/webDiscoveryProject.ts b/components/brave_extension/extension/brave_extension/background/webDiscoveryProject.ts index 8497260dba9c..b3c4f6b5f5d0 100644 --- a/components/brave_extension/extension/brave_extension/background/webDiscoveryProject.ts +++ b/components/brave_extension/extension/brave_extension/background/webDiscoveryProject.ts @@ -17,20 +17,20 @@ if (App !== undefined) { // Dynamically inject WDP content script so that users with the pref disabled // don't need to pay the loading cost on each page. - chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => { - if (tab.id) { - const urlMatches = (): boolean => { - const url = tab.url || tab.pendingUrl || '' - return url ? !/chrome:\/\//.test(url) : false - } - // We inject on `complete` since multiple `loading` events can fire. - if (APP.isRunning && (changeInfo.status === 'complete') && urlMatches()) { - chrome.tabs.executeScript(tab.id, { - file: 'bundles/web-discovery-content-script.bundle.js', - matchAboutBlank: false, - runAt: 'document_start' - }, () => { /* Deliberately left blank */ }) - } + chrome.webNavigation.onCommitted.addListener((details: chrome.webNavigation.WebNavigationTransitionCallbackDetails) => { + // We skip injecting on bfcache load since it will cause us to double + // inject. + const isBfCache = details.transitionQualifiers.includes('forward_back') + if (APP.isRunning && !isBfCache && !/chrome:\/\//.test(details.url)) { + chrome.tabs.executeScript(details.tabId, { + file: 'bundles/web-discovery-content-script.bundle.js', + matchAboutBlank: false, + runAt: 'document_end' + }, () => { + if (chrome.runtime.lastError) { + console.warn('[web-discovery-project] Could not inject script:', chrome.runtime.lastError) + } + }) } })