-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (57 loc) · 2.59 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
const lengthSlider = document.querySelector(".password__length input");
const options = document.querySelectorAll(".password__option input");
const copyIcon = document.querySelector(".password__inputbox i");
const passwordInput = document.querySelector(".password__inputbox input");
const passIndicator = document.querySelector(".password__indicator");
const generateBtn = document.querySelector(".password__button");
const characters = {
lowercase: "abcdefghijklmnopqrstuvwxyz",
uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
numbers: "0123456789",
symbols: "!$%&|[](){}:;.,*+-#@<>~"
};
const generatePassword = () => {
let randomPassword = "";
const passLength = parseInt(lengthSlider.value); // gets length from the slider
// Concatenating selected character sets into a single string
let selectedChars = "";
options.forEach(option => {
if (option.checked) {
selectedChars += characters[option.id];
}
});
// Generate random password
for (let i = 0; i < passLength; i++) {
const randomIndex = Math.floor(Math.random() * selectedChars.length);
randomPassword += selectedChars[randomIndex];
}
// Update the password input field with the generated password
passwordInput.value = randomPassword;
// Update password strength indicator
updatePassIndicator();
};
// Password Indicator function
const updatePassIndicator = () => {
passIndicator.id = lengthSlider.value <= 8 ? "weak" :
lengthSlider.value <= 16 ? "medium" : "strong";
};
// Password Slider
const updateSlider = () => {
document.querySelector(".password__length span").innerHTML = lengthSlider.value;
// generatePassword();
};
// Copy password
const copyPassword = () => {
navigator.clipboard.writeText(passwordInput.value);
copyIcon.style.color = "#b9e0f2";
setTimeout(() => {
copyIcon.style.color = "#fff";
}, 500);
};
copyIcon.addEventListener("click", copyPassword);
lengthSlider.addEventListener("input", updateSlider);
generateBtn.addEventListener("click", generatePassword);
// Initial generation of password and update of password strength indicator
updateSlider();
// Disclaimer
alert("Disclaimer:\n\n\tThis password generator is make for my learning purposes only.\n\tWhile it can generate strong passwords,\n\tit's important to remember that no password can guarantee complete security.\n\tPlease use generated passwords responsibly and consider additional security measures for sensitive accounts.🔒💡\n\n\t\t\t\tMade with ❤️ by @Rocky");