103 lines
4.1 KiB
JavaScript
103 lines
4.1 KiB
JavaScript
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 = `
|
|
<form action="${window.location.href}" method="GET">
|
|
<label for="n">Anzahl der Passwörter:</label>
|
|
<input type="number" id="n" name="n" value="${n}" min="1" max="128" placeholder="4" value="4" required><br>
|
|
|
|
<label for="len">Länge der Passwörter:</label>
|
|
<input type="number" id="len" name="len" value="${len}" min="4" max="32" placeholder="16" value="16" required><br>
|
|
|
|
<label for="specialChar">Sonderzeichen:</label>
|
|
<input type="checkbox" id="specialBool" checked>
|
|
<input type="text" id="specialChar" name="specialChar" value="@%+!#$?:.(){}[]\/\\" required onblur="fillIfEmpty()">
|
|
<input type="submit" value="Generieren">
|
|
</form>
|
|
`;
|
|
} else {
|
|
const passwordContainer = document.querySelector('.password-container');
|
|
passwords.forEach(password => {
|
|
const passwordElement = document.createElement('div');
|
|
passwordElement.classList.add('password');
|
|
passwordElement.innerHTML = `
|
|
<span class="password-text">${password}</span>
|
|
<button class="copy-button">Kopieren</button>
|
|
`;
|
|
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 = '@%+!#$?:.(){}[]\/\\';
|
|
}
|
|
} |