-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (64 loc) · 1.92 KB
/
index.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
let passwordLength = document.getElementById('passwordLength');
let password = document.getElementById('password');
let saveButton = document.getElementById('saveButton');
let copyButton = document.getElementById('copyButton');
let form = document.getElementById('formId');
let lowerCheck = document.getElementById('lowercaseCheck');
let upperCheck = document.getElementById('uppercaseCheck');
let numbersCheck = document.getElementById('numbersCheck');
let symbolsCheck = document.getElementById('symbolsCheck');
function generatePassword(length) {
const charSets = [
lowerCheck.checked ? 'abcdefghijklmnopqrstuvwxyz' : '',
upperCheck.checked ? 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' : '',
numbersCheck.checked ? '0123456789' : '',
symbolsCheck.checked ? "!@#$%^&*()_+=-{}[]';?.,<>~`" : '',
];
let generator = '';
if (
!lowerCheck.checked &&
!upperCheck.checked &&
!numbersCheck.checked &&
!symbolsCheck.checked
) {
generator = '';
return generator;
}
const data = charSets.join('');
for (let i = 0; i < length; i++) {
generator += data[~~(Math.random() * data.length)];
}
console.log(generator);
return generator;
}
function getPassword() {
const newPassword = generatePassword(passwordLength.value);
password.value = newPassword;
if (newPassword) {
copyButton.disabled = false;
saveButton.classList.remove('disabled');
} else {
copyButton.disabled = true;
saveButton.classList.add('disabled');
}
}
const copyPassword = async () => {
try {
await navigator.clipboard.writeText(password.value);
alert('Password copied to clipboard!');
} catch (err) {
alert('Failed to copy: ', err);
}
};
const savePassword = () => {
saveButton.setAttribute(
'href',
'data:text/plain;charset=utf-8,' + encodeURIComponent(password.value),
);
saveButton.setAttribute('download', 'password.txt');
};
// Preventing page refresh
function submitForm(event) {
event.preventDefault();
}
form.addEventListener('submit', submitForm);