diff options
Diffstat (limited to 'packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts')
-rw-r--r-- | packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts b/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts index ff9cc029a..39b76c341 100644 --- a/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts +++ b/packages/taler-wallet-webextension/src/hooks/useTalerActionURL.ts @@ -1,6 +1,6 @@ /* This file is part of GNU Taler - (C) 2021 Taler Systems S.A. + (C) 2022 Taler Systems S.A. GNU Taler is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -14,46 +14,45 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ -import { classifyTalerUri, TalerUriType } from "@gnu-taler/taler-util"; import { useEffect, useState } from "preact/hooks"; +import { useIocContext } from "../context/iocContext.js"; -export function useTalerActionURL(): [string | undefined, (s: boolean) => void] { - const [talerActionUrl, setTalerActionUrl] = useState<string | undefined>( - undefined +export interface UriLocation { + uri: string; + location: "clipboard" | "activeTab"; +} + +export function useTalerActionURL(): [ + UriLocation | undefined, + (s: boolean) => void, +] { + const [talerActionUrl, setTalerActionUrl] = useState<UriLocation | undefined>( + undefined, ); const [dismissed, setDismissed] = useState(false); + const { findTalerUriInActiveTab, findTalerUriInClipboard } = useIocContext(); useEffect(() => { async function check(): Promise<void> { - const talerUri = await findTalerUriInActiveTab(); - setTalerActionUrl(talerUri) + const clipUri = await findTalerUriInClipboard(); + if (clipUri) { + setTalerActionUrl({ + location: "clipboard", + uri: clipUri, + }); + return; + } + const tabUri = await findTalerUriInActiveTab(); + if (tabUri) { + setTalerActionUrl({ + location: "activeTab", + uri: tabUri, + }); + return; + } } check(); }, []); + const url = dismissed ? undefined : talerActionUrl; return [url, setDismissed]; } - -async function findTalerUriInActiveTab(): Promise<string | undefined> { - return new Promise((resolve, reject) => { - chrome.tabs.executeScript( - { - code: ` - (() => { - let x = document.querySelector("a[href^='taler://'") || document.querySelector("a[href^='taler+http://'"); - return x ? x.href.toString() : null; - })(); - `, - allFrames: false, - }, - (result) => { - if (chrome.runtime.lastError) { - console.error(chrome.runtime.lastError); - resolve(undefined); - return; - } - console.log("got result", result); - resolve(result[0]); - }, - ); - }); -} |