From ebd004195673c58718c7c9d8b8270df28b35b539 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 13 Apr 2023 12:19:00 -0300 Subject: taler wallet interaction support, first version --- .../build-fast-with-linaria.mjs | 2 + .../taler-wallet-webextension/manifest-v2.json | 5 + .../taler-wallet-webextension/manifest-v3.json | 8 +- .../src/taler-wallet-interaction-loader.ts | 135 +++++++++++++++ .../src/taler-wallet-interaction-support.ts | 192 +++++++++++++++++++++ 5 files changed, 341 insertions(+), 1 deletion(-) create mode 100644 packages/taler-wallet-webextension/src/taler-wallet-interaction-loader.ts create mode 100644 packages/taler-wallet-webextension/src/taler-wallet-interaction-support.ts (limited to 'packages/taler-wallet-webextension') diff --git a/packages/taler-wallet-webextension/build-fast-with-linaria.mjs b/packages/taler-wallet-webextension/build-fast-with-linaria.mjs index 1232eac98..44e502b6a 100755 --- a/packages/taler-wallet-webextension/build-fast-with-linaria.mjs +++ b/packages/taler-wallet-webextension/build-fast-with-linaria.mjs @@ -60,6 +60,8 @@ const entryPoints = [ 'src/background.ts', 'src/stories.tsx', 'src/background.dev.ts', + 'src/taler-wallet-interaction-loader.ts', + 'src/taler-wallet-interaction-support.ts', 'src/browserWorkerEntry.ts' ] diff --git a/packages/taler-wallet-webextension/manifest-v2.json b/packages/taler-wallet-webextension/manifest-v2.json index 6adadad98..a5b77168c 100644 --- a/packages/taler-wallet-webextension/manifest-v2.json +++ b/packages/taler-wallet-webextension/manifest-v2.json @@ -26,6 +26,11 @@ "https://*/*", "webRequest" ], + "content_scripts": [{ + "id": "taler-wallet-interaction-support", + "matches": ["file://*/*", "http://*/*", "https://*/*"], + "js": ["dist/taler-wallet-interaction-loader.js"] + }], "protocol_handlers": [ { "protocol": "ext+taler", diff --git a/packages/taler-wallet-webextension/manifest-v3.json b/packages/taler-wallet-webextension/manifest-v3.json index 4e18125b3..68b3e23ee 100644 --- a/packages/taler-wallet-webextension/manifest-v3.json +++ b/packages/taler-wallet-webextension/manifest-v3.json @@ -29,10 +29,16 @@ "optional_permissions": [ "webRequest" ], + "content_scripts": [{ + "id": "taler-wallet-interaction", + "matches": ["file://*/*", "http://*/*", "https://*/*"], + "js": ["dist/taler-wallet-interaction-loader.js"] + }], "web_accessible_resources": [ { "resources": [ - "static/wallet.html" + "static/wallet.html", + "dist/taler-wallet-interaction-support.js" ], "matches": [ "https://*/*", diff --git a/packages/taler-wallet-webextension/src/taler-wallet-interaction-loader.ts b/packages/taler-wallet-webextension/src/taler-wallet-interaction-loader.ts new file mode 100644 index 000000000..838b47397 --- /dev/null +++ b/packages/taler-wallet-webextension/src/taler-wallet-interaction-loader.ts @@ -0,0 +1,135 @@ +/* + This file is part of GNU Taler + (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 + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + +/** + * This will modify all the pages that the user load when navigating with Web Extension enabled + * + * Can't do useful integration since it run in ISOLATED (or equivalent) mode. + * + * If taler support is expected, it will inject a script which will complete the integration. + */ + +// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities#content_script_environment + +// ISOLATED mode in chromium browsers +// https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/renderer/bindings/core/v8/V8BindingDesign.md#world +// X-Ray vision in Firefox +// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Sharing_objects_with_page_scripts#xray_vision_in_firefox + +// *** IMPORTANT *** + +// Content script lifecycle during navigation +// In Firefox: Content scripts remain injected in a web page after the user has navigated away, +// however, window object properties are destroyed. +// In Chrome: Content scripts are destroyed when the user navigates away from a web page. + +const documentDocTypeIsHTML = + window.document.doctype && window.document.doctype.name === "html"; +const suffixIsNotXMLorPDF = + !window.location.pathname.endsWith(".xml") && + !window.location.pathname.endsWith(".pdf"); +const rootElementIsHTML = + document.documentElement.nodeName && + document.documentElement.nodeName.toLowerCase() === "html"; +const pageAcceptsTalerSupport = document.head.querySelector( + "meta[name=taler-support]", +); +// safe check, if one of this is true then taler handler is not useful +// or not expected +const shouldNotInject = + !documentDocTypeIsHTML || + !suffixIsNotXMLorPDF || + // !pageAcceptsTalerSupport || FIXME: removing this before release for testing + !rootElementIsHTML; +const logger = { + debug: (...msg: any[]) => {}, + info: (...msg: any[]) => + console.log(`${new Date().toISOString()} TALER`, ...msg), + error: (...msg: any[]) => + console.error(`${new Date().toISOString()} TALER`, ...msg), +}; + +function start() { + if (shouldNotInject) { + return; + } + const debugEnabled = + pageAcceptsTalerSupport?.getAttribute("debug") === "true"; + if (debugEnabled) { + logger.debug = logger.info; + } + createBridgeWithExtension(); + logger.debug("bridged created"); + injectTalerSupportScript(debugEnabled); + logger.debug("done"); +} + +/** + * Create a