diff options
author | Sebastian <sebasjm@gmail.com> | 2022-09-07 12:46:06 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-09-07 12:48:16 -0300 |
commit | 2279ee2c39db5eb1a9dd5ea719043d1e4ebc7141 (patch) | |
tree | d3a4080798681e11f8e59b27e55bab50051fd47d /packages/taler-wallet-webextension/src | |
parent | ef1aab52299ba5767fb482a66f1c340351f9eecc (diff) |
add a tab update listener to look for taler://
Diffstat (limited to 'packages/taler-wallet-webextension/src')
-rw-r--r-- | packages/taler-wallet-webextension/src/platform/chrome.ts | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/packages/taler-wallet-webextension/src/platform/chrome.ts b/packages/taler-wallet-webextension/src/platform/chrome.ts index f888d131e..4ce995bd7 100644 --- a/packages/taler-wallet-webextension/src/platform/chrome.ts +++ b/packages/taler-wallet-webextension/src/platform/chrome.ts @@ -105,22 +105,30 @@ type HeaderListenerFunc = ( ) => void; let currentHeaderListener: HeaderListenerFunc | undefined = undefined; +type TabListenerFunc = ( + tabId: number, info: chrome.tabs.TabChangeInfo, +) => void; +let currentTabListener: TabListenerFunc | undefined = undefined; + export function containsTalerHeaderListener(): boolean { - return currentHeaderListener !== undefined; + return currentHeaderListener !== undefined || currentTabListener !== undefined; } export async function removeHostPermissions(): Promise<boolean> { //if there is a handler already, remove it if ( - "webRequest" in chrome && - "onHeadersReceived" in chrome.webRequest && currentHeaderListener && - chrome.webRequest.onHeadersReceived.hasListener(currentHeaderListener) + chrome?.webRequest?.onHeadersReceived?.hasListener(currentHeaderListener) ) { chrome.webRequest.onHeadersReceived.removeListener(currentHeaderListener); } + if (currentTabListener && + chrome?.tabs?.onUpdated?.hasListener(currentTabListener)) { + chrome.tabs.onUpdated.removeListener(currentTabListener) + } currentHeaderListener = undefined; + currentTabListener = undefined; //notify the browser about this change, this operation is expensive if ("webRequest" in chrome) { @@ -365,12 +373,26 @@ function registerTalerHeaderListener( .map((h) => h.value) .filter((value): value is string => !!value); if (values.length > 0) { + logger.info(`Found a Taler URI in a response header for the request ${details.url} from tab ${details.tabId}`) callback(details.tabId, values[0]); } } return; } + + async function tabListener(tabId: number, info: chrome.tabs.TabChangeInfo): Promise<void> { + console.log("tab update", tabId, info) + if (tabId < 0) return; + if (info.status !== "complete") return; + const uri = await findTalerUriInTab(tabId); + console.log("uri", uri) + if (!uri) return; + logger.info(`Found a Taler URI in the tab ${tabId}`) + callback(tabId, uri) + } + const prevHeaderListener = currentHeaderListener; + const prevTabListener = currentTabListener; getPermissionsApi() .containsHostPermissions() @@ -382,17 +404,29 @@ function registerTalerHeaderListener( ) { chrome.webRequest.onHeadersReceived.removeListener(prevHeaderListener); } + if (prevTabListener && chrome?.tabs?.onUpdated?.hasListener(prevTabListener)) { + chrome.tabs.onUpdated.removeListener(prevTabListener) + } + //if the result was positive, add the headerListener if (result) { - const listener: chrome.webRequest.WebResponseHeadersEvent | undefined = + const headersEvent: chrome.webRequest.WebResponseHeadersEvent | undefined = chrome?.webRequest?.onHeadersReceived; - if (listener) { - listener.addListener(headerListener, { urls: ["<all_urls>"] }, [ + if (headersEvent) { + headersEvent.addListener(headerListener, { urls: ["<all_urls>"] }, [ "responseHeaders", ]); currentHeaderListener = headerListener; } + + const tabsEvent: chrome.tabs.TabUpdatedEvent | undefined = + chrome?.tabs?.onUpdated; + if (tabsEvent) { + tabsEvent.addListener(tabListener); + currentTabListener = tabListener; + } } + //notify the browser about this change, this operation is expensive chrome?.webRequest?.handlerBehaviorChanged(() => { if (chrome.runtime.lastError) { @@ -400,6 +434,7 @@ function registerTalerHeaderListener( } }); }); + } const alertIcons = { |