From 583545bae705f8a2f034c716feaa317d49122d81 Mon Sep 17 00:00:00 2001 From: ost-ptk Date: Fri, 28 Apr 2023 15:42:00 +0300 Subject: [PATCH] added event listener for tab updating --- src/background/index.ts | 58 ++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/background/index.ts b/src/background/index.ts index 0636934f5..de5291475 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -156,6 +156,8 @@ const updateOrigin = async (windowId: number) => { } const store = await getExistingMainStoreSingletonOrInit(); + const state = store.getState(); + const activeOrigin = state.activeOrigin; const activeTabs = await browser.tabs.query({ active: true, windowId }); const tab0 = activeTabs[0]; @@ -166,42 +168,50 @@ const updateOrigin = async (windowId: number) => { newActiveOrigin = getUrlOrigin(tab0.url) || null; } - store.dispatch(activeOriginChanged(newActiveOrigin)); - - const state = store.getState(); - const activeAccount = selectVaultActiveAccount(state); - - if (newActiveOrigin && activeAccount) { - const isLocked = selectVaultIsLocked(state); - const isActiveAccountConnected = selectIsAccountConnected( - state, - newActiveOrigin, - activeAccount.name - ); - - emitSdkEventToActiveTabsWithOrigin( - newActiveOrigin, - sdkEvent.changedTab({ - isLocked: isLocked, - isConnected: isLocked ? undefined : isActiveAccountConnected, - activeKey: - !isLocked && isActiveAccountConnected - ? activeAccount.publicKey - : undefined - }) - ); + if (activeOrigin !== newActiveOrigin) { + store.dispatch(activeOriginChanged(newActiveOrigin)); + + const activeAccount = selectVaultActiveAccount(state); + + if (newActiveOrigin && activeAccount) { + const isLocked = selectVaultIsLocked(state); + const isActiveAccountConnected = selectIsAccountConnected( + state, + newActiveOrigin, + activeAccount.name + ); + + emitSdkEventToActiveTabsWithOrigin( + newActiveOrigin, + sdkEvent.changedTab({ + isLocked: isLocked, + isConnected: isLocked ? undefined : isActiveAccountConnected, + activeKey: + !isLocked && isActiveAccountConnected + ? activeAccount.publicKey + : undefined + }) + ); + } } }; browser.windows.onFocusChanged.addListener(async (windowId: number) => { updateOrigin(windowId); }); + browser.tabs.onActivated.addListener( async ({ windowId, tabId }: browser.Tabs.OnActivatedActiveInfoType) => { updateOrigin(windowId); } ); +browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { + if (changeInfo && changeInfo.url && tab.windowId) { + updateOrigin(tab.windowId); + } +}); + // NOTE: if two events are send at the same time (same function) it must reuse the same store instance browser.runtime.onMessage.addListener( async (action: RootAction | SdkMethod | ServiceMessage, sender) => {