import type { Connections , Message } from './types/types' ; const connections = { } ; chrome . runtime . onConnect . addListener ( ( port ) => { const extensionListener = ( message , portID ) => { const { tabId , action } = message ; if ( action === 'connectChromogen' ) { connections [ tabId ] = portID ; } chrome . tabs . sendMessage ( Number ( tabId ) , message ) ; } ; port . onMessage . addListener ( extensionListener ) ; port . onDisconnect . addListener ( ( portID ) => { portID . onMessage . removeListener ( extensionListener ) ; for ( const key in connections ) { if ( connections [ key ] === portID ) { delete connections [ key ] ; break ; } } } ) ; } ) ; chrome . runtime . onMessage . addListener ( ( message , sender ) => { const { tab } = sender ; if ( tab ) { const tabId = `${ tab . id }` ; if ( connections [ tabId ] ) { connections [ tabId ] . postMessage ( { action : message . action , } ) ; } } } ) ;