-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (108 loc) · 2.67 KB
/
index.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
// small : 97-122
// caps: 65-90
//symbols: 32-47
"use strict";
let elements = document.getElementsByClassName("pwd-option");
let keys = Object.keys(elements);
let pwd_length = document.getElementById("pwd-length");
let slider = document.getElementById("slider");
let pwd_field = document.getElementById("pwd-field");
document.getElementById("copy-btn").addEventListener("click", () => {
copyToClipboard();
});
document.getElementById("new-pwd-btn").addEventListener("click", () => {
generatePassword();
});
pwd_length.addEventListener("keydown", event => {
// can also use event.keyCode or event.key or event.code
if (event.which === 13) {
slider.value = event.target.value;
generatePassword();
}
});
slider.addEventListener("input", event => {
event.preventDefault();
pwd_length.value = event.target.value;
generatePassword();
});
function addEventListeners(elements) {
for (let element of elements) {
element.addEventListener("click", () => {
setCheckboxStatus(event);
generatePassword();
});
}
}
function setCheckboxStatus(event) {
let status = uncheckingAllowed();
if (!status) {
event.target.checked = true;
}
}
function uncheckingAllowed() {
let optionValues = [];
console.log(Object.values(elements));
for (let element of elements) {
optionValues.push(element.checked);
}
let count = 0;
for (let value of optionValues) {
if (value) {
count++;
}
}
if (count < 1) {
return false;
}
return true;
}
function getPwdOptions(elements) {
let options = {};
for (let element of elements) {
options[element.name] = element.checked;
}
return options;
}
const copyToClipboard = () => {
let password = pwd_field.value;
navigator.clipboard.writeText(password);
};
function createPassword(length, options) {
let obj = {};
let password = "";
if (options.lowercase) {
obj.SMALL = [97, 122];
}
if (options.uppercase) {
obj.CAPS = [65, 90];
}
if (options.number) {
obj.NUM = [0, 9];
}
if (options.symbol) {
obj.SYM = [33, 47];
}
let keys = Object.keys(obj);
if (keys.length < 1) {
return "";
}
for (let i = 0; i < length; i++) {
let random = Math.floor(Math.random() * keys.length);
let charToEncode = Math.floor(Math.random() * (obj[keys[random]][1] - obj[keys[random]][0]) + obj[keys[random]][0]);
if (keys[random] == "NUM") {
password += charToEncode;
} else {
password += String.fromCharCode(charToEncode);
}
}
return password;
}
function generatePassword() {
let length = pwd_length.value;
// let elements = document.querySelectorAll(".pwd-option");
let options = getPwdOptions(elements);
let password = createPassword(length, options);
pwd_field.value = password;
}
addEventListeners(elements);
generatePassword();