import { queueUtterance } from './queueUtterance' ; import { setupCrossDomainCommunication } from './setupCrossDomainCommunication' ; const pageLoadTimestamp = Date . now ( ) ; export const speakOnNewMessage = ( ) => { const crossDomainListenerObj = setupCrossDomainCommunication ( ) ; crossDomainListenerObj . addMessageListener ( ( message ) => { if ( message === 'Page load timestamp?' ) { crossDomainListenerObj . postMessage ( `Page load timestamp: ${ pageLoadTimestamp }` ) ; } } ) ; const unreadSpan = document . querySelector ( '.indicator-badge.js-unread-count' ) ! ; new MutationObserver ( ( ) => { const timeoutId = window . setTimeout ( ( ) => { queueUtterance ( 'Message' ) ; crossDomainListenerObj . removeMessageListener ( listener ) ; } , 200 ) ; const listener = ( message ) => { const match = message . match ( /Page load timestamp: (\d+)/ ) ; if ( ! match ) { return ; } const otherTimestamp = Number ( match [ 1 ] ) ; if ( otherTimestamp > pageLoadTimestamp ) { window . clearTimeout ( timeoutId ) ; crossDomainListenerObj . removeMessageListener ( listener ) ; } } ; crossDomainListenerObj . addMessageListener ( listener ) ; crossDomainListenerObj . postMessage ( 'Page load timestamp?' ) ; } ) . observe ( unreadSpan , { childList : true } ) ; } ;