-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
137 lines (119 loc) · 4.22 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
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
const inputSlider = document.querySelector(".slider");
const lengthDisplay = document.querySelector("[data-lengthNumber]");
const passwordDisplay = document.querySelector(".display");
const copyBtn = document.querySelector(".copyBtn");
const copyMsg = document.querySelector("[copyMsg]");
const uppercaseCheck = document.querySelector("#uppercase");
const lowercaseCheck = document.querySelector("#lowercase");
const numbersCheck = document.querySelector("#numbers");
const symbolsCheck = document.querySelector("#symbols");
const indicator = document.querySelector(".indicator");
const generateBtn = document.querySelector(".generateBtn");
const allCheckBox = document.querySelectorAll("input[type=checkbox]");
const allSymbols = '~`!@#$%^&*()_-+={[}]|:;"<,>.?/';
// initial value
let password = "";
let passwordLength = 10;
let checkCount = 1;
handleSlider();
setIndicator("#ccc"); // initial gray color
// set passwordLength
function handleSlider() {
console.log("sad");
inputSlider.value = passwordLength;
lengthDisplay.innerText = passwordLength;
// set color of slider
const min = inputSlider.min;
const max = inputSlider.max;
inputSlider.style.backgroundSize = ((passwordLength - min) / (max - min)) * 100 + "% 100%";
}
function setIndicator(color) {
indicator.style.backgroundColor = color;
indicator.style.boxShadow = `0 0 12px 5px ${color}`;
}
const getRandInt = (min, max) => Math.floor(Math.random() * (max - min)) + min;
const getRandDigit = () => getRandInt(0, 9);
const getRandLowerCase = () => String.fromCharCode(getRandInt(97, 123));
const getRandUpperCase = () => String.fromCharCode(getRandInt(65, 91));
const getRandSymbol = () => allSymbols.charAt(getRandInt(0, allSymbols.length));
function calStrength() {
let hasUpper = uppercaseCheck.checked;
let hasLower = lowercaseCheck.checked;
let hasNum = numbersCheck.checked;
let hasSym = symbolsCheck.checked;
if (hasUpper && hasLower && (hasNum || hasSym) && passwordLength >= 8) {
setIndicator("#0f0");
} else if ((hasLower || hasUpper) && (hasNum || hasSym) && passwordLength >= 6) {
setIndicator("#ff0");
} else {
setIndicator("#f00");
}
}
inputSlider.addEventListener("input", (e) => {
passwordLength = e.target.value;
handleSlider();
});
function handleCheckBoxChange() {
checkCount = 0;
allCheckBox.forEach((checkbox) => {
if (checkbox.checked) checkCount++;
});
// special condition
if (checkCount > passwordLength) {
passwordLength = checkCount;
handleSlider();
}
}
allCheckBox.forEach((checkbox) => {
checkbox.addEventListener("change", handleCheckBoxChange);
});
async function copyContent() {
try {
await navigator.clipboard.writeText(passwordDisplay.value);
copyMsg.innerText = "copied";
} catch {
copyMsg.innerText = "Failed";
}
copyMsg.classList.add("active");
setTimeout(() => {
copyMsg.classList.remove("active");
}, 2000);
}
copyBtn.addEventListener("click", () => {
if (passwordDisplay.value) copyContent();
});
function shufflePassword(pass) {
// using fisher yates method
let arr = Array.from(pass);
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(getRandInt(0, i + 1));
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
let str = "";
arr.forEach((el) => (str += el));
return str;
}
generateBtn.addEventListener("click", () => {
if (checkCount == 0) return;
if (passwordLength < checkCount) {
passwordLength = checkCount;
handleSlider();
}
password = "";
let funArr = [];
if (uppercaseCheck.checked) funArr.push(getRandUpperCase);
if (lowercaseCheck.checked) funArr.push(getRandLowerCase);
if (numbersCheck.checked) funArr.push(getRandDigit);
if (symbolsCheck.checked) funArr.push(getRandSymbol);
// password contains atleast 1 char of every type
for (let i = 0; i < funArr.length; i++) password += funArr[i]();
for (let i = funArr.length; i < passwordLength; i++) {
let j = getRandInt(0, funArr.length);
password += funArr[j]();
}
password = shufflePassword(password);
passwordDisplay.value = password;
calStrength();
});