diff options
author | Sebastian <sebasjm@gmail.com> | 2023-04-18 10:47:53 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-04-18 10:47:53 -0300 |
commit | b34f3568e8c9dde73f11583a6b46ef1509990397 (patch) | |
tree | 2898223d8e67023dbfd2d338eb145bd06b737a71 /packages/taler-wallet-webextension/src/platform/chrome.ts | |
parent | b1a0d034fc1be0824e1eac46661604558264beee (diff) | |
download | wallet-core-b34f3568e8c9dde73f11583a6b46ef1509990397.tar.xz |
check if the script should be injected
remove header listener
Diffstat (limited to 'packages/taler-wallet-webextension/src/platform/chrome.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/platform/chrome.ts | 295 |
1 files changed, 106 insertions, 189 deletions
diff --git a/packages/taler-wallet-webextension/src/platform/chrome.ts b/packages/taler-wallet-webextension/src/platform/chrome.ts index 03259314e..51cf2f64e 100644 --- a/packages/taler-wallet-webextension/src/platform/chrome.ts +++ b/packages/taler-wallet-webextension/src/platform/chrome.ts @@ -31,10 +31,13 @@ import { MessageFromFrontend, MessageResponse, Permissions, + Settings, + defaultSettings, } from "./api.js"; const api: BackgroundPlatformAPI & ForegroundPlatformAPI = { isFirefox, + getSettingsFromStorage, findTalerUriInActiveTab, findTalerUriInClipboard, getPermissionsApi, @@ -49,11 +52,9 @@ const api: BackgroundPlatformAPI & ForegroundPlatformAPI = { registerOnInstalled, listenToAllChannels: listenToAllChannels as any, registerReloadOnNewVersion, - registerTalerHeaderListener, sendMessageToAllChannels, sendMessageToBackground, useServiceWorkerAsBackgroundProcess, - containsTalerHeaderListener, keepAlive, }; @@ -61,6 +62,19 @@ export default api; const logger = new Logger("chrome.ts"); +async function getSettingsFromStorage(): Promise<Settings> { + const data = await chrome.storage.local.get("wallet-settings"); + if (!data) return defaultSettings; + const settings = data["wallet-settings"]; + if (!settings) return defaultSettings; + try { + const parsed = JSON.parse(settings); + return parsed; + } catch (e) { + return defaultSettings; + } +} + function keepAlive(callback: any): void { if (extensionIsManifestV3()) { chrome.alarms.create("wallet-worker", { periodInMinutes: 1 }); @@ -78,10 +92,10 @@ function isFirefox(): boolean { return false; } -const hostPermissions = { - permissions: ["webRequest"], - origins: ["http://*/*", "https://*/*"], -}; +// const hostPermissions = { +// permissions: ["webRequest"], +// origins: ["http://*/*", "https://*/*"], +// }; export function containsClipboardPermissions(): Promise<boolean> { return new Promise((res, rej) => { @@ -96,17 +110,17 @@ export function containsClipboardPermissions(): Promise<boolean> { }); } -export function containsHostPermissions(): Promise<boolean> { - return new Promise((res, rej) => { - chrome.permissions.contains(hostPermissions, (resp) => { - const le = chrome.runtime.lastError?.message; - if (le) { - rej(le); - } - res(resp); - }); - }); -} +// export function containsHostPermissions(): Promise<boolean> { +// return new Promise((res, rej) => { +// chrome.permissions.contains(hostPermissions, (resp) => { +// const le = chrome.runtime.lastError?.message; +// if (le) { +// rej(le); +// } +// res(resp); +// }); +// }); +// } export async function requestClipboardPermissions(): Promise<boolean> { return new Promise((res, rej) => { @@ -121,73 +135,67 @@ export async function requestClipboardPermissions(): Promise<boolean> { }); } -export async function requestHostPermissions(): Promise<boolean> { - return new Promise((res, rej) => { - chrome.permissions.request(hostPermissions, (resp) => { - const le = chrome.runtime.lastError?.message; - if (le) { - rej(le); - } - res(resp); - }); - }); -} - -type HeaderListenerFunc = ( - details: chrome.webRequest.WebResponseHeadersDetails, -) => 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 || currentTabListener !== undefined - ); -} - -export async function removeHostPermissions(): Promise<boolean> { - //if there is a handler already, remove it - if ( - 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) { - chrome.webRequest.handlerBehaviorChanged(() => { - if (chrome.runtime.lastError) { - logger.error(JSON.stringify(chrome.runtime.lastError)); - } - }); - } - - if (extensionIsManifestV3()) { - // Trying to remove host permissions with manifest >= v3 throws an error - return true; - } - return new Promise((res, rej) => { - chrome.permissions.remove(hostPermissions, (resp) => { - const le = chrome.runtime.lastError?.message; - if (le) { - rej(le); - } - res(resp); - }); - }); -} +// export async function requestHostPermissions(): Promise<boolean> { +// return new Promise((res, rej) => { +// chrome.permissions.request(hostPermissions, (resp) => { +// const le = chrome.runtime.lastError?.message; +// if (le) { +// rej(le); +// } +// res(resp); +// }); +// }); +// } + +// type HeaderListenerFunc = ( +// details: chrome.webRequest.WebResponseHeadersDetails, +// ) => void; +// let currentHeaderListener: HeaderListenerFunc | undefined = undefined; + +// type TabListenerFunc = (tabId: number, info: chrome.tabs.TabChangeInfo) => void; +// let currentTabListener: TabListenerFunc | undefined = undefined; + +// export async function removeHostPermissions(): Promise<boolean> { +// //if there is a handler already, remove it +// if ( +// 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) { +// chrome.webRequest.handlerBehaviorChanged(() => { +// if (chrome.runtime.lastError) { +// logger.error(JSON.stringify(chrome.runtime.lastError)); +// } +// }); +// } + +// if (extensionIsManifestV3()) { +// // Trying to remove host permissions with manifest >= v3 throws an error +// return true; +// } +// return new Promise((res, rej) => { +// chrome.permissions.remove(hostPermissions, (resp) => { +// const le = chrome.runtime.lastError?.message; +// if (le) { +// rej(le); +// } +// res(resp); +// }); +// }); +// } export function removeClipboardPermissions(): Promise<boolean> { return new Promise((res, rej) => { @@ -214,9 +222,9 @@ function addPermissionsListener( function getPermissionsApi(): CrossBrowserPermissionsApi { return { addPermissionsListener, - containsHostPermissions, - requestHostPermissions, - removeHostPermissions, + // containsHostPermissions, + // requestHostPermissions, + // removeHostPermissions, requestClipboardPermissions, removeClipboardPermissions, containsClipboardPermissions, @@ -227,12 +235,16 @@ function getPermissionsApi(): CrossBrowserPermissionsApi { * * @param callback function to be called */ -function notifyWhenAppIsReady(callback: () => void): void { - if (extensionIsManifestV3()) { - callback(); - } else { - window.addEventListener("load", callback); - } +function notifyWhenAppIsReady(): Promise<void> { + return new Promise((resolve, reject) => { + if (extensionIsManifestV3()) { + resolve(); + } else { + window.addEventListener("load", () => { + resolve(); + }); + } + }); } function openWalletURIFromPopup(maybeTalerUri: string): void { @@ -478,101 +490,6 @@ function getWalletWebExVersion(): WalletVersion { return manifestData; } -function registerTalerHeaderListener( - callback: (tabId: number, url: string) => void, -): void { - logger.trace("setting up header listener"); - - function headerListener( - details: chrome.webRequest.WebResponseHeadersDetails, - ): void { - if (chrome.runtime.lastError) { - logger.error(JSON.stringify(chrome.runtime.lastError)); - return; - } - if ( - details.statusCode === 402 || - details.statusCode === 202 || - details.statusCode === 200 - ) { - const values = (details.responseHeaders || []) - .filter((h) => h.name.toLowerCase() === "taler") - .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> { - if (tabId < 0) return; - const tabLocationHasBeenUpdated = info.status === "complete"; - const tabTitleHasBeenUpdated = info.title !== undefined; - if (tabLocationHasBeenUpdated || tabTitleHasBeenUpdated) { - const uri = await findTalerUriInTab(tabId); - if (!uri) return; - logger.info(`Found a Taler URI in the tab ${tabId}`); - callback(tabId, uri); - } - } - - const prevHeaderListener = currentHeaderListener; - const prevTabListener = currentTabListener; - - getPermissionsApi() - .containsHostPermissions() - .then((result) => { - //if there is a handler already, remove it - if ( - prevHeaderListener && - chrome?.webRequest?.onHeadersReceived?.hasListener(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 (result) { - const headersEvent: - | chrome.webRequest.WebResponseHeadersEvent - | undefined = chrome?.webRequest?.onHeadersReceived; - 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) { - logger.error(JSON.stringify(chrome.runtime.lastError)); - } - }); - }); -} - const alertIcons = { "16": "/static/img/taler-alert-16.png", "19": "/static/img/taler-alert-19.png", @@ -750,7 +667,7 @@ function registerOnInstalled(callback: () => void): void { if (details.reason === chrome.runtime.OnInstalledReason.INSTALL) { callback(); } - registerIconChangeOnTalerContent(); + await registerIconChangeOnTalerContent(); }); } |