Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
110 lines (90 loc) · 3.07 KB
/
Copy pathscript.js
File metadata and controls
110 lines (90 loc) · 3.07 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const inputSenha = document.getElementById("senha")
const inputTamanho = document.getElementById("tamanho")
const valorTamanho = document.getElementById("valorTamanho")
const chkMaiusculas = document.getElementById("maiusculas")
const chkMinusculas = document.getElementById("minusculas")
const chkNumeros = document.getElementById("numeros")
const chkSimbolos = document.getElementById("simbolos")
const textoForca = document.getElementById("textoForca")
const nivelForca = document.getElementById("nivelForca")
inputTamanho.addEventListener("input", () => {
valorTamanho.textContent = inputTamanho.value
if (inputSenha.value) {
atualizarForcaSenha(inputSenha.value)
}
})
function gerarSenha() {
const letrasMaiusculas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const letrasMinusculas = "abcdefghijklmnopqrstuvwxyz"
const numeros = "0123456789"
const simbolos = "!@#$%&*()_+-=[]{}<>?/"
let caracteresDisponiveis = ""
if (chkMaiusculas.checked) {
caracteresDisponiveis += letrasMaiusculas
}
if (chkMinusculas.checked) {
caracteresDisponiveis += letrasMinusculas
}
if (chkNumeros.checked) {
caracteresDisponiveis += numeros
}
if (chkSimbolos.checked) {
caracteresDisponiveis += simbolos
}
if (caracteresDisponiveis === "") {
alert("Selecione pelo menos uma opção para gerar a senha.")
inputSenha.value = ""
atualizarForcaSenha("")
return
}
const tamanho = Number(inputTamanho.value)
let senhaGerada = ""
for (let i = 0; i < tamanho; i++) {
const indiceAleatorio = Math.floor(Math.random() * caracteresDisponiveis.length)
senhaGerada += caracteresDisponiveis[indiceAleatorio]
}
inputSenha.value = senhaGerada
atualizarForcaSenha(senhaGerada)
}
function copiarSenha() {
if (inputSenha.value === "") {
alert("Gere uma senha antes de copiar.")
return
}
navigator.clipboard.writeText(inputSenha.value)
.then(() => {
alert("Senha copiada com sucesso!")
})
.catch(() => {
alert("Não foi possível copiar a senha.")
})
}
function atualizarForcaSenha(senha) {
if (!senha) {
textoForca.textContent = "-"
nivelForca.style.width = "0%"
nivelForca.style.background = "transparent"
return
}
let pontuacao = 0
if (senha.length >= 8) pontuacao++
if (senha.length >= 12) pontuacao++
if (/[A-Z]/.test(senha)) pontuacao++
if (/[a-z]/.test(senha)) pontuacao++
if (/[0-9]/.test(senha)) pontuacao++
if (/[^A-Za-z0-9]/.test(senha)) pontuacao++
if (pontuacao <= 2) {
textoForca.textContent = "Fraca"
nivelForca.style.width = "33%"
nivelForca.style.background = "#ef4444"
} else if (pontuacao <= 4) {
textoForca.textContent = "Média"
nivelForca.style.width = "66%"
nivelForca.style.background = "#f59e0b"
} else {
textoForca.textContent = "Forte"
nivelForca.style.width = "100%"
nivelForca.style.background = "#22c55e"
}
}
gerarSenha()