/
Coderdev
/
web-static-labs
Обзор
Документация
Войти
/
Coderdev
/
web-static-labs
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
html/lab5/task6/script.js
129 строк
4 KB
Coderdev
1
30 мар 2026, 17:29
30 мар 2026, 17:29
04c9649
Код
Авторство
О чём код?
"use strict"; const lengthInput = document.getElementById("length"); const digitsCheckbox = document.getElementById("digits"); const upperCheckbox = document.getElementById("upper"); const lowerCheckbox = document.getElementById("lower"); const specialCheckbox = document.getElementById("special"); const generateBtn = document.getElementById("generateBtn"); const copyBtn = document.getElementById("copyBtn"); const passwordOutput = document.getElementById("passwordOutput"); const error = document.getElementById("error"); const DIGITS = "0123456789"; const UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const LOWER = "abcdefghijklmnopqrstuvwxyz"; const SPECIAL = "!@#$%^&*()_+-=[]{}<>?/"; generateBtn.addEventListener("click", generatePassword); copyBtn.addEventListener("click", copyPassword); function generatePassword() { error.textContent = ""; passwordOutput.value = ""; const length = Number(lengthInput.value); if (!Number.isInteger(length) || length < 6 || length > 32) { error.textContent = "Длина пароля должна быть числом от 6 до 32."; return; } let chars = ""; const requiredChars = []; if (digitsCheckbox.checked) { chars += DIGITS; requiredChars.push(getRandomChar(DIGITS)); } if (upperCheckbox.checked) { chars += UPPER; requiredChars.push(getRandomChar(UPPER)); } if (lowerCheckbox.checked) { chars += LOWER; requiredChars.push(getRandomChar(LOWER)); } if (specialCheckbox.checked) { chars += SPECIAL; requiredChars.push(getRandomChar(SPECIAL)); } if (chars === "") { error.textContent = "Выберите хотя бы один тип символов."; return; } if (requiredChars.length > length) { error.textContent = "Длина пароля слишком маленькая для выбранных параметров."; return; } const passwordArray = [...requiredChars]; while (passwordArray.length < length) { passwordArray.push(getRandomChar(chars)); } shuffleArray(passwordArray); passwordOutput.value = passwordArray.join(""); } function getRandomChar(str) { const index = Math.floor(Math.random() * str.length); return str[index]; } function shuffleArray(arr) { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); const temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } function copyPassword() { const password = passwordOutput.value; if (!password) { error.textContent = "Сначала сгенерируйте пароль."; return; } error.textContent = ""; if (navigator.clipboard && window.isSecureContext) { navigator.clipboard.writeText(password) .then(function () { alert("Пароль скопирован в буфер обмена."); }) .catch(function () { fallbackCopy(password); }); } else { fallbackCopy(password); } } function fallbackCopy(text) { const temp = document.createElement("textarea"); temp.value = text; document.body.appendChild(temp); temp.select(); temp.setSelectionRange(0, 99999); try { document.execCommand("copy"); alert("Пароль скопирован в буфер обмена."); } catch (error) { alert("Не удалось скопировать пароль."); } document.body.removeChild(temp); }