-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
60 lines (53 loc) · 1.73 KB
/
script.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
50
51
52
53
54
55
56
57
58
59
60
// script.js
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();