let contrasnaUsuario = ""; const buttonDonwland = new CustomEvent("button-donwland", { detail: { message: "Evento boton de descarga" }, }); document .getElementById("generarContraseña") .addEventListener("click", getPassword); document .getElementById("downloadBtn") .addEventListener("click", downloadPassword); document.getElementById("copy-contra").addEventListener("click", copiarPorta); function getPassword() { contrasnaUsuario = autoCreate(12); document.getElementById("showPassword").innerText = contrasnaUsuario; document.getElementById("downloadBtn").disabled = false; document.getElementById("copy-contra").disabled = false; } function getTimeNow() { let date = new Date(); let mes = date.getMonth(); mes = mes.toLocaleString.length == 1 ? `0${mes}` : mes; let segundos = date.getSeconds(); segundos = segundos.toLocaleString().length == 1 ? `0${segundos}` : segundos; return `${date.getFullYear()}-${mes}-${date.getDate()} | ${date.getHours()}:${date.getMinutes()}:${segundos}`; } function autoCreate(plength) { let caracteres = "abcdefghijklmnopqrstubwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; let password = ""; for (i = 0; i < plength; i++) { password += caracteres.charAt( Math.floor(Math.random() * caracteres.length) ); } return password; } function downloadPassword() { let fecha = getTimeNow(); const contenido = `Creado a: ${fecha}\nContraseña: ${contrasnaUsuario}`; const archivo = new Blob([contenido], { type: "text/plain" }); // Crear un enlace temporal para la descarga const enlace = document.createElement("a"); enlace.href = URL.createObjectURL(archivo); enlace.download = "password.txt"; enlace.click(); URL.revokeObjectURL(enlace.href); document.getElementById("downloadBtn").dispatchEvent(buttonDonwland); } function copiarPorta() { const texto = document.getElementById("showPassword").innerText; navigator.clipboard .writeText(texto) .then(function () {}) .catch(function (error) { alert("Error al copiar al portapapeles: ", error); }); }