generateMask: async()

in src/js/popup/popup.js [78:161]


      generateMask: async (event, type = "random", data = null) => {
        
        // Types: "random", "custom"
        sendRelayEvent("Panel", "click", `popup-generate-${type}-mask`);
        preventDefaultBehavior(event);

        const isRandomMask = (type == "random");
        const isCustomMask = (type == "custom");
        const { premium } = await browser.storage.local.get("premium");

        event.target.classList.add("is-loading");

        const newRelayAddressResponseArgs = isCustomMask ?  { method: "makeDomainAddress" } : { method: "makeRelayAddress" }
        
        if (isRandomMask) {
          // When rebuilding panel, scroll to the top of it
          const panel = document.querySelector(".fx-relay-mask-list");
          panel.scrollIntoView(true);
        } 

        // Request the active tab from the background script and parse the `document.location.hostname`
        const currentPageHostName = await browser.runtime.sendMessage({
          method: "getCurrentPageHostname",
        });

        // If active tab is a non-internal browser page, add a label to the creation request
        if (currentPageHostName !== null) {
          newRelayAddressResponseArgs.description = currentPageHostName;
        }

        if (isCustomMask && data) {
          newRelayAddressResponseArgs.address = data.address
          newRelayAddressResponseArgs.block_list_emails = data.block_list_emails
        }

        // Attempt to create a new alias
        const newRelayAddressResponse = await browser.runtime.sendMessage(newRelayAddressResponseArgs);

        // Catch edge cases where the "Generate New Alias" button is still enabled,
        // but the user has already reached the max number of aliases.
        if (newRelayAddressResponse.status === 402) {
          event.target.classList.remove("is-loading");
          throw new Error(
            browser.i18n.getMessage("pageInputIconMaxAliasesError_mask")
          );
        }

        // Reset previous form
        if (premium && isCustomMask) {
            const customMaskDomainInput = document.getElementById("customMaskName");
            customMaskDomainInput.value = "";
            const customMaskBlockPromosCheckbox = document.getElementById("customMaskBlockPromos");
            customMaskBlockPromosCheckbox.checked = false;
        }
        
        // Catch edge cases where the "Generate New Alias" button is still enabled,
        // but the user has already reached the max number of aliases.
        if (newRelayAddressResponse.status === 409 || newRelayAddressResponse.status === 400) {
          event.target.classList.remove("is-loading");
          
          const errorMessage = document.querySelector(".fx-relay-masks-error-message");
          errorMessage.classList.add("is-shown");
          
          errorMessage.addEventListener("click",popup.events.dismissErrorClick, false);

          await popup.panel.masks.utilities.buildMasksList({newMaskCreated: false});
          
          return;
        }

        event.target.classList.remove("is-loading");

        // Hide onboarding panel
        const noMasksCreatedPanel = document.querySelector(".fx-relay-no-masks-created");
        noMasksCreatedPanel.classList.add("is-hidden");

        await popup.panel.masks.utilities.buildMasksList({newMaskCreated: true});

        
        if (!premium) {
          await popup.panel.masks.utilities.setRemainingMaskCount();
        }

      }