summaryrefslogtreecommitdiffstats
path: root/browser/extensions/webcompat/shims/rambler-authenticator.js
blob: 0554eabc2ce1034178dff1af25ef2b621b5e0079 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

if (!window.ramblerIdHelper) {
  const originalScript = document.currentScript.src;

  const sendMessageToAddon = (function() {
    const shimId = "Rambler";
    const pendingMessages = new Map();
    const channel = new MessageChannel();
    channel.port1.onerror = console.error;
    channel.port1.onmessage = event => {
      const { messageId, response } = event.data;
      const resolve = pendingMessages.get(messageId);
      if (resolve) {
        pendingMessages.delete(messageId);
        resolve(response);
      }
    };
    function reconnect() {
      const detail = {
        pendingMessages: [...pendingMessages.values()],
        port: channel.port2,
        shimId,
      };
      window.dispatchEvent(new CustomEvent("ShimConnects", { detail }));
    }
    window.addEventListener("ShimHelperReady", reconnect);
    reconnect();
    return function(message) {
      const messageId =
        Math.random()
          .toString(36)
          .substring(2) + Date.now().toString(36);
      return new Promise(resolve => {
        const payload = {
          message,
          messageId,
          shimId,
        };
        pendingMessages.set(messageId, resolve);
        channel.port1.postMessage(payload);
      });
    };
  })();

  const ramblerIdHelper = {
    getProfileInfo: (successCallback, errorCallback) => {
      successCallback({});
    },
    openAuth: () => {
      sendMessageToAddon("optIn").then(function() {
        const openAuthArgs = arguments;
        window.ramblerIdHelper = undefined;
        const s = document.createElement("script");
        s.src = originalScript;
        document.head.appendChild(s);
        s.addEventListener("load", () => {
          const helper = window.ramblerIdHelper;
          for (const { fn, args } of callLog) {
            helper[fn].apply(helper, args);
          }
          helper.openAuth.apply(helper, openAuthArgs);
        });
      });
    },
  };

  const callLog = [];
  function addLoggedCall(fn) {
    ramblerIdHelper[fn] = () => {
      callLog.push({ fn, args: arguments });
    };
  }

  addLoggedCall("registerOnFrameCloseCallback");
  addLoggedCall("registerOnFrameRedirect");
  addLoggedCall("registerOnPossibleLoginCallback");
  addLoggedCall("registerOnPossibleLogoutCallback");
  addLoggedCall("registerOnPossibleOauthLoginCallback");

  window.ramblerIdHelper = ramblerIdHelper;
}