tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rambler-authenticator.js (2991B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 if (!window.ramblerIdHelper) {
      8  const originalScript = (() => {
      9    const src = document.currentScript?.src;
     10    try {
     11      const { protocol, hostname, pathname, href } = new URL(src);
     12      if (
     13        (protocol === "http:" || protocol === "https:") &&
     14        hostname === "id.rambler.ru" &&
     15        pathname === "rambler-id-helper/auth_events.js"
     16      ) {
     17        return href;
     18      }
     19    } catch (_) {}
     20    return "https://id.rambler.ru/rambler-id-helper/auth_events.js";
     21  })();
     22 
     23  const sendMessageToAddon = (function () {
     24    const shimId = "Rambler";
     25    const pendingMessages = new Map();
     26    const channel = new MessageChannel();
     27    channel.port1.onerror = console.error;
     28    channel.port1.onmessage = event => {
     29      const { messageId, response } = event.data;
     30      const resolve = pendingMessages.get(messageId);
     31      if (resolve) {
     32        pendingMessages.delete(messageId);
     33        resolve(response);
     34      }
     35    };
     36    function reconnect() {
     37      const detail = {
     38        pendingMessages: [...pendingMessages.values()],
     39        port: channel.port2,
     40        shimId,
     41      };
     42      window.dispatchEvent(new CustomEvent("ShimConnects", { detail }));
     43    }
     44    window.addEventListener("ShimHelperReady", reconnect);
     45    reconnect();
     46    return function (message) {
     47      const messageId =
     48        Math.random().toString(36).substring(2) + Date.now().toString(36);
     49      return new Promise(resolve => {
     50        const payload = {
     51          message,
     52          messageId,
     53          shimId,
     54        };
     55        pendingMessages.set(messageId, resolve);
     56        channel.port1.postMessage(payload);
     57      });
     58    };
     59  })();
     60 
     61  const ramblerIdHelper = {
     62    getProfileInfo: (successCallback, _errorCallback) => {
     63      successCallback({});
     64    },
     65    openAuth: () => {
     66      sendMessageToAddon("optIn").then(function () {
     67        const openAuthArgs = arguments;
     68        window.ramblerIdHelper = undefined;
     69        const s = document.createElement("script");
     70        s.src = originalScript;
     71        document.head.appendChild(s);
     72        s.addEventListener("load", () => {
     73          const helper = window.ramblerIdHelper;
     74          for (const { fn, args } of callLog) {
     75            helper[fn].apply(helper, args);
     76          }
     77          helper.openAuth.apply(helper, openAuthArgs);
     78        });
     79      });
     80    },
     81  };
     82 
     83  const callLog = [];
     84  function addLoggedCall(fn) {
     85    ramblerIdHelper[fn] = () => {
     86      callLog.push({ fn, args: arguments });
     87    };
     88  }
     89 
     90  addLoggedCall("registerOnFrameCloseCallback");
     91  addLoggedCall("registerOnFrameRedirect");
     92  addLoggedCall("registerOnPossibleLoginCallback");
     93  addLoggedCall("registerOnPossibleLogoutCallback");
     94  addLoggedCall("registerOnPossibleOauthLoginCallback");
     95 
     96  window.ramblerIdHelper = ramblerIdHelper;
     97 }