function generatePassword(len, specialChar) { // Define character sets for each type of character const lowercase = 'abcdefghijklmnopqrstuvwxyz'; const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; const numbers = '0123456789'; let pool = lowercase + uppercase + numbers; let password = ''; if (specialChar) { // Adding special Characters to the pool; const specialCharInput = specialChar; pool += specialCharInput; // Initialize the password with one character of each type password = lowercase.charAt(Math.floor(Math.random() * lowercase.length)) + uppercase.charAt(Math.floor(Math.random() * uppercase.length)) + numbers.charAt(Math.floor(Math.random() * numbers.length)) + specialCharInput.charAt(Math.floor(Math.random() * specialCharInput.length)); } else { // Initialize the password with one character of each type password = lowercase.charAt(Math.floor(Math.random() * lowercase.length)) + uppercase.charAt(Math.floor(Math.random() * uppercase.length)) + numbers.charAt(Math.floor(Math.random() * numbers.length)); } // Fill the rest of the password with random characters for (let i = password.length; i < len; i++) { password += pool.charAt(Math.floor(Math.random() * pool.length)); } // Shuffle the password to randomize the order of characters return password.split('').sort(() => Math.random() - 0.5).join(''); } function generatePasswords(n, len, specialChar) { const passwords = []; for (let i = 0; i < n; i++) { passwords.push(generatePassword(len, specialChar)); } return passwords; } const queryParams = new URLSearchParams(window.location.search); const n = queryParams.get('n') || 4; const len = queryParams.get('len') || 14; const specialChar = queryParams.get('specialChar'); const passwords = generatePasswords(n, len, specialChar); if (!queryParams.get('n')) { document.querySelector('.password-container').innerHTML = `
`; } else { const passwordContainer = document.querySelector('.password-container'); passwords.forEach(password => { const passwordElement = document.createElement('div'); passwordElement.classList.add('password'); passwordElement.innerHTML = ` ${password} `; passwordElement.querySelector('.copy-button').addEventListener('click', () => { copyToClipboard(passwordElement.querySelector('.password-text')); }); passwordContainer.appendChild(passwordElement); }); } function copyToClipboard(element) { const textArea = document.createElement('textarea'); textArea.value = element.innerText; document.body.appendChild(textArea); textArea.select(); document.execCommand('copy'); document.body.removeChild(textArea); const button = element.parentElement.querySelector('.copy-button'); button.textContent = 'Kopiert!'; button.style.backgroundColor = 'grey'; } document.getElementById('specialBool').onchange = function() { document.getElementById('specialChar').disabled = !this.checked; } function fillIfEmpty() { var inputElement = document.getElementById('specialChar'); if (inputElement.value === '') { inputElement.value = '@%+!#$?:.(){}[]\/\\'; } }