-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
98 lines (82 loc) · 3.2 KB
/
script.js
File metadata and controls
98 lines (82 loc) · 3.2 KB
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
const lengthSlider = document.querySelector(".pass-length input"),
options = document.querySelectorAll(".option input"),
copyIcon = document.querySelector(".input-box span"),
passwordInput = document.querySelector(".input-box input"),
passIndicator = document.querySelector(".pass-indicator"),
generateBtn = document.querySelector(".generate-btn");
// Устанавливаем placeholder
passwordInput.placeholder = "Your password";
const characters = {
lowercase: "abcdefghijklmnopqrstuvwxyz",
uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
numbers: "0123456789",
symbols: "~!@#$%^&*()_+-=?[]{}\\|;':<>/,."
}
const generatePassword = () => {
let staticPassword = "",
randomPassword = "",
excludeDuplicate = false,
passLength = lengthSlider.value;
// Счетчик выбранных опций
let selectedOptions = 0;
options.forEach(option => {
if(option.checked) {
if(option.id !== "exc-duplicate" && option.id !== "spaces") {
staticPassword += characters[option.id];
selectedOptions++;
} else if(option.id === "spaces") {
staticPassword += " ";
} else {
excludeDuplicate = true;
}
}
});
// Если нет выбранных опций - показываем placeholder
if(selectedOptions === 0) {
passwordInput.value = "";
return;
}
// Генерация пароля
for (let i = 0; i < passLength; i++) {
const randomChar = staticPassword[Math.floor(Math.random() * staticPassword.length)];
if(excludeDuplicate) {
if(!randomPassword.includes(randomChar) || randomChar === " ") {
randomPassword += randomChar;
} else {
i--;
}
} else {
randomPassword += randomChar;
}
}
passwordInput.value = randomPassword;
}
const updatePassIndicator = () => {
passIndicator.id = lengthSlider.value <= 8 ? "weak" :
lengthSlider.value <= 16 ? "medium" : "strong";
}
const updateSlider = () => {
document.querySelector(".pass-length span").innerText = lengthSlider.value;
generatePassword();
updatePassIndicator();
}
// Инициализация при полной загрузке страницы
window.addEventListener("load", () => {
// Сбрасываем все чекбоксы
options.forEach(option => option.checked = false);
// Устанавливаем значения по умолчанию
document.getElementById("lowercase").checked = true;
document.getElementById("numbers").checked = true;
// Обновляем UI
updateSlider();
});
// Обработчики событий
copyIcon.addEventListener("click", () => {
if(passwordInput.value) {
navigator.clipboard.writeText(passwordInput.value);
copyIcon.innerText = "check";
setTimeout(() => copyIcon.innerText = "copy_all", 600);
}
});
lengthSlider.addEventListener("input", updateSlider);
generateBtn.addEventListener("click", generatePassword);