-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
218 lines (170 loc) · 5.7 KB
/
script.js
File metadata and controls
218 lines (170 loc) · 5.7 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Handle Slider Control and Display Password Length
let lengthDisplay = document.querySelector('[lengthDisplay');
// console.log(lengthDisplay)
let slider = document.querySelector('input[type=range]');
// console.log(slider)
function handleSlider() {
slider.value = passwordLength;
lengthDisplay.innerText = passwordLength;
}
let passwordLength = 10;
handleSlider();
slider.addEventListener('input', (event) => {
passwordLength = event.target.value;
handleSlider();
});
// --------------------------------------
// Generate Random Letters and Number and Symbols
const symbol = '~`!@#$%^&*()_-+={[}]|:;"<,>.?/';
function generateRandom(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// Random Lowercase Letter
function generateRandomLowercase() {
return String.fromCharCode(generateRandom(97, 123));
}
// Random Lowercase Letter
function generateRandomUppercase() {
return String.fromCharCode(generateRandom(65, 91));
}
// Random Number
function generateRandomNumber() {
return generateRandom(1, 10);
}
// Generate Symbol
function generateRandomSymbol() {
let index = generateRandom(0, symbol.length);
return symbol[index];
}
// console.log(generateRandomLowercase());
// console.log(generateRandomUppercase());
// console.log(generateRandomNumber());
// console.log(generateRandomSymbol());
// --------------------------------------
// Strength Color Based on Password
let indicator = document.querySelector('.indicator');
// Set Indicator
function setIndicator(color) {
indicator.style.backgroundColor = color;
indicator.style.boxShadow = `0 0 12px 1px ${color}`;
}
// Default Indicator
setIndicator("#ccc");
const uppercase = document.querySelector('#uppercase');
const lowercase = document.querySelector('#lowercase');
const numbers = document.querySelector('#numbers');
const symbols = document.querySelector('#symbols');
function calcStrength() {
let hasUpper = false;
let hasLower = false;
let hasNumber = false;
let hasSymbol = false;
if (uppercase.checked) hasUpper = true;
if (lowercase.checked) hasLower = true;
if (numbers.checked) hasNumber = true;
if (symbols.checked) hasSymbol = true;
if (hasUpper && hasLower && (hasNumber || hasSymbol) && passwordLength >= 8) {
setIndicator("#0f0");
} else if (
(hasLower || hasUpper) &&
(hasNumber || hasSymbol) &&
passwordLength >= 6
) {
setIndicator("#ff0");
} else {
setIndicator("#f00");
}
}
// -----------------------------------
// Copy Message
let copyMessage = document.querySelector("[copyMessage]");
let copyBtn = document.querySelector(".copyBtn");
let passwordDisplay = document.querySelector("input[passwordDisplay]");
// passwordDisplay.value = "My Name is Priyansh";
// Why we use it - https://stackoverflow.com/questions/45071353/copy-text-string-on-click#:~:text=15-,Use%20the%20Clipboard,-API!
async function copyContent() {
try {
await navigator.clipboard.writeText(passwordDisplay.value);
copyMessage.innerText = "Copied"
}
catch (e) {
// alert("Something went wrong in CopyContent");
copyMessage.innerText = "Failed";
}
copyMessage.classList.add('active');
setTimeout(() => {
copyMessage.classList.remove('active');
}, 1000)
}
copyBtn.addEventListener("click", () => {
if (passwordDisplay.value)
copyContent();
});
// ------------------------------------
// shuffle algorithm is the Fisher-Yates (aka Knuth) Shuffle.
// Shuffle the array randomly - Fisher Yates Method
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
let str = "";
array.forEach((el) => (str += el));
return str;
}
// Password Generate
// By Default UpperCase Checked
// uppercase.checked = true;
let checkBoxes = document.querySelectorAll("input[type=checkbox]");
// console.log(checkBoxes);
let checkCount = 0;
// CheckBox - Handle
function handleCheckBoxChange() {
checkCount = 0;
checkBoxes.forEach((checkbox) => {
if (checkbox.checked)
checkCount++;
});
//special condition
if (passwordLength < checkCount) {
passwordLength = checkCount;
handleSlider();
}
}
checkBoxes.forEach((checkbox) => {
checkbox.addEventListener('change', handleCheckBoxChange);
})
let password = "";
let generateBtn = document.querySelector("#generateBtn");
generateBtn.addEventListener('click', () => {
if (checkCount <= 0)
return;
if (passwordLength < checkCount) {
passwordLength = checkCount;
handleSlider();
}
// Remove Previous Password
password = "";
let arrayOfCheckedFunction = [];
if (uppercase.checked) arrayOfCheckedFunction.push(generateRandomUppercase);
if (lowercase.checked) arrayOfCheckedFunction.push(generateRandomLowercase);
if (numbers.checked) arrayOfCheckedFunction.push(generateRandomNumber);
if (symbols.checked) arrayOfCheckedFunction.push(generateRandomSymbol);
// Compulsory Addition
for (let i = 0; i < arrayOfCheckedFunction.length; i++) {
password += arrayOfCheckedFunction[i]();
}
// console.log("Password: " + password);
// Additional addition
for (let i = 0; i < passwordLength - arrayOfCheckedFunction.length; i++) {
let randIndex = generateRandom(0, arrayOfCheckedFunction.length);
password += arrayOfCheckedFunction[randIndex]();
}
// console.log("Password: " + password);
// Shuffle Password
password = shuffle(Array.from(password));
passwordDisplay.value = password;
calcStrength();
});