-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
76 lines (65 loc) · 2.42 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const lengthSlider = document.querySelector(".pass-length input"),
options = document.querySelectorAll(".option input"),
copyIcon = document.querySelector(".input-box span"),
passwordInput = document.querySelector(".input-box input"),
passIndicator = document.querySelector(".pass-indicator"),
generateBtn = document.querySelector(".generate-btn");
const characters = { // all options for generate password
lowercase: "abcdefghijklmnopqrstuvwxyz",
uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
numbers: "0123456789",
symbols: "~!@#$%^&*()_+-=?[]{}\|;':<>/,."
}
const generatePassword = () => {
let staticPassword = "",
randomPassword = "",
excludeDuplicate = false,
passLength = lengthSlider.value;
options.forEach(option => {
if(option.checked) {
if(option.id !== "exc-duplicate" && option.id !== "spaces") {
staticPassword += characters[option.id];
} else if(option.id == "spaces") {
staticPassword += ` ${staticPassword} `;
} else {
excludeDuplicate = true;
}
}
});
for (let i = 0; i < passLength; i++) {
let randomChar = staticPassword[Math.floor(Math.random() * staticPassword.length)];
if(excludeDuplicate) {
!randomPassword.includes(randomChar) || randomChar == " " ? randomPassword += randomChar : i--;
} else {
randomPassword += randomChar;
}
}
passwordInput.value = randomPassword // passing randomPassword to passwordInput value
}
const upadatePassIndicator = () => {
if(lengthSlider.value <= 8) {
passIndicator.id = "weak";
} else if (lengthSlider.value <= 16) {
passIndicator.id = "medium";
} else {
passIndicator.id = "strong";
}
}
const updateSlider = () => {
// passing slider value as counter text
document.querySelector(".pass-length span").innerText = lengthSlider.value;
generatePassword();
upadatePassIndicator();
}
updateSlider();
const copyPassword = () => {
navigator.clipboard.writeText(passwordInput.value),
copyIcon.innerText = "check";
setTimeout(() => {
copyIcon.innerText = "copy_all";
}, 600);
}
copyIcon.addEventListener("click", copyPassword);
lengthSlider.addEventListener("input", updateSlider);
generateBtn.addEventListener("click", generatePassword);
// @bycapwan