-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
60 lines (50 loc) · 1.59 KB
/
main.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
const data = {
capital: "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),
small: "abcdefghijklmnopqrstuvwxyz".split(""),
number: "0123456789".split(""),
sign: "!@#$%^&*()_-}{[]|/".split(""),
};
const title = document.querySelector(".title");
const lengthInput = document.querySelector("#length");
const checkboxes = document.querySelectorAll("input[type='checkbox']");
const password = document.querySelector(".password");
const generateBtn = document.querySelector(".generate-btn");
const copyBtn = document.querySelector(".copy-btn");
const generateCode = (filters, len) => {
if (filters.length === 0) return "";
const codeChars = [];
filters.forEach((filter) => {
codeChars.push(...data[filter]);
});
let code = "";
for (let i = 0; i < len; i++) {
const idx = Math.floor(Math.random() * codeChars.length);
code += codeChars[idx];
}
return code;
};
const copyToClipboard = (text) => {
const el = document.createElement("textarea");
el.innerText = text;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
};
generateBtn.addEventListener("click", (evt) => {
evt.preventDefault();
const len = lengthInput.value;
const filters = [];
checkboxes.forEach((input) => {
if (input.checked) filters.push(input.dataset.type);
});
const code = generateCode(filters, len);
password.innerText = code;
});
copyBtn.addEventListener("click", () => {
copyToClipboard(password.innerText);
title.innerText = "Password saved it to the clipboad";
setTimeout(() => {
title.innerText = "Password generated";
}, 750);
});