summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutlogins/content/aboutLoginsUtils.mjs
blob: 150f1b18e3968d71355ba8cdf877d81258fab81f (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
/* 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/. */

/**
 * Dispatches a custom event to the AboutLoginsChild.jsm script which
 * will record the event.
 * @param {object} event.method The telemety event method
 * @param {object} event.object The telemety event object
 * @param {object} event.value [optional] The telemety event value
 */
export function recordTelemetryEvent(event) {
  document.dispatchEvent(
    new CustomEvent("AboutLoginsRecordTelemetryEvent", {
      bubbles: true,
      detail: event,
    })
  );
}

export function setKeyboardAccessForNonDialogElements(enableKeyboardAccess) {
  const pageElements = document.querySelectorAll(
    "login-item, login-list, menu-button, login-filter, fxaccounts-button, [tabindex]"
  );

  let { activeElement: docActiveElement } = document;
  if (
    !enableKeyboardAccess &&
    docActiveElement &&
    !docActiveElement.closest("confirmation-dialog")
  ) {
    let elementToBlur =
      docActiveElement?.shadowRoot?.activeElement ?? docActiveElement;
    elementToBlur.blur();
  }

  pageElements.forEach(el => {
    if (!enableKeyboardAccess) {
      if (el.tabIndex > -1) {
        el.dataset.oldTabIndex = el.tabIndex;
      }
      el.tabIndex = "-1";
    } else if (el.dataset.oldTabIndex) {
      el.tabIndex = el.dataset.oldTabIndex;
      delete el.dataset.oldTabIndex;
    } else {
      el.removeAttribute("tabindex");
    }
  });
}

export function promptForPrimaryPassword(messageId) {
  return new Promise(resolve => {
    window.AboutLoginsUtils.promptForPrimaryPassword(resolve, messageId);
  });
}

/**
 * Initializes a dialog based on a template using shadow dom.
 * @param {HTMLElement} element The element to attach the shadow dom to.
 * @param {string} templateSelector The selector of the template to be used.
 * @returns {object} The shadow dom that is attached.
 */
export function initDialog(element, templateSelector) {
  let template = document.querySelector(templateSelector);
  let shadowRoot = element.attachShadow({ mode: "open" });
  document.l10n.connectRoot(shadowRoot);
  shadowRoot.appendChild(template.content.cloneNode(true));
  return shadowRoot;
}