APG/public/script.js
2025-03-28 23:35:26 +03:00

99 lines
4.3 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
// Password Generation Elements
const symbolsStringInput = document.getElementById('symbolsString');
const useSymbolsCheckbox = document.getElementById('useSymbols');
const generatePasswordButton = document.getElementById('generatePasswordButton');
const passwordResultsDiv = document.getElementById('passwordResults');
const lengthInput = document.getElementById('length');
const lengthValueSpan = document.getElementById('lengthValue');
const countInput = document.getElementById('count');
const countValueSpan = document.getElementById('countValue');
const includeNumbersCheckbox = document.getElementById('includeNumbers');
const resetSymbolsButton = document.getElementById('resetSymbols'); // Get the reset symbols button
const defaultSymbols = `\!\@\#\$\%\^\&\*\(\)\_\+\=\-\`\~\[\]\{\}\|\;\\\'\:\"\,\.\/\<\>\?`; // Store default symbols
// --- Password Generation Functions ---
function toggleSymbolsInput() {
symbolsStringInput.disabled = !useSymbolsCheckbox.checked;
}
function generatePasswords(length, includeLowercase, includeUppercase, includeNumbers, includeSymbols, symbolsString, count) {
//const allSymbols = `\!\@\#\$\%\^\&\*\(\)\_\+\=\-\`\~\[\]\{\}\|\;\\\'\:\"\,\.\/\<\>\?`;
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numberChars = '0123456789';
let allowedChars = '';
if (includeLowercase) allowedChars += lowercaseChars;
if (includeUppercase) allowedChars += uppercaseChars;
if (includeNumbers) allowedChars += numberChars;
if (includeSymbols) {
allowedChars += symbolsString;
}
if (allowedChars.length === 0) {
return ["Выберите хотя бы один тип символов."];
}
let passwords = [];
for (let i = 0; i < count; i++) {
let password = '';
while (password.length < length) {
const randomIndex = Math.floor(Math.random() * allowedChars.length);
password += allowedChars.charAt(randomIndex);
if (allowedChars.length === 0) {
break;
}
}
passwords.push(password);
}
return passwords;
}
// --- Event Listeners ---
useSymbolsCheckbox.addEventListener('change', toggleSymbolsInput);
resetSymbolsButton.addEventListener('click', function () { // Add event listener for reset button
symbolsStringInput.value = defaultSymbols;
});
generatePasswordButton.addEventListener('click', function () {
const length = parseInt(lengthInput.value);
const includeLowercase = document.getElementById('lowercase').checked;
const includeUppercase = document.getElementById('uppercase').checked;
const includeNumbers = includeNumbersCheckbox.checked;
const includeSymbols = useSymbolsCheckbox.checked;
const symbolsString = symbolsStringInput.value;
const count = parseInt(countInput.value);
const passwords = generatePasswords(length, includeLowercase, includeUppercase, includeNumbers, includeSymbols, symbolsString, count);
passwordResultsDiv.textContent = passwords.join('\n');
});
// Update range value display
lengthInput.addEventListener('input', function () {
lengthValueSpan.textContent = lengthInput.value;
});
countInput.addEventListener('input', function () {
countValueSpan.textContent = countInput.value;
});
// Initial setup
toggleSymbolsInput();
lengthValueSpan.textContent = lengthInput.value;
countValueSpan.textContent = countInput.value;
const length = parseInt(lengthInput.value);
const includeLowercase = document.getElementById('lowercase').checked;
const includeUppercase = document.getElementById('uppercase').checked;
const includeNumbers = includeNumbersCheckbox.checked;
const includeSymbols = useSymbolsCheckbox.checked;
const symbolsString = symbolsStringInput.value;
const count = parseInt(countInput.value);
const passwords = generatePasswords(length, includeLowercase, includeUppercase, includeNumbers, includeSymbols, symbolsString, count);
passwordResultsDiv.textContent = passwords.join('\n');
});