diff --git a/src/background/main.background.ts b/src/background/main.background.ts index 6a1d8969b0..974a505852 100644 --- a/src/background/main.background.ts +++ b/src/background/main.background.ts @@ -135,7 +135,10 @@ export default class MainBackground { this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService, this.i18n2Service); this.lockService = new LockService(this.cipherService, this.folderService, this.collectionService, - this.cryptoService, this.platformUtilsService, this.storageService, this.messagingService); + this.cryptoService, this.platformUtilsService, this.storageService, this.messagingService, async () => { + await this.setIcon(); + await this.refreshBadgeAndMenu(true); + }); this.syncService = new SyncService(this.userService, this.apiService, this.settingsService, this.folderService, this.cipherService, this.cryptoService, this.collectionService, this.storageService, this.messagingService, (expired: boolean) => this.logout(expired)); @@ -214,23 +217,27 @@ export default class MainBackground { await this.actionSetIcon(this.sidebarAction, suffix); } - async refreshBadgeAndMenu() { + async refreshBadgeAndMenu(forLocked: boolean = false) { if (this.isSafari || !chrome.windows || !chrome.contextMenus) { return; } - const tab = await BrowserApi.getTabFromCurrentWindowId(); - if (!tab) { + const menuDisabled = await this.storageService.get(ConstantsService.disableContextMenuItemKey); + if (!menuDisabled) { + await this.buildContextMenu(); + } else { + await this.contextMenusRemoveAll(); + } + + if (forLocked) { + await this.loadMenuAndUpdateBadgeForLockedState(!menuDisabled); + this.onUpdatedRan = this.onReplacedRan = false; return; } - const disabled = await this.storageService.get(ConstantsService.disableContextMenuItemKey); - if (!disabled) { - await this.buildContextMenu(); - await this.contextMenuReady(tab, true); - } else { - await this.contextMenusRemoveAll(); - await this.contextMenuReady(tab, false); + const tab = await BrowserApi.getTabFromCurrentWindow(); + if (tab) { + await this.contextMenuReady(tab, !menuDisabled); } } @@ -406,11 +413,23 @@ export default class MainBackground { this.browserActionSetBadgeText(theText, tabId); this.sidebarActionSetBadgeText(theText, tabId); } catch (e) { - if (contextMenuEnabled) { - await this.loadNoLoginsContextMenuOptions(this.i18nService.vaultLocked); - } - this.browserActionSetBadgeText('', tabId); - this.sidebarActionSetBadgeText('', tabId); + await this.loadMenuAndUpdateBadgeForLockedState(contextMenuEnabled); + } + } + + private async loadMenuAndUpdateBadgeForLockedState(contextMenuEnabled: boolean) { + if (contextMenuEnabled) { + await this.loadNoLoginsContextMenuOptions(this.i18nService.vaultLocked); + } + + const tabs = await BrowserApi.getActiveTabs(); + if (tabs != null) { + tabs.forEach((tab) => { + if (tab.id != null) { + this.browserActionSetBadgeText('', tab.id); + this.sidebarActionSetBadgeText('', tab.id); + } + }); } } diff --git a/src/background/runtime.background.ts b/src/background/runtime.background.ts index e441ed9fc8..c5aa1d9146 100644 --- a/src/background/runtime.background.ts +++ b/src/background/runtime.background.ts @@ -77,7 +77,7 @@ export default class RuntimeBackground { case 'unlocked': case 'locked': await this.main.setIcon(); - await this.main.refreshBadgeAndMenu(); + await this.main.refreshBadgeAndMenu(msg.command === 'locked'); break; case 'logout': await this.main.logout(msg.expired); diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 9badd89ed7..4fcd259957 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -1,6 +1,6 @@ export class BrowserApi { static isSafariApi: boolean = (typeof safari !== 'undefined') && - navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1; + navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1; static isChromeApi: boolean = !BrowserApi.isSafariApi && (typeof chrome !== 'undefined'); static async getTabFromCurrentWindowId(): Promise { @@ -21,6 +21,12 @@ export class BrowserApi { }); } + static async getActiveTabs(): Promise { + return await BrowserApi.tabsQuery({ + active: true, + }); + } + static tabsQuery(options: any): Promise { if (BrowserApi.isChromeApi) { return new Promise((resolve) => { @@ -29,23 +35,28 @@ export class BrowserApi { }); }); } else if (BrowserApi.isSafariApi) { - let win: any = null; + let wins: any[] = []; if (options.currentWindow) { - win = safari.application.activeBrowserWindow; - } - - if (!win || !win.tabs || !win.tabs.length) { - return Promise.resolve([]); - } - - const tabs: any[] = []; - if (options.active && win.activeTab) { - tabs.push(win.activeTab); + if (safari.application.activeBrowserWindow) { + wins.push(safari.application.activeBrowserWindow); + } + } else { + wins = safari.application.browserWindows; } const returnedTabs: any[] = []; - tabs.forEach((tab: any) => { - returnedTabs.push(BrowserApi.makeTabObject(tab)); + wins.forEach((win: any) => { + if (!win.tabs) { + return; + } + + if (options.active && win.activeTab) { + returnedTabs.push(BrowserApi.makeTabObject(win.activeTab)); + } else if (!options.active) { + win.tabs.forEach((tab: any) => { + returnedTabs.push(BrowserApi.makeTabObject(tab)); + }); + } }); return Promise.resolve(returnedTabs);