-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
212 lines (179 loc) · 6.64 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Region Variable assignment
var generateBtn = document.querySelector("#generate");
var body = document.querySelector("body");
var pwdUserInput = [];
var upperCaseChars = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ'];
var lowerCaseChars = ['abcdefghijklmnopqrstuvwxyz'];
var numbers = ['0123456789'];
var specialChars = ['\!\#\$\%\&\(\)\*\+\-\.\:\;\=\?\@\[\]\^\_\{\|\}'];
var pwdSliderRange = 64;
var result = "";
var buttonStyle = document.getElementById("generate").style.cssText;
// EndRegion
// Region Event listener
generateBtn.addEventListener("click", writePassword);
//Region HTML element creation
var card = document.querySelector(".card-body");
var cardDiv = document.querySelector(".card");
var cardFooter = document.querySelector(".card-footer");
var textArea = document.querySelector("#password");
// Create the elements to be displayed in the welcome dialog
var welcomeDialogDiv = document.createElement("div");
var welcomeDialogHeading = document.createElement("h5");
var welcomeDialogBody = document.createElement("p");
// p tag for label of slider
var pTag = document.createElement("p");
// New footer buttons
var backButton = document.createElement("button");
backButton.setAttribute("id", "backBotton");
var copyButton = document.createElement("button");
copyButton.setAttribute("id", "copyButton");
// End region
loadPasswordInputArea();
// Write password to the #password input
function writePassword() {
generatePassword();
if (result != "") {
console.log("Not empty");
clearInputPreSelections();
card.replaceChild(textArea, welcomeDialogDiv);
footerDisplayLogic();
cardFooter.replaceChild(backButton, generateBtn);
cardFooter.appendChild(copyButton);
textArea.value = result;
}
}
// Region to Create a div with a back and a copy button
function footerDisplayLogic() {
backButton.setAttribute("class", "footerbtn");
backButton.value = "Back";
backButton.textContent = "Back";
backButton.style.cssText = buttonStyle;
backButton.addEventListener('click', function () {
pwdUserInput = [];
result = "";
card.replaceChild(welcomeDialogDiv, textArea);
cardFooter.replaceChild(generateBtn, backButton);
cardFooter.removeChild(copyButton);
})
copyButton.setAttribute("name", "Copy Password");
copyButton.setAttribute("class", "footerbtn");
copyButton.value = "Copy Password";
copyButton.textContent = "Copy";
copyButton.style.cssText = buttonStyle;
copyButton.addEventListener('click', function () {
textArea.select();
document.execCommand("copy");
})
}
// End region
function loadPasswordInputArea() {
displayPasswordInputChoices();
}
// Region creates and displays pwd choice section
function displayPasswordInputChoices() {
// Add classes to style dialog in bootstrap
welcomeDialogDiv.setAttribute("class", "alert");
welcomeDialogDiv.setAttribute("role", "alert");
welcomeDialogHeading.setAttribute("class", "alert-heading");
// Add content to the dialog heading and body
welcomeDialogHeading.textContent = "Please choose from options below and click \"Generate Password\"!";
welcomeDialogDiv.appendChild(welcomeDialogHeading);
welcomeDialogDiv.appendChild(welcomeDialogBody);
createAndDisplayCheckboxItem("UppercaseCB", "Uppercase", welcomeDialogDiv);
createAndDisplayCheckboxItem("LowercaseCB", "Lowercase", welcomeDialogDiv);
createAndDisplayCheckboxItem("NumbersCB", "Numbers", welcomeDialogDiv);
createAndDisplayCheckboxItem("SpecialCharactersCB", "Special characters", welcomeDialogDiv);
createAndDisplaySlider(welcomeDialogDiv);
// Replace the textarea with welcome dialog
card.replaceChild(welcomeDialogDiv, textArea);
}
// End region
// Region create and display check box items
function createAndDisplayCheckboxItem(checkboxID, checkboxText, welcomeDialogDiv) {
var checkbox = document.createElement('input');
checkbox.setAttribute("type", "checkbox");
checkbox.setAttribute("name", "checkbox");
checkbox.id = checkboxID;
checkbox.addEventListener('change', function () {
if (this.checked) {
pwdUserInput.push(checkbox.id);
console.log(`${checkbox.id} checked`);
console.log(`${pwdUserInput} `);
}
if (this.checked == false) {
pwdUserInput.pop(checkbox.id);
console.log(`${checkbox.id} un-checked`);
console.log(`${pwdUserInput} `);
}
});
var label = document.createElement("label");
var textNode = document.createTextNode(checkboxText);
label.htmlFor = checkboxID
label.appendChild(textNode);
welcomeDialogDiv.appendChild(checkbox);
welcomeDialogDiv.appendChild(label);
welcomeDialogDiv.appendChild(document.createElement("br"));
return checkbox;
}
// End region
// Region create and display slider
function createAndDisplaySlider(welcomeDialogDiv) {
var sliderElement = document.createElement("input");
sliderElement.setAttribute("type", "range");
sliderElement.setAttribute("class", "slider");
sliderElement.setAttribute("id", "sliderElement");
sliderElement.setAttribute("min", "8");
sliderElement.setAttribute("max", "128");
sliderElement.setAttribute("value", "64");
sliderElement.oninput = function () {
pTag.setAttribute("id", "sliderLabel");
pTag.textContent = `Range: ${sliderElement.value} `;
pwdSliderRange = sliderElement.value;
}
welcomeDialogDiv.appendChild(pTag);
welcomeDialogDiv.appendChild(sliderElement);
console.log(sliderElement.value);
}
// End region
// Region generate random password
function generatePassword() {
var pwd = [];
if (pwdUserInput === undefined || pwdUserInput.length == 0) {
displayPromptIfNoInput();
} else {
if (pwdUserInput.includes("UppercaseCB")) {
pwd = pwd + upperCaseChars;
}
if (pwdUserInput.includes("LowercaseCB")) {
pwd = pwd + lowerCaseChars;
}
if (pwdUserInput.includes("NumbersCB")) {
pwd = pwd + numbers;
}
if (pwdUserInput.includes("SpecialCharactersCB")) {
pwd = pwd + specialChars;
}
while (result.length < pwdSliderRange) {
result += pwd[Math.floor(Math.random() * pwd.length)];
}
console.log(`Password -> ${result}`);
}
}
// End region
// Region no input prompt
function displayPromptIfNoInput() {
window.alert("Please select an option below to create password");
}
// End region
// Region reset checkboxes, slider, and slider text on clicking back button
function clearInputPreSelections() {
document.getElementById("UppercaseCB").checked = false;
document.getElementById("LowercaseCB").checked = false;
document.getElementById("NumbersCB").checked = false;
document.getElementById("SpecialCharactersCB").checked = false;
document.getElementById("sliderElement").value = "64";
pwdSliderRange = 64;
pTag.textContent = "";
}
// End region