import { SafariApp } from "./safariApp" ; import { Utils } from "jslib-common/misc/utils" ; export class BrowserApi { static isWebExtensionsApi : boolean = typeof browser !== "undefined" ; static isSafariApi : boolean = navigator . userAgent . indexOf ( " Safari/" ) !== - 1 && navigator . userAgent . indexOf ( " Chrome/" ) === - 1 && navigator . userAgent . indexOf ( " Chromium/" ) === - 1 ; static isChromeApi : boolean = ! BrowserApi . isSafariApi && typeof chrome !== "undefined" ; static isFirefoxOnAndroid : boolean = navigator . userAgent . indexOf ( "Firefox/" ) !== - 1 && navigator . userAgent . indexOf ( "Android" ) !== - 1 ; static async getTabFromCurrentWindowId ( ) { return await BrowserApi . tabsQueryFirst ( { active : true , windowId : chrome . windows . WINDOW_ID_CURRENT , } ) ; } static async getTabFromCurrentWindow ( ) { return await BrowserApi . tabsQueryFirst ( { active : true , currentWindow : true , } ) ; } static async getActiveTabs ( ) { return await BrowserApi . tabsQuery ( { active : true , } ) ; } static async tabsQuery ( options ) { return new Promise ( ( resolve ) => { chrome . tabs . query ( options , ( tabs ) => { resolve ( tabs ) ; } ) ; } ) ; } static async tabsQueryFirst ( options ) { const tabs = await BrowserApi . tabsQuery ( options ) ; if ( tabs . length > 0 ) { return tabs [ 0 ] ; } return null ; } static tabSendMessageData ( tab , command , data = null ) { const obj = { command : command , } ; if ( data != null ) { obj . data = data ; } return BrowserApi . tabSendMessage ( tab , obj ) ; } static async tabSendMessage ( tab , obj , options = null ) { if ( ! tab || ! tab . id ) { return ; } return new Promise < void > ( ( resolve ) => { chrome . tabs . sendMessage ( tab . id , obj , options , ( ) => { if ( chrome . runtime . lastError ) { } resolve ( ) ; } ) ; } ) ; } static getBackgroundPage ( ) { return chrome . extension . getBackgroundPage ( ) ; } static getApplicationVersion ( ) { return chrome . runtime . getManifest ( ) . version ; } static async isPopupOpen ( ) { return Promise . resolve ( chrome . extension . getViews ( { type : "popup" } ) . length > 0 ) ; } static createNewTab ( url , extensionPage = false , active = true ) { chrome . tabs . create ( { url : url , active : active } ) ; } static messageListener ( name , callback ) { chrome . runtime . onMessage . addListener ( ( msg , sender , response ) => { callback ( msg , sender , response ) ; } ) ; } static async closeLoginTab ( ) { const tabs = await BrowserApi . tabsQuery ( { active : true , title : "Bitwarden" , windowType : "normal" , currentWindow : true , } ) ; if ( tabs . length === 0 ) { return ; } const tabToClose = tabs [ tabs . length - 1 ] . id ; chrome . tabs . remove ( tabToClose ) ; } static async focusSpecifiedTab ( tabId ) { chrome . tabs . update ( tabId , { active : true , highlighted : true } ) ; } static closePopup ( win ) { if ( BrowserApi . isWebExtensionsApi && BrowserApi . isFirefoxOnAndroid ) { browser . tabs . update ( { active : true } ) . finally ( win . close ) ; } else { win . close ( ) ; } } static downloadFile ( win , blobData , blobOptions , fileName ) { if ( BrowserApi . isSafariApi ) { const type = blobOptions != null ? blobOptions . type : null ; let data = null ; if ( type === "text/plain" && typeof blobData === "string" ) { data = blobData ; } else { data = Utils . fromBufferToB64 ( blobData ) ; } SafariApp . sendMessageToApp ( "downloadFile" , JSON . stringify ( { blobData : data , blobOptions : blobOptions , fileName : fileName , } ) , true ) ; } else { const blob = new Blob ( [ blobData ] , blobOptions ) ; if ( navigator . msSaveOrOpenBlob ) { navigator . msSaveBlob ( blob , fileName ) ; } else { const a = win . document . createElement ( "a" ) ; a . href = URL . createObjectURL ( blob ) ; a . download = fileName ; win . document . body . appendChild ( a ) ; a . click ( ) ; win . document . body . removeChild ( a ) ; } } } static gaFilter ( ) { return process . env . ENV !== "production" ; } static getUILanguage ( win ) { return chrome . i18n . getUILanguage ( ) ; } static reloadExtension ( win ) { if ( win != null ) { return win . location . reload ( true ) ; } else { return chrome . runtime . reload ( ) ; } } static reloadOpenWindows ( ) { const views = chrome . extension . getViews ( ) as Window [ ] ; views . filter ( ( w ) => w . location . href != null ) . forEach ( ( w ) => { w . location . reload ( ) ; } ) ; } static connectNative ( application ) { if ( BrowserApi . isWebExtensionsApi ) { return browser . runtime . connectNative ( application ) ; } else if ( BrowserApi . isChromeApi ) { return chrome . runtime . connectNative ( application ) ; } } static requestPermission ( permission ) { if ( BrowserApi . isWebExtensionsApi ) { return browser . permissions . request ( permission ) ; } return new Promise ( ( resolve , reject ) => { chrome . permissions . request ( permission , resolve ) ; } ) ; } static getPlatformInfo ( ) { if ( BrowserApi . isWebExtensionsApi ) { return browser . runtime . getPlatformInfo ( ) ; } return new Promise ( ( resolve ) => { chrome . runtime . getPlatformInfo ( resolve ) ; } ) ; } }