-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.js
146 lines (115 loc) · 5.05 KB
/
dom.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
// dom.js
const userInputs = {
numChar: "",
uppercase: false,
lowercase: false,
numbers: false,
symbols: false
};
const subData = document.getElementById('subData');
const displayPasswordContainer = document.getElementById('password-container');
// Function to update and return the userInputs object
function getUserInputs() {
userInputs.numChar = document.getElementById('numChar').value;
userInputs.uppercase = document.getElementById('uppercase').checked;
userInputs.lowercase = document.getElementById('lowercase').checked;
userInputs.numbers = document.getElementById('numbers').checked;
userInputs.symbols = document.getElementById('symbols').checked;
return userInputs;
}
subData.addEventListener('click', () => {
console.log('button clicked');
// Get the updated user inputs
const updatedUserInputs = getUserInputs();
console.log('updated User Inputs:', updatedUserInputs);
console.log('======================')
analyzeUserInputs(updatedUserInputs)
// Check if the generate button already exists
if (!document.getElementById('generateBtn')) {
const generateContainer = document.getElementById('generateContainer')
const generateBtn = document.createElement('button');
generateBtn.id = 'generateBtn';
generateBtn.innerText = 'Generate Secret Key!!';
generateContainer.append(generateBtn);
} else {
console.log('Generate button already exists');
}
generateBtn.addEventListener('click', () => {
getUserInputs()
analyzeUserInputs(userInputs)
randomArray(userInputs)
disableButton()
const password = secureKeyMaker(pickedArray)
console.log('password: ' + password);
displayPasswordContainer.append(password)
const clipboardContainer = document.getElementById('clipboardContainer');
const clearDataContainer = document.getElementById('clearDataContainer');
//const generatedPassword = secureKeyMaker();
//copyToClipboard(generatedPassword);
//alert('Password Copied to Clipboard!');
// Check if the clipboard button already exists
if (!document.getElementById('clipboardBtn')) {
const clipboardBtn = document.createElement('button');
clipboardBtn.id = 'clipboardBtn';
clipboardBtn.innerText = 'Copy to Clipboard';
clipboardContainer.append(clipboardBtn);
clipboardBtn.addEventListener('click', () => {
console.log('copy clicked');
const generatedPassword = password;
copyToClipboard(generatedPassword);
alert('Password Copied to Clipboard!');
});
} else {
console.log('Clipboard button already exists');
}
// Check if the clear and reload button already exists
if (!document.getElementById('clearDataBtn')) {
const clearDataBtn = document.createElement('button');
clearDataBtn.id = 'clearDataBtn';
clearDataBtn.innerText = 'Clear & Reload';
clearDataContainer.append(clearDataBtn);
clearDataBtn.addEventListener('click', () => {
clearScreen();
});
} else {
console.log('Clear data button already exists');
}
});
})
// Disable the generate password button from appending anything after the first click
clearAppendedData();
//displayPasswordContainer.innerText = generatedPassword; // Assuming secureKeyMaker returns a string
// return updatedUserInputs
//});
function clearAppendedData() {
displayPasswordContainer.innerHTML = '';
}
function copyToClipboard(text) {
if (navigator.clipboard && window.isSecureContext) {
// Navigator clipboard api method
return navigator.clipboard.writeText(text).then(() => {
console.log('Copied to clipboard');
}, (err) => {
console.error('Failed to copy: ', err);
});
} else {
// Fallback method for older browsers
const textArea = document.createElement('textarea');
textArea.value = text;
// Make the textarea out of viewport
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
return new Promise((resolve, reject) => {
document.execCommand('copy') ? resolve() : reject();
textArea.remove();
});
}
}
const clearScreen = () => { return location.reload(); };
// Example of using updated user inputs outside the click event
// You can call getUserInputs() to get the latest inputs whenever needed
console.log('Initial userInputs:', getUserInputs());
//const generateBtn = document.createElement('button');