add a tab update listener to look for taler://

This commit is contained in:
Sebastian 2022-09-07 12:46:06 -03:00
parent ef1aab5229
commit 2279ee2c39
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -105,22 +105,30 @@ type HeaderListenerFunc = (
) => void; ) => void;
let currentHeaderListener: HeaderListenerFunc | undefined = undefined; let currentHeaderListener: HeaderListenerFunc | undefined = undefined;
type TabListenerFunc = (
tabId: number, info: chrome.tabs.TabChangeInfo,
) => void;
let currentTabListener: TabListenerFunc | undefined = undefined;
export function containsTalerHeaderListener(): boolean { export function containsTalerHeaderListener(): boolean {
return currentHeaderListener !== undefined; return currentHeaderListener !== undefined || currentTabListener !== undefined;
} }
export async function removeHostPermissions(): Promise<boolean> { export async function removeHostPermissions(): Promise<boolean> {
//if there is a handler already, remove it //if there is a handler already, remove it
if ( if (
"webRequest" in chrome &&
"onHeadersReceived" in chrome.webRequest &&
currentHeaderListener && currentHeaderListener &&
chrome.webRequest.onHeadersReceived.hasListener(currentHeaderListener) chrome?.webRequest?.onHeadersReceived?.hasListener(currentHeaderListener)
) { ) {
chrome.webRequest.onHeadersReceived.removeListener(currentHeaderListener); chrome.webRequest.onHeadersReceived.removeListener(currentHeaderListener);
} }
if (currentTabListener &&
chrome?.tabs?.onUpdated?.hasListener(currentTabListener)) {
chrome.tabs.onUpdated.removeListener(currentTabListener)
}
currentHeaderListener = undefined; currentHeaderListener = undefined;
currentTabListener = undefined;
//notify the browser about this change, this operation is expensive //notify the browser about this change, this operation is expensive
if ("webRequest" in chrome) { if ("webRequest" in chrome) {
@ -365,12 +373,26 @@ function registerTalerHeaderListener(
.map((h) => h.value) .map((h) => h.value)
.filter((value): value is string => !!value); .filter((value): value is string => !!value);
if (values.length > 0) { 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]); callback(details.tabId, values[0]);
} }
} }
return; 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 prevHeaderListener = currentHeaderListener;
const prevTabListener = currentTabListener;
getPermissionsApi() getPermissionsApi()
.containsHostPermissions() .containsHostPermissions()
@ -382,17 +404,29 @@ function registerTalerHeaderListener(
) { ) {
chrome.webRequest.onHeadersReceived.removeListener(prevHeaderListener); 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 the result was positive, add the headerListener
if (result) { if (result) {
const listener: chrome.webRequest.WebResponseHeadersEvent | undefined = const headersEvent: chrome.webRequest.WebResponseHeadersEvent | undefined =
chrome?.webRequest?.onHeadersReceived; chrome?.webRequest?.onHeadersReceived;
if (listener) { if (headersEvent) {
listener.addListener(headerListener, { urls: ["<all_urls>"] }, [ headersEvent.addListener(headerListener, { urls: ["<all_urls>"] }, [
"responseHeaders", "responseHeaders",
]); ]);
currentHeaderListener = headerListener; 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 //notify the browser about this change, this operation is expensive
chrome?.webRequest?.handlerBehaviorChanged(() => { chrome?.webRequest?.handlerBehaviorChanged(() => {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
@ -400,6 +434,7 @@ function registerTalerHeaderListener(
} }
}); });
}); });
} }
const alertIcons = { const alertIcons = {