import { browser } from "webextension-polyfill-ts" ; import { EXTERNAL_MSG_REQUEST , EXTERNAL_MSG_RESPONSE , } from "@shared/constants/services" ; export const redirectMessagesToBackground = ( ) => { window . addEventListener ( "message" , async ( event ) => { const messagedId = event ?. data ?. messageId || 0 ; if ( event . source !== window ) return ; if ( ! event . data . source || event . data . source !== EXTERNAL_MSG_REQUEST ) return ; let res = { error : "Unable to send message to extension" } ; try { res = await browser . runtime . sendMessage ( event . data ) ; } catch ( e ) { console . error ( e ) ; } window . postMessage ( { source : EXTERNAL_MSG_RESPONSE , messagedId , ... res } , window . location . origin ) ; } , false ) ; } ;