-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
102 lines (87 loc) · 3.81 KB
/
scripts.js
File metadata and controls
102 lines (87 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const lowercase = 'abcdefghijklmnopqrstuvwxyz';
const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const digits = '0123456789';
const special = '!"#$%&\'()*+,-./:;<=>?@[]^_`{|}~';
function generatePassword(length, characters) {
return Array.from({ length }, () => characters[Math.floor(Math.random() * characters.length)]).join('');
}
function getCharacterSet() {
let chars = '';
if (document.getElementById('lowercase').checked) chars += lowercase;
if (document.getElementById('uppercase').checked) chars += uppercase;
if (document.getElementById('digits').checked) chars += digits;
if (document.getElementById('special').checked) chars += special;
return chars;
}
async function generatePasswords() {
const length = parseInt(document.getElementById('manualPasswordLength').value);
const numPasswords = parseInt(document.getElementById('manualNumPasswords').value);
const characters = getCharacterSet();
if (!characters) {
alert('Please select at least one character set.');
return;
}
const passwordTableBody = document.querySelector('#passwordTable tbody');
passwordTableBody.innerHTML = '';
for (let i = 0; i < numPasswords; i++) {
const password = generatePassword(length, characters);
const row = passwordTableBody.insertRow();
const numberCell = row.insertCell(0);
const passwordCell = row.insertCell(1);
const actionCell = row.insertCell(2);
numberCell.textContent = i + 1;
passwordCell.textContent = password;
// Use data-password attribute to store the password
actionCell.innerHTML = `<button class="copy-btn" data-password="${encodeURIComponent(password)}">Copy</button>`;
}
}
async function copyToClipboard(button) {
const text = decodeURIComponent(button.getAttribute('data-password'));
try {
await navigator.clipboard.writeText(text);
alert('Password copied to clipboard');
} catch (err) {
alert('Failed to copy password');
}
}
document.getElementById('generateBtn').addEventListener('click', generatePasswords);
function updateRangeValue(inputId, valueId, manualInputId) {
const input = document.getElementById(inputId);
const value = document.getElementById(valueId);
input.addEventListener('input', () => {
value.textContent = input.value;
document.getElementById(manualInputId).value = input.value;
});
}
document.getElementById('manualPasswordLength').addEventListener('input', (e) => {
const value = e.target.value;
if (value >= 4 && value <= 32) {
document.getElementById('passwordLength').value = value;
document.getElementById('passwordLengthValue').textContent = value;
}
});
document.getElementById('manualNumPasswords').addEventListener('input', (e) => {
const value = e.target.value;
if (value >= 1 && value <= 10) {
document.getElementById('numPasswords').value = value;
document.getElementById('numPasswordsValue').textContent = value;
}
});
function updateCharacterSet() {
// Update character set when any checkbox changes
document.querySelectorAll('input[type="checkbox"]').forEach(checkbox => {
checkbox.addEventListener('change', () => {
const characters = getCharacterSet();
document.getElementById('generateBtn').disabled = !characters;
});
});
}
updateRangeValue('passwordLength', 'passwordLengthValue', 'manualPasswordLength');
updateRangeValue('numPasswords', 'numPasswordsValue', 'manualNumPasswords');
updateCharacterSet();
// Attach event listener to copy buttons
document.addEventListener('click', (event) => {
if (event.target.classList.contains('copy-btn')) {
copyToClipboard(event.target);
}
});