summaryrefslogtreecommitdiff
path: root/packages/taler-wallet-webextension/src/taler-wallet-interaction-loader.ts
blob: 838b47397c546dcfb40ecbaf61296fdbc3a25672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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 <http://www.gnu.org/licenses/>
 */

/**
 * 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 <script /> element that load the support in the page context.
 * The interaction support script will create the API to send message
 * that will be received by this loader and be redirected to the extension
 * using the bridge.
 */
function injectTalerSupportScript(debugEnabled: boolean) {
  const container = document.head || document.documentElement;
  const scriptTag = document.createElement("script");

  scriptTag.setAttribute("async", "false");
  const url = new URL(
    chrome.runtime.getURL("/dist/taler-wallet-interaction-support.js"),
  );
  url.searchParams.set("id", chrome.runtime.id);
  if (debugEnabled) {
    url.searchParams.set("debug", "true");
  }
  scriptTag.src = url.href;
  try {
    container.insertBefore(scriptTag, container.children[0]);
  } catch (e) {
    logger.info("inserting link handler failed!");
    logger.error(e);
  }
}

/**
 * Create a bridge connection between the page and the extension.
 *
 * Useful for API calls and replies. Not yet supported.
 */
function createBridgeWithExtension() {
  const port = chrome.runtime.connect();

  window.addEventListener(
    "message",
    (event) => {
      logger.debug("message received", event);
      if (event.source !== window) {
        return;
      }
      if (event.origin !== window.origin) {
        return;
      }

      if (event.data.type && event.data.type === "FROM_PAGE") {
        logger.debug("Content script received: " + event.data.text);
        port.postMessage(event.data.text);
      }
    },
    false,
  );
}

start();