diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/platform')
4 files changed, 73 insertions, 11 deletions
diff --git a/packages/taler-wallet-webextension/src/platform/api.ts b/packages/taler-wallet-webextension/src/platform/api.ts index 23fd80ed7..da257c25f 100644 --- a/packages/taler-wallet-webextension/src/platform/api.ts +++ b/packages/taler-wallet-webextension/src/platform/api.ts @@ -37,6 +37,10 @@ export interface CrossBrowserPermissionsApi { requestHostPermissions(): Promise<boolean>; removeHostPermissions(): Promise<boolean>; + containsClipboardPermissions(): Promise<boolean>; + requestClipboardPermissions(): Promise<boolean>; + removeClipboardPermissions(): Promise<boolean>; + addPermissionsListener( callback: (p: Permissions, lastError?: string) => void, ): void; diff --git a/packages/taler-wallet-webextension/src/platform/chrome.ts b/packages/taler-wallet-webextension/src/platform/chrome.ts index 7311354c9..75900882f 100644 --- a/packages/taler-wallet-webextension/src/platform/chrome.ts +++ b/packages/taler-wallet-webextension/src/platform/chrome.ts @@ -77,6 +77,18 @@ const hostPermissions = { origins: ["http://*/*", "https://*/*"], }; +export function containsClipboardPermissions(): Promise<boolean> { + return new Promise((res, rej) => { + chrome.permissions.contains({ permissions: ["clipboardRead"] }, (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) => { @@ -89,6 +101,18 @@ export function containsHostPermissions(): Promise<boolean> { }); } +export async function requestClipboardPermissions(): Promise<boolean> { + return new Promise((res, rej) => { + chrome.permissions.request({ permissions: ["clipboardRead"] }, (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) => { @@ -155,6 +179,18 @@ export async function removeHostPermissions(): Promise<boolean> { }); } +export function removeClipboardPermissions(): Promise<boolean> { + return new Promise((res, rej) => { + chrome.permissions.remove({ permissions: ["clipboardRead"] }, (resp) => { + const le = chrome.runtime.lastError?.message; + if (le) { + rej(le); + } + res(resp); + }); + }); +} + function addPermissionsListener( callback: (p: Permissions, lastError?: string) => void, ): void { @@ -170,6 +206,9 @@ function getPermissionsApi(): CrossBrowserPermissionsApi { containsHostPermissions, requestHostPermissions, removeHostPermissions, + requestClipboardPermissions, + removeClipboardPermissions, + containsClipboardPermissions, }; } @@ -382,11 +421,9 @@ function registerTalerHeaderListener( } 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) @@ -585,7 +622,6 @@ async function registerIconChangeOnTalerContent(): Promise<void> { chrome.tabs.onUpdated.addListener( async (tabId, info: chrome.tabs.TabChangeInfo) => { if (tabId < 0) return; - logger.info("tab updated", tabId, info); if (info.status !== "complete") return; const uri = await findTalerUriInTab(tabId); if (uri) { @@ -690,9 +726,22 @@ async function findTalerUriInTab(tabId: number): Promise<string | undefined> { } } +async function timeout(ms: number): Promise<void> { + return new Promise(resolve => setTimeout(resolve, ms)); +} async function findTalerUriInClipboard(): Promise<string | undefined> { - const textInClipboard = await window.navigator.clipboard.readText(); - return textInClipboard.startsWith("taler://") || textInClipboard.startsWith("taler+http://") ? textInClipboard : undefined + try { + //It looks like clipboard promise does not return, so we need a timeout + const textInClipboard = await Promise.any([ + timeout(100), + window.navigator.clipboard.readText() + ]) + if (!textInClipboard) return; + return textInClipboard.startsWith("taler://") || textInClipboard.startsWith("taler+http://") ? textInClipboard : undefined + } catch (e) { + logger.error("could not read clipboard", e) + return undefined + } } async function findTalerUriInActiveTab(): Promise<string | undefined> { diff --git a/packages/taler-wallet-webextension/src/platform/dev.ts b/packages/taler-wallet-webextension/src/platform/dev.ts index bb7e181c4..8a410b062 100644 --- a/packages/taler-wallet-webextension/src/platform/dev.ts +++ b/packages/taler-wallet-webextension/src/platform/dev.ts @@ -32,6 +32,9 @@ const api: PlatformAPI = { containsHostPermissions: async () => true, removeHostPermissions: async () => false, requestHostPermissions: async () => false, + containsClipboardPermissions: async () => true, + removeClipboardPermissions: async () => false, + requestClipboardPermissions: async () => false, }), getWalletWebExVersion: () => ({ version: "none", diff --git a/packages/taler-wallet-webextension/src/platform/firefox.ts b/packages/taler-wallet-webextension/src/platform/firefox.ts index a56e21f24..943168956 100644 --- a/packages/taler-wallet-webextension/src/platform/firefox.ts +++ b/packages/taler-wallet-webextension/src/platform/firefox.ts @@ -16,9 +16,12 @@ import { CrossBrowserPermissionsApi, Permissions, PlatformAPI } from "./api.js"; import chromePlatform, { - containsHostPermissions as chromeContains, - removeHostPermissions as chromeRemove, - requestHostPermissions as chromeRequest, + containsHostPermissions as chromeHostContains, + removeHostPermissions as chromeHostRemove, + requestHostPermissions as chromeHostRequest, + containsClipboardPermissions as chromeClipContains, + removeClipboardPermissions as chromeClipRemove, + requestClipboardPermissions as chromeClipRequest, } from "./chrome.js"; const api: PlatformAPI = { @@ -43,9 +46,12 @@ function addPermissionsListener(callback: (p: Permissions) => void): void { function getPermissionsApi(): CrossBrowserPermissionsApi { return { addPermissionsListener, - containsHostPermissions: chromeContains, - requestHostPermissions: chromeRequest, - removeHostPermissions: chromeRemove, + containsHostPermissions: chromeHostContains, + requestHostPermissions: chromeHostRequest, + removeHostPermissions: chromeHostRemove, + containsClipboardPermissions: chromeClipContains, + removeClipboardPermissions: chromeClipRemove, + requestClipboardPermissions: chromeClipRequest, }; } |