/
maxxon
/
web-static-labs
Обзор
Документация
Войти
/
maxxon
/
web-static-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
html/lab5/task6/script.js
77 строк
4 KB
Dasha_10
изменения
19 окт 2025, 18:51
19 окт 2025, 18:51
b6adcb0
Код
Авторство
О чём код?
document.addEventListener('DOMContentLoaded', function() { const passwordLengthInput = document.getElementById('passwordLength'); const includeNumbersCheckbox = document.getElementById('includeNumbers'); const includeLowercaseCheckbox = document.getElementById('includeLowercase'); const includeUppercaseCheckbox = document.getElementById('includeUppercase'); const includeSymbolsCheckbox = document.getElementById('includeSymbols'); const generateButton = document.getElementById('generateButton'); const generatedPasswordElement = document.getElementById('generatedPassword'); const copyButton = document.getElementById('copyButton'); const resultSection = document.getElementById('resultSection'); function generatePassword(length, includeNumbers, includeLowercase, includeUppercase, includeSymbols) { let characters = ''; if (includeNumbers) characters += '0123456789'; if (includeLowercase) characters += 'abcdefghijklmnopqrstuvwxyz'; if (includeUppercase) characters += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; if (includeSymbols) characters += '!@#$%^&*()_+~`|}{[]\:;?><,./-='; if (characters.length === 0) { return 'Выберите хотя бы один тип символов.'; } let password = ''; for (let i = 0; i < length; i++) { const randomIndex = Math.floor(Math.random() * characters.length); password += characters.charAt(randomIndex); } return password; } generateButton.addEventListener('click', function() { const length = parseInt(passwordLengthInput.value); const includeNumbers = includeNumbersCheckbox.checked; const includeLowercase = includeLowercaseCheckbox.checked; const includeUppercase = includeUppercaseCheckbox.checked; const includeSymbols = includeSymbolsCheckbox.checked; const password = generatePassword(length, includeNumbers, includeLowercase, includeUppercase, includeSymbols); generatedPasswordElement.textContent = password; resultSection.style.display = 'block'; // Показываем секцию с паролем и кнопкой }); copyButton.addEventListener('click', function() { const password = generatedPasswordElement.textContent; // Polyfill для старых браузеров function copyTextToClipboard(text) { var textArea = document.createElement("textarea"); textArea.style.position = 'fixed'; textArea.style.top = 0; textArea.style.left = 0; textArea.style.width = '2em'; textArea.style.height = '2em'; textArea.style.padding = 0; textArea.style.border = 'none'; textArea.style.outline = 'none'; textArea.style.boxShadow = 'none'; textArea.style.background = 'transparent'; textArea.value = text; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { var successful = document.execCommand('copy'); var msg = successful ? 'successful' : 'unsuccessful'; console.log('Copying text command was ' + msg); } catch (err) { console.log('Oops, unable to copy'); } document.body.removeChild(textArea); } copyTextToClipboard(password); alert('Пароль скопирован в буфер обмена!'); // Можно заменить на более изящное уведомление }); });