|
| 1 | +import { generateAleatoryEmails, generateCustomEmails, generateHumanEmails } from "./modules/generateEmail.js"; |
| 2 | + |
| 3 | +const d = document; |
| 4 | +const $ = (e) => d.querySelector(e); |
| 5 | +const $a = (e) => d.querySelectorAll(e); |
| 6 | + |
| 7 | +export const app = (e) => { |
| 8 | + let path = window.location.pathname.split(".")[0]; |
| 9 | + if (path === "/email-generator/index" || path === "/email-generator/") { |
| 10 | + document.title = "Generador Correos"; |
| 11 | + |
| 12 | + if ($("#aleatory_email").checked) { |
| 13 | + $("#form-container").insertAdjacentHTML("beforeend", "<aleatory-email-component></aleatory-email-component>"); |
| 14 | + } |
| 15 | + |
| 16 | + d.addEventListener("submit", async (e) => { |
| 17 | + // ALEATORY EMAILS |
| 18 | + if (e.target.matches("#generate-aleatory-emails")) { |
| 19 | + e.preventDefault(); |
| 20 | + $("#email-response").innerHTML = ""; |
| 21 | + let data = Object.fromEntries(new FormData(e.target)); |
| 22 | + if (!data.length_email && !isNaN(data.length_email)) { |
| 23 | + alert("Longitud del email es requerido"); |
| 24 | + } else if (!data.quantity_email && !isNaN(data.length_email)) { |
| 25 | + alert("Cantidad es requerida"); |
| 26 | + } else { |
| 27 | + if (!(Number(data.length_email) > 3 && Number(data.length_email) <= 40)) { |
| 28 | + alert("Longitud no valida"); |
| 29 | + } else if (!(Number(data.quantity_email) <= 100)) { |
| 30 | + alert("Cantidad no valida"); |
| 31 | + } else if (!(/^[a-z0-9.-]+\.[a-z]{2,6}$/.test(data.domain_email))) { |
| 32 | + alert("Dominio invalido, no agregues @"); |
| 33 | + } else { |
| 34 | + let correos = generateAleatoryEmails(data); |
| 35 | + correos.forEach((e) => { |
| 36 | + $("#email-response").insertAdjacentHTML("beforeend", `<li class="list-group-item">${e}</li>`); |
| 37 | + }) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // HUMAN EMAILS |
| 43 | + if (e.target.matches("#generate-human-emails")) { |
| 44 | + e.preventDefault(); |
| 45 | + $("#email-response").innerHTML = ""; |
| 46 | + let data = Object.fromEntries(new FormData(e.target)); |
| 47 | + if (!data.quantity_email && !isNaN(data.length_email)) { |
| 48 | + alert("Cantidad es requerida"); |
| 49 | + } else { |
| 50 | + if (!(Number(data.quantity_email) <= 100)) { |
| 51 | + alert("Cantidad no valida"); |
| 52 | + } else { |
| 53 | + let correos = await generateHumanEmails(data); |
| 54 | + correos.forEach((e) => { |
| 55 | + $("#email-response").insertAdjacentHTML("beforeend", `<li class="list-group-item">${e}</li>`); |
| 56 | + }) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // CUSTOM EMAILS |
| 62 | + if (e.target.matches("#generate-custom-emails")) { |
| 63 | + e.preventDefault(); |
| 64 | + $("#email-response").innerHTML = ""; |
| 65 | + let data = Object.fromEntries(new FormData(e.target)); |
| 66 | + if (!data.quantity_email && !isNaN(data.length_email)) { |
| 67 | + alert("Cantidad es requerida"); |
| 68 | + } else { |
| 69 | + if (!(/^[a-zA-Z0-9.]+$/.test(data.user_email))) { |
| 70 | + alert("Usuario email no es valido"); |
| 71 | + } else if (!(/^[a-z0-9.-]+\.[a-z]{2,6}$/.test(data.domain_email))) { |
| 72 | + alert("Dominio invalido, no agregues @"); |
| 73 | + } else if (!(Number(data.quantity_email) <= 100)) { |
| 74 | + alert("Cantidad no valida"); |
| 75 | + } else { |
| 76 | + let correos = generateCustomEmails(data); |
| 77 | + correos.forEach((e) => { |
| 78 | + $("#email-response").insertAdjacentHTML("beforeend", `<li class="list-group-item">${e}</li>`); |
| 79 | + }) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + }) |
| 85 | + |
| 86 | + d.addEventListener("input", (e) => { |
| 87 | + if (e.target.matches("[name='type_email']")) { |
| 88 | + $("#form-container").innerHTML = ""; |
| 89 | + $("#email-response").innerHTML = ""; |
| 90 | + |
| 91 | + if (e.target.matches("#aleatory_email")) { |
| 92 | + $("#form-container").insertAdjacentHTML("beforeend", "<aleatory-email-component></aleatory-email-component>") |
| 93 | + } |
| 94 | + if (e.target.matches("#human_email")) { |
| 95 | + $("#form-container").insertAdjacentHTML("beforeend", "<human-email-component></human-email-component>") |
| 96 | + } |
| 97 | + if (e.target.matches("#custom_email")) { |
| 98 | + $("#form-container").insertAdjacentHTML("beforeend", "<custom-email-component></custom-email-component>") |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + d.addEventListener("click", (e) => { |
| 105 | + if (e.target.matches("#settings input[type='checkbox']")) { |
| 106 | + let checkSelected = [...$a("#settings input[type='checkbox']")].filter((checkbox) => checkbox.checked); |
| 107 | + if (checkSelected.length === 0) { |
| 108 | + e.target.checked = true; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (e.target.matches("#copy-emails, #copy-emails *")) { |
| 113 | + const elementosLi = $a('li'); |
| 114 | + let contenidoCopiado = ''; |
| 115 | + |
| 116 | + for (let i = 0; i < elementosLi.length; i++) { |
| 117 | + contenidoCopiado += elementosLi[i].textContent + '\n'; |
| 118 | + } |
| 119 | + |
| 120 | + const temp = document.createElement('textarea'); |
| 121 | + temp.value = contenidoCopiado; |
| 122 | + document.body.appendChild(temp); |
| 123 | + temp.select(); |
| 124 | + document.execCommand('copy'); |
| 125 | + document.body.removeChild(temp); |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | + |
| 130 | +} |
| 131 | + |
0 commit comments