Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
163 lines (128 loc) · 4.71 KB
/
Copy pathscript.js
File metadata and controls
163 lines (128 loc) · 4.71 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// DOM Elements - all the elements we need from HTML
const passwordInput = document.getElementById("password");
const lengthSlider = document.getElementById("length");
const lengthDisplay = document.getElementById("length-value");
const uppercaseCheckbox = document.getElementById("uppercase");
const lowercaseCheckbox = document.getElementById("lowercase");
const numbersCheckbox = document.getElementById("numbers");
const symbolsCheckbox = document.getElementById("symbols");
const generateButton = document.getElementById("generate-btn");
const copyButton = document.getElementById("copy-btn");
const strengthBar = document.querySelector(".strength-bar");
const strengthText = document.querySelector(".strength-container p");
const strengthLabel = document.getElementById("strength-label");
// Character sets
const uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
const numberCharacters = "0123456789";
const symbolCharacters = "!@#$%^&*()-_=+[]{}|;:,.<>?/";
// user should first select the options the way they want their password to be from length uppercase lowercase
// numbers or symbols should be included or not
// and when they press the button to generate password
// the function should consider all the options and then generate a password form teh character sets matching the conditions
// user have given then the user should be able to see and copy the password from copyButton
//1) EVENT FLOW
lengthSlider.addEventListener("input", () => {
lengthDisplay.textContent = lengthSlider.value;
});
generateButton.addEventListener("click", generatePassword);
window.addEventListener("DOMContentLoaded", generatePassword);
function generatePassword() {
// 1. Get the selected password length
const passwordLength = Number(lengthSlider.value);
console.log(passwordLength);
// 2. check which character options are selected
// 3. build one string containing all allowed characters
let allowedCharacters = getAllowedCharacters();
if (allowedCharacters.length === 0) {
alert("Please select at least one option");
return;
}
// 4. Generate random characters untill password reaches selected length
const password = createRandomPassword(passwordLength, allowedCharacters);
// 5. Display the generated password
passwordInput.value = password;
// 6. Update the password strength
updateStrengthMeter(password);
}
function createRandomPassword(length, characters) {
let password = "";
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
password += characters[randomIndex];
}
return password;
}
function getAllowedCharacters() {
// then check each checkbox
let characters = "";
if (uppercaseCheckbox.checked) {
characters += uppercaseLetters;
}
if (lowercaseCheckbox.checked) {
characters += lowercaseLetters;
}
if (numbersCheckbox.checked) {
characters += numberCharacters;
}
if (symbolsCheckbox.checked) {
characters += symbolCharacters;
}
return characters;
}
function updateStrengthMeter(password) {
const passwordLength = password.length;
const hasUppercase = /[A-Z]/.test(password);
const hasLowercase = /[a-z]/.test(password);
const hasNumbers = /[0-9]/.test(password);
const hasSymbols = /[!@#$%^&*()\-_=+\[\]{}|;:,.<>?/]/.test(password);
let strengthScore = 0;
strengthScore += Math.min(passwordLength * 2, 40);
if (hasUppercase) {
strengthScore += 15;
}
if (hasLowercase) {
strengthScore += 15;
}
if (hasNumbers) {
strengthScore += 15;
}
if (hasSymbols) {
strengthScore += 15;
}
if (passwordLength < 8) {
strengthScore = Math.min(strengthScore, 40);
}
const safeScore = Math.max(5, Math.min(100, strengthScore));
strengthBar.style.width = safeScore + "%";
console.log(`Strength score ${strengthScore}`);
if (strengthScore < 40) {
strengthLabel.textContent = "Weak";
strengthBar.style.backgroundColor = "var(--weak-color)";
} else if (strengthScore <= 70) {
strengthLabel.textContent = "Medium";
strengthBar.style.backgroundColor = "var(--medium-color)";
} else {
strengthLabel.textContent = "Strong";
strengthBar.style.backgroundColor = "var(--strong-color)";
}
}
copyButton.addEventListener("click", () => {
if (!passwordInput.value) return;
navigator.clipboard
.writeText(passwordInput.value)
.then(() => {
showCopySuccess();
})
.catch((error) => {
console.log("Could not copy:", error);
});
});
function showCopySuccess() {
copyButton.classList.remove("far", "fa-copy");
copyButton.classList.add("fas", "fa-check");
setTimeout(() => {
copyButton.classList.remove("fas", "fa-check");
copyButton.classList.add("far", "fa-copy");
}, 1500);
}