-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
101 lines (76 loc) · 2.12 KB
/
main.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
const pwE1 = document.getElementById("pw");
const copyE1 = document.getElementById("copy");
const lenE1 = document.getElementById("len");
const upperE1 = document.getElementById("upper");
const lowerE1 = document.getElementById("lower");
const numberE1 = document.getElementById("number");
const symbolE1 = document.getElementById("symbol");
const generateE1 = document.getElementById("generate");
const upperLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lowerLetters = "abcdefghijklmnopqrstuvwxyz";
const numbers = "0123456789";
const symbols = "!@#$%^&*()_+=";
function getLowercase() {
return lowerLetters[Math.floor(Math.random() * lowerLetters.length)];
}
function getUppercase() {
return upperLetters[Math.floor(Math.random() * upperLetters.length)];
}
function getNumber() {
return numbers[Math.floor(Math.random() * numbers.length)];
}
function getSymbol() {
return symbols[Math.floor(Math.random() * symbols.length)];
}
function generatePassword() {
const len = lenE1.value;
let password = "";
if(upperE1.checked) {
password += getUppercase();
}
if(lowerE1.checked) {
password += getLowercase();
}
if(numberE1.checked) {
password += getNumber();
}
if(symbolE1.checked) {
password += getSymbol();
}
for(let i = password.length; i < len; i++) {
const x = generateX();
password += x;
}
pwE1.innerText = password;
}
function generateX() {
const xs = [];
if (upperE1.checked) {
xs.push(getUppercase());
}
if (lowerE1.checked) {
xs.push(getLowercase());
}
if(numberE1.checked) {
xs.push(getNumber());
}
if (symbolE1.checked) {
xs.push(getSymbol());
}
if (xs.length === 0) return"";
return xs[Math.floor(Math.random() * xs.length)];
}
generateE1.addEventListener("click", generatePassword);
copyE1.addEventListener("click", () => {
const textarea = document.createElement("textarea");
const password = pwE1.innerText;
if (!password) {
return;
}
textarea.value = password;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
alert("Password copied to clipboard");
});