-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (60 loc) · 2.79 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
const generateButton = document.getElementById('generateButton');
const resetButton = document.getElementById('resetButton');
const copyButton = document.getElementById('copyButton');
const listenButton = document.getElementById('listenButton');
const passwordOutput = document.getElementById('passwordOutput');
generateButton.addEventListener('click', () => {
const passwordLength = document.getElementById('passwordLength').value;
const includeUppercase = document.getElementById('includeUppercase').checked;
const includeLowercase = document.getElementById('includeLowercase').checked;
const includeNumbers = document.getElementById('includeNumbers').checked;
const includeSpecialChars = document.getElementById('includeSpecialChars').checked;
const generatedPassword = generatePassword(passwordLength, includeUppercase, includeLowercase, includeNumbers, includeSpecialChars);
passwordOutput.textContent = generatedPassword;
speakPassword(generatedPassword); // Read out the generated password
});
resetButton.addEventListener('click', () => {
passwordOutput.textContent = '';
document.getElementById('passwordLength').value = '12';
document.getElementById('includeUppercase').checked = true;
document.getElementById('includeLowercase').checked = true;
document.getElementById('includeNumbers').checked = true;
document.getElementById('includeSpecialChars').checked = true;
});
copyButton.addEventListener('click', () => {
const password = passwordOutput.textContent;
copyToClipboard(password);
});
listenButton.addEventListener('click', () => {
const password = passwordOutput.textContent;
speakPassword(password);
});
function generatePassword(length, includeUppercase, includeLowercase, includeNumbers, includeSpecialChars) {
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
const numberChars = '0123456789';
const specialChars = '!@#$%^&*()-_=+[]{}|;:,.<>?';
let chars = '';
if (includeUppercase) chars += uppercaseChars;
if (includeLowercase) chars += lowercaseChars;
if (includeNumbers) chars += numberChars;
if (includeSpecialChars) chars += specialChars;
let generatedPassword = '';
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * chars.length);
generatedPassword += chars.charAt(randomIndex);
}
return generatedPassword;
}
function copyToClipboard(text) {
const textarea = document.createElement('textarea');
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
}
function speakPassword(text) {
const utterance = new SpeechSynthesisUtterance(text);
speechSynthesis.speak(utterance);
}