-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
100 lines (81 loc) · 2.84 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 result = document.getElementById("result");
const uppercase = document.getElementById("uppercase");
const lowercase = document.getElementById("lowercase");
const numbers = document.getElementById("numbers");
const symbols = document.getElementById("symbols");
const passLength = document.getElementById("length");
const genBtn = document.getElementById("generate");
const copyBtn = document.getElementById("clipboard")
// types
const randomFuction = {
upper: getRandomUp,
lower: getRandomLow,
number: getRandomNo,
symbol: getRandomSymbol
};
//clipboard functonality
copyBtn.addEventListener("click", () => {
const textarea = document.createElement("textarea")
const passW = result.innerText
if(!passW) {
return "Select at least one character type!";
}
textarea.value = passW
document.body.appendChild(textarea)
textarea.select()
document.execCommand("copy")
textarea.remove()
alert('Password has been copied to clipboard');
})
// Event Listener
genBtn.addEventListener("click", () => {
const length = parseInt(passLength.value, 10);
const hasUppercase = uppercase.checked;
const hasLowercase = lowercase.checked;
const hasNumbers = numbers.checked;
const hasSymbols = symbols.checked;
if (!length || length <= 0) {
result.innerText = "Enter a valid password length!";
return;
}
result.innerText = genPassword(hasUppercase, hasLowercase, hasNumbers, hasSymbols, length);
});
// Password Generator Function
function genPassword(upper, lower, number, symbol, length) {
let generatedPass = "";
const typesArray = [{ upper }, { lower }, { number }, { symbol }].filter(
item => Object.values(item)[0]
);
if (typesArray.length === 0) {
return "Select at least one character type!";
}
// Generate characters for the password
for (let i = 0; i < length; i++) {
const randomType = typesArray[Math.floor(Math.random() * typesArray.length)];
const funcName = Object.keys(randomType)[0];
generatedPass += randomFuction[funcName]();
}
return generatedPass;
}
// Utility function to shuffle an array
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
}
return array;
}
// Random Character Functions
function getRandomLow() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 97);
}
function getRandomUp() {
return String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}
function getRandomNo() {
return String.fromCharCode(Math.floor(Math.random() * 10) + 48);
}
function getRandomSymbol() {
const symbolsChar = "!@#$%&*()-_+={}[]|:;<>,.?/~";
return symbolsChar[Math.floor(Math.random() * symbolsChar.length)];
}