-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
191 lines (157 loc) · 6.55 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
const adjectives = []; // Load from adjectives.txt
const nouns = []; // Load from nouns.txt
document.addEventListener("DOMContentLoaded", async () => {
await loadWords();
document.getElementById("generate").addEventListener("click", generatePassword);
document.getElementById("copy").addEventListener("click", copyPassword);
document.getElementById("copyFormat").addEventListener("click", copyFormattedPassword);
document.getElementById("generate-api").addEventListener("click", generatePasswordViaAPI);
// Set the default password length to 14
document.getElementById("length").value = 24;
});
async function loadWords() {
// Load words from the text files
try {
const [adjectiveResponse, nounResponse] = await Promise.all([
fetch('adjectives.txt'),
fetch('nouns.txt')
]);
adjectives.push(...(await adjectiveResponse.text()).split('\n'));
nouns.push(...(await nounResponse.text()).split('\n'));
} catch (error) {
console.error('Error loading words:', error);
}
}
function generatePassword() {
const length = parseInt(document.getElementById("length").value);
const passwordParts = [];
let totalLength = 0;
// Loop to generate words and check their length against total password length
while (totalLength < length - 3) { // Leave space for numbers and a symbol
const word = getRandomWord();
if (word) {
passwordParts.push(capitalizeFirstLetter(word));
totalLength += word.length;
}
// Ensure at least one word is added
if (passwordParts.length === 0) {
alert("Please increase the length to accommodate at least one word.");
return;
}
}
const symbols = "!@#$%^&*()-_=+[]{}|;:,.<>?";
const randomSymbol = symbols[Math.floor(Math.random() * symbols.length)];
const randomNumbers = generateRandomNumbers(2);
const password = passwordParts.join('') + randomSymbol + randomNumbers;
document.getElementById("password").value = password;
addToHistory(password);
}
function generatePasswordViaAPI() {
const length = document.getElementById("length").value || 24; // Default to 12 if not provided
const apiURL = `https://api.psswd.org/?length=${length}`;
fetch(apiURL)
.then(response => response.json())
.then(data => {
// Display the password in the result area
document.getElementById("password").value = data;
// Add the API-generated password to the history
addToHistory(data);
})
.catch(error => {
console.error('Error generating password via API:', error);
});
}
function getRandomWord() {
const allWords = [...adjectives, ...nouns];
return allWords[Math.floor(Math.random() * allWords.length)].trim();
}
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function generateRandomNumbers(count) {
let numbers = '';
for (let i = 0; i < count; i++) {
numbers += Math.floor(Math.random() * 10);
}
return numbers;
}
function addToHistory(password) {
const historyContainer = document.getElementById("passwordHistory");
// Check if the maximum number of history items has been reached
if (historyContainer.children.length >= 5) {
historyContainer.removeChild(historyContainer.lastChild); // Remove the oldest entry
}
// Create the new password entry
const historyEntry = document.createElement("div");
historyEntry.classList.add("history-entry");
historyEntry.innerHTML = `
<span>${password}</span>
<div class="button-group">
<button class="copyHistory">Copy</button>
<button class="copyFormatHistory">Copy & Format</button>
</div>
`;
// Prepend the new password entry to the top
historyContainer.prepend(historyEntry);
// Add event listeners for the new buttons
historyEntry.querySelector('.copyHistory').addEventListener('click', () => copyPassword(password, historyEntry.querySelector('.copyHistory')));
historyEntry.querySelector('.copyFormatHistory').addEventListener('click', () => copyFormattedPassword(password));
}
// Copy regular password
function copyPassword() {
const password = document.getElementById("password").value;
// Check if the Clipboard API is supported
if (navigator.clipboard) {
navigator.clipboard.writeText(password).then(() => {
changeButtonText("copy"); // Show tick on Copy button
}).catch(err => {
console.error("Error copying password: ", err);
});
} else {
fallbackCopyText(password, "copy");
}
}
// Copy formatted password
function copyFormattedPassword() {
const password = document.getElementById("password").value;
const formattedText = `Login Details\nUsername: \nPassword: ${password}`;
if (navigator.clipboard) {
navigator.clipboard.writeText(formattedText).then(() => {
changeButtonText("copyFormat"); // Show tick on Copy & Format button
}).catch(err => {
console.error("Error copying formatted password: ", err);
});
} else {
fallbackCopyText(formattedText, "copyFormat");
}
}
// Fallback method for older browsers without Clipboard API
function fallbackCopyText(text, buttonId) {
const tempTextarea = document.createElement("textarea");
tempTextarea.value = text;
document.body.appendChild(tempTextarea);
tempTextarea.select();
document.execCommand("copy");
document.body.removeChild(tempTextarea);
// Show tick
changeButtonText(buttonId);
}
function changeButtonText(buttonId) {
const button = document.getElementById(buttonId);
const originalText = button.getAttribute('data-original-text'); // Store original text
// Save the original button text if not already saved
if (!originalText) {
button.setAttribute('data-original-text', button.textContent.trim());
}
// Hide the original text and show the tick
button.textContent = ''; // Clear the button text temporarily
// Create a tick element and append it to the button
const tick = document.createElement('span');
tick.innerHTML = '✓'; // Text tick
button.appendChild(tick); // Append the tick to the button
// After 1 second, remove the tick and restore the original text
setTimeout(() => {
tick.remove(); // Remove the tick
button.textContent = button.getAttribute('data-original-text'); // Restore original text
}, 1000); // Show the tick for 1 second
}