Skip to content

Commit

Permalink
scripts/background: use browser polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
digiwand committed Oct 25, 2022
1 parent 3b66c2c commit 0047146
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global chrome */

/**
* @file The entry point for the web extension singleton process.
*/
Expand Down Expand Up @@ -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) {

This comment has been minimized.

Copy link
@digiwand

digiwand Oct 25, 2022

Author Contributor

[WIP] I plan to remove this check and update some more logic in this 0047146 commit

@jpuri It seems that when we only send the message to the active tab of the currentWindow, we might miss the dapp contentscript(s). When this happens, I get into a state in which I cannot reactivate the dapp streams.

I think we might want to send the message to all of the tabs to ensure we reestablish the streams for all related dapps, unless we have a better way to query or filter out the dapp tabs.

This comment has been minimized.

Copy link
@digiwand

digiwand Oct 26, 2022

Author Contributor

updated here b0dc2a8

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);
Expand Down

0 comments on commit 0047146

Please sign in to comment.