From 2ab6234d11b3b11e10dd993d454eeaad63bfc886 Mon Sep 17 00:00:00 2001 From: Ariella Vu <20778143+digiwand@users.noreply.github.com> Date: Wed, 26 Oct 2022 03:26:16 +0700 Subject: [PATCH 1/4] scripts/background: use browser polyfill --- app/scripts/background.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index 1ade2627a9e9..de66bde7e850 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -1,5 +1,3 @@ -/* global chrome */ - /** * @file The entry point for the web extension singleton process. */ @@ -106,8 +104,8 @@ if (isManifestV3) { browser.runtime.onConnect.addListener(initApp); // Message below signals content script in DAPPS to connect to metamask background as backend is not active // It is required to re-connect DAPPS after service worker re-activation - chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { - chrome.tabs.sendMessage( + browser.tabs.query({ active: true, currentWindow: true }, function (tabs) { + browser.tabs.sendMessage( tabs[0].id, { name: EXTENSION_MESSAGES.READY }, () => undefined, From 3b66c2c1d7f826f7c3741f29ce3f9d1891ca7a8d Mon Sep 17 00:00:00 2001 From: Ariella Vu <20778143+digiwand@users.noreply.github.com> Date: Wed, 26 Oct 2022 03:36:39 +0700 Subject: [PATCH 2/4] Revert "scripts/background: use browser polyfill" This reverts commit 2ab6234d11b3b11e10dd993d454eeaad63bfc886. --- app/scripts/background.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index de66bde7e850..1ade2627a9e9 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -1,3 +1,5 @@ +/* global chrome */ + /** * @file The entry point for the web extension singleton process. */ @@ -104,8 +106,8 @@ if (isManifestV3) { browser.runtime.onConnect.addListener(initApp); // Message below signals content script in DAPPS to connect to metamask background as backend is not active // It is required to re-connect DAPPS after service worker re-activation - browser.tabs.query({ active: true, currentWindow: true }, function (tabs) { - browser.tabs.sendMessage( + chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { + chrome.tabs.sendMessage( tabs[0].id, { name: EXTENSION_MESSAGES.READY }, () => undefined, From 00471464812bc33f37510624743ec6698798d590 Mon Sep 17 00:00:00 2001 From: Ariella Vu <20778143+digiwand@users.noreply.github.com> Date: Wed, 26 Oct 2022 04:50:29 +0700 Subject: [PATCH 3/4] scripts/background: use browser polyfill --- app/scripts/background.js | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index 1ade2627a9e9..2473ab33f4fb 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -1,5 +1,3 @@ -/* global chrome */ - /** * @file The entry point for the web extension singleton process. */ @@ -94,25 +92,41 @@ const PHISHING_WARNING_PAGE_TIMEOUT = ONE_SECOND_IN_MILLISECONDS; /** * In case of MV3 we attach a "onConnect" event listener as soon as the application is initialised. * Reason is that in case of MV3 a delay in doing this was resulting in missing first connect event after service worker is re-activated. + * + * @param remotePort */ - const initApp = async (remotePort) => { browser.runtime.onConnect.removeListener(initApp); await initialize(remotePort); log.info('MetaMask initialization complete.'); }; +/** + * Sends a message to the dapp(s) content script to signal it can connect to MetaMask background as + * the backend is not active. It is required to re-connect dapps after service worker re-activates. + */ +const sendReadyMessageToActiveTab = async () => { + try { + const tabs = await browser.tabs.query({ + active: true, + currentWindow: true, + }); + + if (!tabs?.[0]?.id) { + return; + } + + await browser.tabs.sendMessage(tabs[0].id, { + name: EXTENSION_MESSAGES.READY, + }); + } catch (err) { + log.error(err); + } +}; + if (isManifestV3) { browser.runtime.onConnect.addListener(initApp); - // Message below signals content script in DAPPS to connect to metamask background as backend is not active - // It is required to re-connect DAPPS after service worker re-activation - chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { - chrome.tabs.sendMessage( - tabs[0].id, - { name: EXTENSION_MESSAGES.READY }, - () => undefined, - ); - }); + sendReadyMessageToActiveTab(); } else { // initialization flow initialize().catch(log.error); From bbd5cffde0503e41e1056a045d2912981675af18 Mon Sep 17 00:00:00 2001 From: Ariella Vu <20778143+digiwand@users.noreply.github.com> Date: Wed, 26 Oct 2022 04:51:09 +0700 Subject: [PATCH 4/4] script/background: check lastError --- app/scripts/background.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index 2473ab33f4fb..d82d315901e0 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -46,7 +46,7 @@ import rawFirstTimeState from './first-time-state'; import getFirstPreferredLangCode from './lib/get-first-preferred-lang-code'; import getObjStructure from './lib/getObjStructure'; import setupEnsIpfsResolver from './lib/ens-ipfs/setup'; -import { getPlatform } from './lib/util'; +import { checkForError, getPlatform } from './lib/util'; /* eslint-enable import/first */ const { sentry } = global; @@ -101,6 +101,13 @@ const initApp = async (remotePort) => { log.info('MetaMask initialization complete.'); }; +const throwErrorIfLastErrorFound = () => { + const err = checkForError(); + if (err) { + throw err; + } +}; + /** * Sends a message to the dapp(s) content script to signal it can connect to MetaMask background as * the backend is not active. It is required to re-connect dapps after service worker re-activates. @@ -111,6 +118,7 @@ const sendReadyMessageToActiveTab = async () => { active: true, currentWindow: true, }); + throwErrorIfLastErrorFound(); if (!tabs?.[0]?.id) { return; @@ -119,6 +127,7 @@ const sendReadyMessageToActiveTab = async () => { await browser.tabs.sendMessage(tabs[0].id, { name: EXTENSION_MESSAGES.READY, }); + throwErrorIfLastErrorFound(); } catch (err) { log.error(err); }