-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
179 lines (167 loc) · 4.37 KB
/
app.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
168
169
170
171
172
173
174
175
176
177
178
179
console.log("hello");
const length = document.querySelector("#length");
const lengthInput = document.querySelector("#num");
let passwordLength;
// loading dom content on refresh
document.addEventListener("DOMContentLoaded", (event) => {
passwordLength = length.value;
lengthInput.value = passwordLength;
generatePassword();
});
//to change length input value by range slider
length.addEventListener("input", (event) => {
let val = event.target.value;
lengthInput.value = val;
passwordLength = lengthInput.value;
generatePassword();
});
let isUpper = false;
let isNumber = false;
let isSymbol = false;
// accessing uppercase letters
let uppercase = document.querySelector("#uppercase");
uppercase.addEventListener("change", () => {
if (uppercase.checked) {
isUpper = true;
} else {
isUpper = false;
}
pushUpper();
});
// accessing symbols
let symbol = document.querySelector("#special-char");
symbol.addEventListener("change", (e) => {
if (symbol.checked) {
isSymbol = true;
} else {
isSymbol = false;
}
pushSymbols();
});
// acessing numbers
let number = document.querySelector("#number");
number.addEventListener("change", () => {
if (number.checked) {
isNumber = true;
} else {
isNumber = false;
}
pushNumbers();
});
const upper = [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const symbols = ["!", "@", "#", "%", "&", "*", "/", "_"];
const lowerCase = [..."abcdefghijklmnopqrstuvwxyz"];
let letters = [];
lowerCase.forEach((element) => {
letters.push(element);
});
//adding and deletin uppercase value on event listener
const pushUpper = () => {
if (isUpper) {
upper.forEach((el) => {
letters.push(el);
});
} else {
let remVal;
upper.forEach((element) => {
remVal = element;
if (letters.includes(remVal)) {
letters.splice(letters.indexOf(remVal), 1);
}
});
}
generatePassword();
};
//adding and deletin symbols value on event listener
const pushSymbols = () => {
if (isSymbol) {
symbols.forEach((el) => {
letters.push(el);
});
} else {
let remVal;
symbols.forEach((element) => {
remVal = element;
if (letters.includes(remVal)) {
letters.splice(letters.indexOf(remVal), 1);
}
});
}
generatePassword();
};
//adding and deletin uppercase value on event listener
const pushNumbers = () => {
if (isNumber) {
numbers.forEach((el) => {
letters.push(el);
});
} else {
let remVal;
numbers.forEach((element) => {
remVal = element;
if (letters.includes(remVal)) {
letters.splice(letters.indexOf(remVal), 1);
}
});
}
generatePassword();
};
const password = document.querySelector("#pass");
const generatePassword = () => {
let rawPassword = [];
for (i = 0; i < passwordLength; i++) {
let random = Math.floor(Math.random() * letters.length);
rawPassword.push(letters[random]);
}
let finallPassword = "";
rawPassword.forEach((el) => {
finallPassword += el;
});
password.value = finallPassword;
};
//copying password
function copyText() {
// Get the text field
let copyText = document.getElementById("pass");
// Copy the text inside the text field
navigator.clipboard.writeText(copyText.value);
}
const copySpan = document.querySelector(".copy-text");
copySpan.addEventListener("click", () => {
copyText();
pop();
});
const btn = document.querySelector(".copy");
btn.addEventListener("click", () => {
copyText();
pop();
});
const refresh = document.querySelector(".refresh");
refresh.addEventListener("click", generatePassword);
const wrapper = document.querySelector(".wrapper");
function pop() {
let div = document.createElement("div");
div.classList.add("pop");
wrapper.appendChild(div);
div.innerText = "Copied !";
setTimeout(() => {
div.remove();
}, 3050);
}
const passDiv = document.querySelector("#password");
password.addEventListener("input", () => {
let length = password.value.length;
if (length == 0) {
passDiv.style.setProperty("--aaa", "0%");
} else if (length <= 3) {
passDiv.style.setProperty("--after", "red");
passDiv.style.setProperty("--aaa", "33%");
} else if (length <= 5) {
passDiv.style.setProperty("--after", "orangered");
passDiv.style.setProperty("--aaa", "67%");
} else {
passDiv.style.setProperty("--after", "green");
passDiv.style.setProperty("--aaa", "100%");
}
});