/**
 The background script is the extension's event handler; it contains listeners for browser
 events that are important to the extension. It lies dormant until an event is fired then
 performs the instructed logic. An effective background script is only loaded when it is
 needed and unloaded when it goes idle.
 https://developer.chrome.com/docs/extensions/mv3/architecture-overview/#background_script
 */ import * as Sentry from '@sentry/react' ; import { storePayload , StorageKey } from '@shared/utils/storage' ; import { RouteUrls } from '@shared/route-urls' ; import { CONTENT_SCRIPT_PORT , ExternalMethods , MessageFromContentScript , } from '@shared/message-types' ; import type { VaultActions } from '@shared/vault/vault-types' ; import { popupCenter } from '@background/popup' ; import { vaultMessageHandler } from '@background/vault' ; import { initContextMenuActions } from '@background/init-context-menus' ; import { initSentry } from '@shared/utils/sentry-init' ; const IS_TEST_ENV = process . env . TEST_ENV === 'true' ; initSentry ( ) ; initContextMenuActions ( ) ; async function openRequestInFullPage ( path , urlParams ) { await chrome . tabs . create ( { url : chrome . runtime . getURL ( `index.html#${ path }?${ urlParams . toString ( ) }` ) , } ) ; } chrome . runtime . onInstalled . addListener ( details => { Sentry . wrap ( async ( ) => { if ( details . reason === 'install' && ! IS_TEST_ENV ) { await chrome . tabs . create ( { url : chrome . runtime . getURL ( `index.html#${ RouteUrls . Onboarding }` ) , } ) ; } } ) ; } ) ; chrome . runtime . onConnect . addListener ( port => Sentry . wrap ( ( ) => { if ( port . name === CONTENT_SCRIPT_PORT ) { port . onMessage . addListener ( async ( message , port ) => { const { payload } = message ; switch ( message . method ) { case ExternalMethods . authenticationRequest : { void storePayload ( { payload , storageKey : StorageKey . authenticationRequests , port , } ) ; const path = RouteUrls . Onboarding ; const urlParams = new URLSearchParams ( ) ; urlParams . set ( 'authRequest' , payload ) ; if ( IS_TEST_ENV ) { await openRequestInFullPage ( path , urlParams ) ; } else { popupCenter ( { url : `/popup.html#${ path }?${ urlParams . toString ( ) }` } ) ; } break ; } case ExternalMethods . transactionRequest : { void storePayload ( { payload , storageKey : StorageKey . transactionRequests , port , } ) ; const path = RouteUrls . Transaction ; const urlParams = new URLSearchParams ( ) ; urlParams . set ( 'request' , payload ) ; if ( IS_TEST_ENV ) { await openRequestInFullPage ( path , urlParams ) ; } else { popupCenter ( { url : `/popup.html#${ path }?${ urlParams . toString ( ) }` } ) ; } break ; } default : break ; } } ) ; } } ) ) ; chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => Sentry . wrap ( ( ) => { if ( ! sender . url ?. startsWith ( chrome . runtime . getURL ( '' ) ) ) return ; void vaultMessageHandler ( message ) . then ( sendResponse ) . catch ( sendResponse ) ; return true ; } ) ) ; if ( IS_TEST_ENV ) { ( window as any ) . openOptionsPage = function ( page ) { return chrome . runtime . getURL ( `index.html#${ page }` ) ; } ; }