-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
67 lines (59 loc) · 2.1 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
const lengthSlider = document.querySelector(".pass-length input");
const options = document.querySelectorAll(".option input");
const copyIcon = document.querySelector(".input-box span");
const passwordInput = document.querySelector(".input-box input");
const passIndicator = document.querySelector(".pass-indicator");
const generateBtn = document.querySelector(".generate-btn");
const characters = {
lowercase: "abcdefghijklmnopqrstuvwxyz",
uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
numbers: "0123456789",
symbols: "^!$%&|[](){}:;.,*+-#@<>~"
};
const generatePassword = () => {
let staticPassword = "";
let randomPassword = "";
let excludeDuplicate = false;
let 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;
};
const updatePassIndicator = () => {
passIndicator.id = lengthSlider.value <= 8 ? "weak" : lengthSlider.value <= 16 ? "medium" : "strong";
};
const updateSlider = () => {
document.querySelector(".pass-length span").innerText = lengthSlider.value;
generatePassword();
updatePassIndicator();
};
updateSlider();
const copyPassword = () => {
navigator.clipboard.writeText(passwordInput.value);
copyIcon.innerText = "check";
copyIcon.style.color = "#4285F4";
setTimeout(() => {
copyIcon.innerText = "copy_all";
copyIcon.style.color = "#707070";
}, 1500);
};
copyIcon.addEventListener("click", copyPassword);
lengthSlider.addEventListener("input", updateSlider);
generateBtn.addEventListener("click", generatePassword);