-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (50 loc) · 1.75 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
import randomPassword from 'https://unpkg.com/[email protected]/bundle.mjs';
let options = {
numbers: false,
lowerChar: true,
upperChar: true,
specialChar: true,
rangeCount: 8,
};
let countRange = document.querySelector('[type="range"]');
const checkBoxes = document.querySelectorAll('[type="checkbox"]');
const countLabel = document.getElementById('range-count');
const countInfo = document.getElementById('count-info');
const _password = document.getElementById('random-password');
const copyIcon = document.getElementById('copy-password');
const reloadIcon = document.getElementById('reload-password');
generatePassword();
for (let i = 0; i < checkBoxes.length; i++) {
let checkbox = checkBoxes[i];
checkbox.addEventListener('click', function () {
options[this.id] = this.checked ? true : false;
generatePassword();
});
}
countRange.addEventListener('change', function () {
options[this.id] = this.value;
generatePassword();
});
copyIcon.addEventListener('click', function () {
const copiedPassword = _password.innerHTML;
navigator.clipboard.writeText(copiedPassword);
});
reloadIcon.addEventListener('click', function () {
generatePassword();
});
function generatePassword() {
const password = randomPassword(options?.rangeCount, {
includeNumbers: options?.numbers,
includeLowerCase: options?.lowerChar,
includeUpperCase: options?.upperChar,
includeSpecialChar: options?.specialChar,
});
for (let i = 0; i < checkBoxes.length; i++) {
let checkbox = checkBoxes[i];
checkbox.checked = options[checkbox.id];
}
_password.innerHTML = password ? password : '🧞....';
countLabel.innerHTML = options?.rangeCount;
countInfo.innerHTML = Number(options?.rangeCount) > 1 ? 'Characters' : 'Character';
countRange.value = Number(options?.rangeCount);
}