|
| 1 | +const resultEl = document.getElementById("result"); |
| 2 | +const lengthEl = document.getElementById("length"); |
| 3 | +const uppercaseEl = document.getElementById("uppercase"); |
| 4 | +const lowercaseEl = document.getElementById("lowercase"); |
| 5 | +const numbersEl = document.getElementById("numbers"); |
| 6 | +const symbolsEl = document.getElementById("symbols"); |
| 7 | +const generateEl = document.getElementById("generate"); |
| 8 | +const clipboardEl = document.getElementById("clipboard"); |
| 9 | + |
| 10 | +const randomFunc = { |
| 11 | + lower: getRandomLower, |
| 12 | + upper: getRandomUpper, |
| 13 | + number: getRandomNumber, |
| 14 | + symbol: getRandomSymbol, |
| 15 | +}; |
| 16 | + |
| 17 | +clipboardEl.addEventListener("click", () => { |
| 18 | + const textarea = document.createElement("textarea"); |
| 19 | + const password = resultEl.innerText; |
| 20 | + |
| 21 | + if (!password) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + textarea.value = password; |
| 26 | + document.body.appendChild(textarea); |
| 27 | + textarea.select(); |
| 28 | + document.execCommand("copy"); |
| 29 | + textarea.remove(); |
| 30 | + alert("Password copied to clipboard!"); |
| 31 | +}); |
| 32 | + |
| 33 | +generateEl.addEventListener("click", () => { |
| 34 | + const length = +lengthEl.value; |
| 35 | + const hasLower = lowercaseEl.checked; |
| 36 | + const hasUpper = uppercaseEl.checked; |
| 37 | + const hasNumber = numbersEl.checked; |
| 38 | + const hasSymbol = symbolsEl.checked; |
| 39 | + |
| 40 | + resultEl.innerText = generatePassword( |
| 41 | + hasLower, |
| 42 | + hasUpper, |
| 43 | + hasNumber, |
| 44 | + hasSymbol, |
| 45 | + length |
| 46 | + ); |
| 47 | +}); |
| 48 | + |
| 49 | +function generatePassword(lower, upper, number, symbol, length) { |
| 50 | + let generatedPassword = ""; |
| 51 | + const typesCount = lower + upper + number + symbol; |
| 52 | + const typesArr = [{ lower }, { upper }, { number }, { symbol }].filter( |
| 53 | + (item) => Object.values(item)[0] |
| 54 | + ); |
| 55 | + |
| 56 | + if (typesCount === 0) { |
| 57 | + return ""; |
| 58 | + } |
| 59 | + |
| 60 | + for (let i = 0; i < length; i += typesCount) { |
| 61 | + typesArr.forEach((type) => { |
| 62 | + const funcName = Object.keys(type)[0]; |
| 63 | + generatedPassword += randomFunc[funcName](); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + const finalPassword = generatedPassword.slice(0, length); |
| 68 | + |
| 69 | + return finalPassword; |
| 70 | +} |
| 71 | + |
| 72 | +function getRandomLower() { |
| 73 | + return String.fromCharCode(Math.floor(Math.random() * 26) + 97); |
| 74 | +} |
| 75 | + |
| 76 | +function getRandomUpper() { |
| 77 | + return String.fromCharCode(Math.floor(Math.random() * 26) + 65); |
| 78 | +} |
| 79 | + |
| 80 | +function getRandomNumber() { |
| 81 | + return String.fromCharCode(Math.floor(Math.random() * 10) + 48); |
| 82 | +} |
| 83 | + |
| 84 | +function getRandomSymbol() { |
| 85 | + const symbols = "!@#$%^&*(){}[]=<>/,."; |
| 86 | + return symbols[Math.floor(Math.random() * symbols.length)]; |
| 87 | +} |
0 commit comments