aboutLoginsUtils.mjs (2409B)
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 export const CONCEALED_PASSWORD_TEXT = " ".repeat(8); 6 7 /** 8 * Dispatches a custom event to the AboutLoginsChild.sys.mjs script which 9 * will record the event. 10 * 11 * @param {object} event.method The telemety event method 12 * @param {object} event.object The telemety event object 13 * @param {object} event.value [optional] The telemety event value 14 */ 15 export function recordTelemetryEvent(event) { 16 document.dispatchEvent( 17 new CustomEvent("AboutLoginsRecordTelemetryEvent", { 18 bubbles: true, 19 detail: event, 20 }) 21 ); 22 } 23 24 export function setKeyboardAccessForNonDialogElements(enableKeyboardAccess) { 25 const pageElements = document.querySelectorAll( 26 "login-item, login-list, menu-button, login-filter, fxaccounts-button, [tabindex]" 27 ); 28 29 let { activeElement: docActiveElement } = document; 30 if ( 31 !enableKeyboardAccess && 32 docActiveElement && 33 !docActiveElement.closest("confirmation-dialog") 34 ) { 35 let elementToBlur = 36 docActiveElement?.shadowRoot?.activeElement ?? docActiveElement; 37 elementToBlur.blur(); 38 } 39 40 pageElements.forEach(el => { 41 if (!enableKeyboardAccess) { 42 if (el.tabIndex > -1) { 43 el.dataset.oldTabIndex = el.tabIndex; 44 } 45 el.tabIndex = "-1"; 46 } else if (el.dataset.oldTabIndex) { 47 el.tabIndex = el.dataset.oldTabIndex; 48 delete el.dataset.oldTabIndex; 49 } else { 50 el.removeAttribute("tabindex"); 51 } 52 }); 53 } 54 55 export function promptForPrimaryPassword(messageId, reason) { 56 return new Promise(resolve => { 57 window.AboutLoginsUtils.promptForPrimaryPassword( 58 resolve, 59 messageId, 60 reason 61 ); 62 }); 63 } 64 65 /** 66 * Initializes a dialog based on a template using shadow dom. 67 * 68 * @param {HTMLElement} element The element to attach the shadow dom to. 69 * @param {string} templateSelector The selector of the template to be used. 70 * @returns {object} The shadow dom that is attached. 71 */ 72 export function initDialog(element, templateSelector) { 73 let template = document.querySelector(templateSelector); 74 let shadowRoot = element.attachShadow({ mode: "open" }); 75 document.l10n.connectRoot(shadowRoot); 76 shadowRoot.appendChild(template.content.cloneNode(true)); 77 return shadowRoot; 78 }