@@ -141,16 +141,26 @@ const PHISHING_WARNING_PAGE_TIMEOUT = ONE_SECOND_IN_MILLISECONDS;
141141// Event emitter for state persistence
142142export const statePersistenceEvents = new EventEmitter ( ) ;
143143
144- if ( isFirefox || ! isManifestV3 ) {
144+ // On MV3 builds we must listen for this event in `app-init`, otherwise we found that the listener
145+ // is never called.
146+ // There is no `app-init` file on MV2 builds, so we add a listener here instead.
147+ if ( ! isManifestV3 ) {
145148 browser . runtime . onInstalled . addListener ( function ( details ) {
146149 if ( details . reason === 'install' ) {
147- onFirstInstall ( ) ;
150+ onInstall ( ) ;
148151 }
149152 } ) ;
153+
154+ // This condition is for when the `onInstalled` listener in `app-init` was called before
155+ // `background.js` was loaded.
150156} else if ( globalThis . __metamaskWasJustInstalled ) {
151- onFirstInstall ( ) ;
157+ onInstall ( ) ;
158+ // Delete just to clean up global namespace
159+ delete globalThis . __metamaskWasJustInstalled ;
160+ // This condition is for when `background.js` was loaded before the `onInstalled` listener was
161+ // called.
152162} else {
153- globalThis . __metamaskTriggerOnInstall = ( ) => onFirstInstall ( ) ;
163+ globalThis . __metamaskTriggerOnInstall = ( ) => onInstall ( ) ;
154164}
155165
156166/**
@@ -1274,8 +1284,10 @@ const addAppInstalledEvent = () => {
12741284 } , 500 ) ;
12751285} ;
12761286
1277- // On first install, open a new tab with MetaMask
1278- function onFirstInstall ( ) {
1287+ /**
1288+ * Trigger actions that should happen only upon initial install (e.g. open tab for onboarding).
1289+ */
1290+ function onInstall ( ) {
12791291 log . debug ( 'First install detected' ) ;
12801292 if ( process . env . IN_TEST ) {
12811293 addAppInstalledEvent ( ) ;
0 commit comments