-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (110 loc) · 4.7 KB
/
Copy pathscript.js
File metadata and controls
133 lines (110 loc) · 4.7 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
document.addEventListener("DOMContentLoaded", () => {
const addTab = document.getElementById('addTab');
const listTab = document.getElementById('listTab');
const addSection = document.getElementById('addSection');
const listSection = document.getElementById('listSection');
const usernameInput = document.getElementById('usernameInput');
const passwordInput = document.getElementById('passwordInput');
const saveBtn = document.getElementById('saveBtn');
const clearBtn = document.getElementById('clearBtn');
const generateBtn = document.getElementById('generateBtn');
const tableBody = document.getElementById('tableBody');
// Switch tabs
addTab.addEventListener('click', () => {
addSection.style.display = 'block';
listSection.style.display = 'none';
addTab.style.backgroundColor = '#ffeb3b';
listTab.style.backgroundColor = 'transparent';
});
listTab.addEventListener('click', () => {
addSection.style.display = 'none';
listSection.style.display = 'block';
listTab.style.backgroundColor = '#ffeb3b';
addTab.style.backgroundColor = 'transparent';
renderTable(); // show updated table
});
// Clear form
clearBtn.addEventListener('click', () => {
usernameInput.value = '';
passwordInput.value = '';
});
// Generate hard password
generateBtn.addEventListener('click', () => {
const strongPass = generateStrongPassword();
passwordInput.value = strongPass;
});
// Save password
saveBtn.addEventListener('click', () => {
const username = usernameInput.value.trim();
const password = passwordInput.value.trim();
if (!username || !password) {
alert("Both fields are required!");
return;
}
const saved = getSavedPasswords();
saved.push({ username, password });
localStorage.setItem('passwords', JSON.stringify(saved));
usernameInput.value = '';
passwordInput.value = '';
alert("Saved!");
renderTable();
});
// Generate a strong password
function generateStrongPassword() {
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+[]{}|:;<>,.?/~";
let pass = "";
for (let i = 0; i < 12; i++) {
const index = Math.floor(Math.random() * chars.length);
pass += chars[index];
}
return pass;
}
// Get saved passwords from localStorage
function getSavedPasswords() {
return JSON.parse(localStorage.getItem('passwords')) || [];
}
// Render table rows
function renderTable() {
const saved = getSavedPasswords();
tableBody.innerHTML = "";
saved.forEach((entry, index) => {
const tr = document.createElement('tr');
const usernameTd = document.createElement('td');
usernameTd.textContent = entry.username;
const passwordTd = document.createElement('td');
const hiddenPassword = '•'.repeat(entry.password.length);
passwordTd.textContent = hiddenPassword;
passwordTd.dataset.password = entry.password;
passwordTd.dataset.hidden = "true";
const actionsTd = document.createElement('td');
const toggleBtn = document.createElement('button');
toggleBtn.textContent = "Show";
toggleBtn.addEventListener('click', () => {
if (passwordTd.dataset.hidden === "true") {
passwordTd.textContent = passwordTd.dataset.password;
passwordTd.dataset.hidden = "false";
toggleBtn.textContent = "Hide";
} else {
passwordTd.textContent = '•'.repeat(passwordTd.dataset.password.length);
passwordTd.dataset.hidden = "true";
toggleBtn.textContent = "Show";
}
});
const deleteBtn = document.createElement('button');
deleteBtn.textContent = "Delete";
deleteBtn.addEventListener('click', () => {
saved.splice(index, 1);
localStorage.setItem('passwords', JSON.stringify(saved));
renderTable();
});
actionsTd.appendChild(toggleBtn);
actionsTd.appendChild(deleteBtn);
tr.appendChild(usernameTd);
tr.appendChild(passwordTd);
tr.appendChild(actionsTd);
tableBody.appendChild(tr);
});
}
// On page load
renderTable();
});