/
generator.js
50 lines (42 loc) · 1.49 KB
/
generator.js
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
function generate() {
let dictionary = '';
if (document.getElementById('lowercaseCb').checked) {
dictionary += 'qwertyuiopasdfghjklzxcvbnm';
}
if (document.getElementById('uppercaseCb').checked) {
dictionary += 'QWERTYUIOPASDFGHJKLZXCVBNM';
}
if (document.getElementById('digitsCb').checked) {
dictionary += '1234567890';
}
if (document.getElementById('specialsCb').checked) {
dictionary += '!@#$%^&*()_+-={}[];<>:';
}
const length = document.querySelector('input[type="range"]').value;
if (length < 1 || dictionary.length === 0) {
return;
}
let password = '';
for (let i = 0; i < length; i++) {
const pos = Math.floor(Math.random() * dictionary.length);
password += dictionary[pos];
}
document.querySelector('input[type="text"]').value = password;
}
[...document.querySelectorAll('input[type="checkbox"], button.generate')].forEach(elem => {
elem.addEventListener('click', generate);
});
document.querySelector('input[type="range"]').addEventListener('input', e => {
document.querySelector('div.range span').innerHTML = e.target.value;
generate();
});
document.querySelector('div.password button').addEventListener('click', () => {
const pass = document.querySelector('input[type="text"]').value;
navigator.clipboard.writeText(pass).then(() => {
document.querySelector('div.password button').innerHTML = 'copied!';
setTimeout(() => {
document.querySelector('div.password button').innerHTML = 'copy';
}, 1000);
})
});
generate();