-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
100 lines (82 loc) · 2.85 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const passwordButton = document.querySelector("#password-button");
const passwordDisplay = document.querySelector("#password-display");
const lengthSelector = document.querySelector("#length-selector");
const symbolSelector = document.querySelector("#use_symbol");
const numberSelector = document.querySelector("#use_num");
lengthSelector.addEventListener("input", (e) => {
e.target.value = clampValues(
e.target.value,
+lengthSelector.min,
+lengthSelector.max
);
});
[numberSelector, symbolSelector, passwordButton].forEach((selector) => {
selector.addEventListener("click", generatePassword);
});
lengthSelector.addEventListener("change", generatePassword);
passwordDisplay.addEventListener("click", copyPassword);
function clampValues(val, min, max) {
if (val < min) {
val = min;
}
if (val > max) {
val = max;
}
return val;
}
function displayPassword(password) {
passwordDisplay.innerText = password;
}
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const digits = "0123456789";
const symbols = "!@#$%^&";
function generatePassword() {
const pswdLength = lengthSelector.value;
let remaining = pswdLength;
let letterCount = getRandomInRange(
Math.ceil(remaining / 4),
Math.ceil(remaining / 3)
);
remaining -= letterCount;
let digitCount = numberSelector.checked
? getRandomInRange(Math.round(remaining / 3), remaining - 2)
: 0;
remaining -= digitCount;
let symbolCount = symbolSelector.checked
? getRandomInRange(Math.ceil(remaining / 3), Math.ceil(remaining / 2))
: 0;
remaining -= symbolCount;
// console.log("letterCount:", letterCount, "remaining:", remaining);
let additionalDigits = numberSelector.checked
? Math.floor(remaining / 2)
: 0;
digitCount += additionalDigits;
remaining -= additionalDigits;
letterCount += remaining;
// console.log("type counts (l,d,s):", letterCount, digitCount, symbolCount);
let password = "";
for (let i = 0; i < letterCount; i++) {
password += letters[Math.floor(Math.random() * letters.length)];
}
for (let i = 0; i < symbolCount; i++) {
password += symbols[Math.floor(Math.random() * symbols.length)];
}
for (let i = 0; i < digitCount; i++) {
password += digits[Math.floor(Math.random() * digits.length)];
}
password = password
.split("")
.sort((a, b) => 0.5 - Math.random())
.sort((a, b) => 0.5 - Math.random())
.join("");
displayPassword(password);
}
function getRandomInRange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function copyPassword() {
let password = passwordDisplay.innerText;
navigator.clipboard.writeText(password);
displayPassword("copied...");
setTimeout(() => {displayPassword(password)}, 250);
}