-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
167 lines (144 loc) · 5.02 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
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
//DOM Elements
const resultEl = document.getElementById("passResult");
const lengthEl = document.getElementById("result");
const upperCase = document.getElementById("upperCase");
const lowerCase = document.getElementById("lowerCase");
const includeNumber = document.getElementById("incNumbers");
const includeSymbol = document.getElementById("incSymbols");
const generateBtn = document.getElementById("generateBtn");
const rangeValue = document.getElementById("rangeValue");
const clipboardEl = document.getElementById("clipboard");
const myRange = document.getElementById("myRange");
const randomFunc = {
lower: getRandomLower,
upper: getRandomUpper,
number: getRandomNumber,
symbol: getRandomSymbol,
};
function getRandomCharacter(types) {
const randomIndex = Math.floor(Math.random() * types.length);
const funcName = types[randomIndex];
return randomFunc[funcName]();
}
// Random Values
function getRandomLower() {return String.fromCharCode(Math.floor(Math.random() * 26) + 97);}
function getRandomUpper() {return String.fromCharCode(Math.floor(Math.random() * 26) + 65);}
function getRandomNumber() {return String.fromCharCode(Math.floor(Math.random() * 10) + 48);}
function getRandomSymbol() {
const symbols = "!@#$%^&*(){}[]=<>/,.";
return symbols[Math.floor(Math.random() * symbols.length)];
}
//Generate event listen
generateBtn.addEventListener("click", () => {
clipboardEl.style.display = "block"
const length = +myRange.value;
const hasLower = lowerCase.checked;
const hasUpper = upperCase.checked;
const hasNumber = includeNumber.checked;
const hasSymbol = includeSymbol.checked;
resultEl.innerText = generatePassword(hasLower, hasUpper, hasNumber, hasSymbol, length);
});
//Copy Password to clipboard
clipboardEl.addEventListener("click", () => {
const textarea = document.createElement("textarea");
const password = resultEl.innerText;
if (!password) {
return;
}
textarea.value = password;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
notfy()
});
//Generate password function
function generatePassword(lower, upper, number, symbol, length) {
let generatedPassword = "";
const typesArr = [];
if (lower) typesArr.push('lower');
if (upper) typesArr.push('upper');
if (number) typesArr.push('number');
if (symbol) typesArr.push('symbol');
if (typesArr.length === 0) {
clipboardEl.style.display = "none";
resultEl.innerText = "Verify a condition!";
resultEl.style.color = "red";
setTimeout(() => {
resultEl.innerText = "P4$5W0rD!";
resultEl.style.color = "orange";
}, 1000);
return resultEl.textContent;
}
for (let i = 0; i < length; i++) {
generatedPassword += getRandomCharacter(typesArr);
}
const finalPass = generatedPassword.slice(0, length);
return finalPass ? finalPass : "Please choose password length.";
}
function updateRange() {
var range = myRange;
var value = range.value;
rangeValue.innerText = value;
const liFirst = document.getElementById("liFirst");
const liSecond = document.getElementById("liSecond");
const liThird = document.getElementById("liThird");
const liFourth = document.getElementById("liFourth");
const difText = document.getElementById("difText");
if (value <= 5) {
liFirst.style.backgroundColor = "lightgreen";
liSecond.style.backgroundColor = "";
liThird.style.backgroundColor = "";
liFourth.style.backgroundColor = "";
difText.textContent = "Too Weak!";
difText.style.color = "lightgreen";
} else if (5 < value && value <= 10) {
liFirst.style.backgroundColor = "lightgreen";
liSecond.style.backgroundColor = "green";
liThird.style.backgroundColor = "";
liFourth.style.backgroundColor = "";
difText.textContent = "EASY";
difText.style.color = "green";
} else if (10 < value && value <= 15) {
liFirst.style.backgroundColor = "lightgreen";
liSecond.style.backgroundColor = "green";
liThird.style.backgroundColor = "tomato";
liFourth.style.backgroundColor = "";
difText.textContent = "MEDIUM";
difText.style.color = "tomato";
} else if (15 < value && value <= 20) {
liFirst.style.backgroundColor = "lightgreen";
liSecond.style.backgroundColor = "green";
liThird.style.backgroundColor = "tomato";
liFourth.style.backgroundColor = "red";
difText.textContent = "HARD!";
difText.style.color = "red";
}
}
myRange.addEventListener("input", ()=>{
updateRange();
generateBtn.click()
// vibrate()
});
window.addEventListener("load", () => {
updateRange();
});
//notfy alert
function notfy() {
const notyf = new Notyf();
notyf.success({
message: "Copied!",
duration: 1000,
position: {
x: "center",
y: "top",
},
});
}
// function vibrate() {
// if (!("vibrate" in navigator)) {
// alert("Vibrate not supported!");
// return;
// }
// navigator.vibrate(10);
// }