File size: 2,098 Bytes
d26abee
 
2947f68
 
 
 
d26abee
 
 
 
 
 
 
 
 
 
99a7594
d26abee
 
 
 
 
 
 
 
 
 
 
 
 
99a7594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d26abee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2947f68
 
d26abee
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
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);
    });
}