import browser from "webextension-polyfill" ; const NOTIFICATION_WIDTH = 322 ; const NOTIFICATION_HEIGHT = 610 ; function wait ( delay ) { return new Promise ( ( resolve ) => setTimeout ( resolve , delay ) ) ; } export async function openUi ( ) { const [ existingPopup ] = await browser . tabs . query ( { url : [ browser . runtime . getURL ( "/index.html" ) , browser . runtime . getURL ( "/index.html*" ) , ] , } ) ; if ( existingPopup && existingPopup . windowId ) return browser . windows . update ( existingPopup . windowId , { focused : true } ) ; let left = 0 ; let top = 0 ; try { const lastFocused = await browser . windows . getLastFocused ( ) ; top = lastFocused . top ?? 0 ; left = ( lastFocused . left ?? 0 ) + Math . max ( ( lastFocused . width ?? 0 ) - NOTIFICATION_WIDTH , 0 ) ; } catch ( _ ) { const { screenX , screenY , outerWidth } = window ; top = Math . max ( screenY , 0 ) ; left = Math . max ( screenX + ( outerWidth - NOTIFICATION_WIDTH ) , 0 ) ; } await wait ( 300 ) ; const popup = await browser . windows . create ( { url : "index.html?popup" , type : "popup" , width : NOTIFICATION_WIDTH , height : NOTIFICATION_HEIGHT , left , top , } ) ; return popup ; }