-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
71 lines (60 loc) · 2.27 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
const NUMBERS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const LETTERS = [...Array(26)].map((_, i) => String.fromCharCode(i + 65));
const SYMBOLS = ['*', '#', '?', '^', '$', '%', '&', '{', '}', '[', ']', '~'];
const lengthRange = document.getElementById('length');
const lengthValueSpan = document.getElementById('lengthValue');
const passwordInput = document.getElementById('password');
const clipBoardButton = document.getElementById('clipboard');
const clipboardAlert = document.getElementById('alert');
const footerElement = document.querySelector('footer');
footerElement.innerHTML = `<p href="https://github.com/hackelite01">Copyright © ${new Date().getFullYear()} Mayank Rajput</p>`;
let state = {
numbers: true,
lowercase: true,
uppercase: true,
symbols: true,
passwordLength: 20,
};
document.addEventListener('DOMContentLoaded', () => {
if (typeof generatePassword !== 'undefined') {
generatePassword();
}
});
const copyToClipboard = async () => {
if (navigator.clipboard) {
await navigator.clipboard.writeText(passwordInput.value);
clipboardAlert.classList.add('active');
clipBoardButton.setAttribute('disabled', true);
setTimeout(() => {
clipboardAlert.classList.remove('active');
clipBoardButton.removeAttribute('disabled');
}, 4000);
}
};
clipBoardButton.addEventListener('click', copyToClipboard);
const allCheckBoxes = [...document.querySelectorAll('input[type="checkbox"]')];
allCheckBoxes.forEach((input) => {
input.addEventListener('change', (e) => {
state = { ...state, [e.target.id]: e.target.checked };
generatePassword();
});
});
lengthRange.addEventListener('input', (e) => {
lengthValueSpan.innerText = e.target.value;
state = { ...state, passwordLength: e.target.value };
generatePassword();
});
const generatePassword = () => {
let finalPassword = '';
const availableCharacters = [
...(state.numbers ? NUMBERS : []),
...(state.uppercase ? LETTERS : []),
...(state.lowercase ? LETTERS.map((letter) => letter.toLowerCase()) : []),
...(state.symbols ? SYMBOLS : []),
];
for (let i = 0; i < state.passwordLength; i++) {
const randomIndex = Math.floor(Math.random() * availableCharacters.length);
finalPassword += availableCharacters[randomIndex];
}
passwordInput.value = finalPassword;
};