tor-browser

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

aboutwelcome-utils.mjs (2971B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // If the container has a "page" data attribute, then this is
      6 // a Spotlight modal or Feature Callout. Otherwise, this is
      7 // about:welcome and we should return the current page.
      8 const page =
      9  document.querySelector(
     10    "#multi-stage-message-root.onboardingContainer[data-page]"
     11  )?.dataset.page || document.location.href;
     12 
     13 export const AboutWelcomeUtils = {
     14  handleUserAction(action) {
     15    return window.AWSendToParent("SPECIAL_ACTION", action);
     16  },
     17  sendImpressionTelemetry(messageId, context) {
     18    window.AWSendEventTelemetry?.({
     19      event: "IMPRESSION",
     20      event_context: {
     21        ...context,
     22        page,
     23      },
     24      message_id: messageId,
     25    });
     26  },
     27  sendActionTelemetry(messageId, elementId, eventName = "CLICK_BUTTON") {
     28    const ping = {
     29      event: eventName,
     30      event_context: {
     31        source: elementId,
     32        page,
     33      },
     34      message_id: messageId,
     35    };
     36    window.AWSendEventTelemetry?.(ping);
     37  },
     38  sendDismissTelemetry(messageId, elementId) {
     39    // Don't send DISMISS telemetry in spotlight modals since they already send
     40    // their own equivalent telemetry.
     41    if (page !== "spotlight") {
     42      this.sendActionTelemetry(messageId, elementId, "DISMISS");
     43    }
     44  },
     45  async fetchFlowParams(metricsFlowUri) {
     46    let flowParams;
     47    try {
     48      const response = await fetch(metricsFlowUri, {
     49        credentials: "omit",
     50      });
     51      if (response.status === 200) {
     52        const { deviceId, flowId, flowBeginTime } = await response.json();
     53        flowParams = { deviceId, flowId, flowBeginTime };
     54      } else {
     55        console.error("Non-200 response", response);
     56      }
     57    } catch (e) {
     58      flowParams = null;
     59    }
     60    return flowParams;
     61  },
     62  sendEvent(type, detail) {
     63    document.dispatchEvent(
     64      new CustomEvent(`AWPage:${type}`, {
     65        bubbles: true,
     66        detail,
     67      })
     68    );
     69  },
     70  getLoadingStrategyFor(url) {
     71    return url?.startsWith("http") ? "lazy" : "eager";
     72  },
     73  handleCampaignAction(action, messageId) {
     74    window.AWSendToParent("HANDLE_CAMPAIGN_ACTION", action).then(handled => {
     75      if (handled) {
     76        this.sendActionTelemetry(messageId, "CAMPAIGN_ACTION");
     77      }
     78    });
     79  },
     80  getValidStyle(style, validStyles, allowVars) {
     81    if (!style) {
     82      return null;
     83    }
     84    return Object.keys(style)
     85      .filter(
     86        key => validStyles.includes(key) || (allowVars && key.startsWith("--"))
     87      )
     88      .reduce((obj, key) => {
     89        obj[key] = style[key];
     90        return obj;
     91      }, {});
     92  },
     93  getTileStyle(tile, validStyle) {
     94    const preferredTileStyle = tile?.style;
     95    const legacyTileStyle = tile?.tiles?.style ?? null;
     96 
     97    return this.getValidStyle(
     98      preferredTileStyle ?? legacyTileStyle,
     99      validStyle,
    100      true
    101    );
    102  },
    103 };