From eb4f2889486739316094d05e6b4a92ea054beddd Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 12 Jan 2018 14:02:48 -0500 Subject: [PATCH] generate random ids for window and tabs --- src/browser/browserApi.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index 7a9ad88a47..e70807541e 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -44,12 +44,10 @@ class BrowserApi { const returnedTabs: any[] = []; tabs.forEach((tab: any) => { - const winIndex = safari.application.browserWindows.indexOf(tab.browserWindow); - const tabIndex = tab.browserWindow.tabs.indexOf(tab); returnedTabs.push({ - id: winIndex + '_' + tabIndex, - index: tabIndex, - windowId: winIndex, + id: BrowserApi.getTabOrWindowId(tab), + index: tab.browserWindow.tabs.indexOf(tab), + windowId: BrowserApi.getTabOrWindowId(tab.browserWindow), title: tab.title, active: tab === tab.browserWindow.activeTab, url: tab.url || 'about:blank', @@ -156,6 +154,22 @@ class BrowserApi { // TODO } } + + private static getTabOrWindowId(tabOrWindow: any) { + if (tabOrWindow.id) { + return tabOrWindow.id; + } + + if (!tabOrWindow.BitwardenCachedId) { + tabOrWindow.BitwardenCachedId = BrowserApi.randomInt(1, Number.MAX_SAFE_INTEGER); + } + + return tabOrWindow.BitwardenCachedId; + } + + private static randomInt(min: number, max: number): number { + return Math.floor(Math.random() * (max - min + 1)) + min; + } } export { BrowserApi };