-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
128 lines (113 loc) · 5 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
document.addEventListener('DOMContentLoaded', function() {
const passwordDisplay = document.getElementById('password');
const generateButton = document.querySelector('.generate-btn');
const copyButton = document.getElementById('copy');
const lengthInput = document.getElementById('length');
const slider = document.getElementById('slider');
const complexityRadios = document.querySelectorAll('input[name="complexity"]');
const uppercaseCheckbox = document.getElementById('uppercase');
const lowercaseCheckbox = document.getElementById('lowercase');
const numbersCheckbox = document.getElementById('numbers');
const symbolsCheckbox = document.getElementById('symbols');
const UPPERCASE_CHAR_CODES = arrayFromLowToHigh(65, 90);
const LOWERCASE_CHAR_CODES = arrayFromLowToHigh(97, 122);
const NUMBER_CHAR_CODES = arrayFromLowToHigh(48, 57);
const SYMBOL_CHAR_CODES = arrayFromLowToHigh(33, 47).concat(
arrayFromLowToHigh(58, 64)
).concat(
arrayFromLowToHigh(91, 96)
).concat(
arrayFromLowToHigh(123, 126)
);
// Generate password event listener
generateButton.addEventListener('click', generatePassword);
// Copy password to clipboard event listener
copyButton.addEventListener('click', copyToClipboard);
// Update slider value
slider.addEventListener('input', function() {
lengthInput.value = slider.value;
});
// Update length input value
lengthInput.addEventListener('input', function() {
slider.value = lengthInput.value;
});
// Complexity radio buttons event listener
complexityRadios.forEach(function(radio) {
radio.addEventListener('change', function() {
updateCheckboxes(this.value);
generatePassword();
});
});
// Checkbox event listeners
[uppercaseCheckbox, lowercaseCheckbox, numbersCheckbox, symbolsCheckbox].forEach(function(checkbox) {
checkbox.addEventListener('change', generatePassword);
});
// Generate Password function
function generatePassword() {
const length = lengthInput.value;
let includeUppercase = uppercaseCheckbox.checked;
let includeLowercase = lowercaseCheckbox.checked;
let includeNumbers = numbersCheckbox.checked;
let includeSymbols = symbolsCheckbox.checked;
// Check that at least one character type is selected
if (!includeUppercase && !includeLowercase && !includeNumbers && !includeSymbols) {
alert('Please select at least one character type.');
return;
}
const password = generateRandomPassword(length, includeUppercase, includeLowercase, includeNumbers, includeSymbols);
passwordDisplay.value = password;
}
// Copy to clipboard function
function copyToClipboard() {
passwordDisplay.select();
document.execCommand('copy');
alert('Password copied to clipboard');
}
// Generate random password function
function generateRandomPassword(length, includeUppercase, includeLowercase, includeNumbers, includeSymbols) {
let charCodes = [];
if (includeUppercase) charCodes = charCodes.concat(UPPERCASE_CHAR_CODES);
if (includeLowercase) charCodes = charCodes.concat(LOWERCASE_CHAR_CODES);
if (includeNumbers) charCodes = charCodes.concat(NUMBER_CHAR_CODES);
if (includeSymbols) charCodes = charCodes.concat(SYMBOL_CHAR_CODES);
const passwordCharacters = [];
for (let i = 0; i < length; i++) {
const characterCode = charCodes[Math.floor(Math.random() * charCodes.length)];
passwordCharacters.push(String.fromCharCode(characterCode));
}
return passwordCharacters.join('');
}
// Helper function to generate array of character codes
function arrayFromLowToHigh(low, high) {
const array = [];
for (let i = low; i <= high; i++) {
array.push(i);
}
return array;
}
// Update checkboxes based on selected complexity
function updateCheckboxes(complexity) {
if (complexity === 'all') {
uppercaseCheckbox.checked = true;
lowercaseCheckbox.checked = true;
numbersCheckbox.checked = true;
symbolsCheckbox.checked = true;
numbersCheckbox.disabled = false;
symbolsCheckbox.disabled = false;
} else if (complexity === 'easy-to-say') {
uppercaseCheckbox.checked = true;
lowercaseCheckbox.checked = true;
numbersCheckbox.checked = false;
symbolsCheckbox.checked = false;
numbersCheckbox.disabled = true;
symbolsCheckbox.disabled = true;
} else if (complexity === 'easy-to-read') {
uppercaseCheckbox.checked = true;
lowercaseCheckbox.checked = true;
numbersCheckbox.checked = false;
symbolsCheckbox.checked = false;
numbersCheckbox.disabled = false;
symbolsCheckbox.disabled = false;
}
}
});