-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
91 lines (83 loc) · 2.7 KB
/
script.js
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
const savedTable = document.querySelector("#saved-table");
savedTable.style.display = "none";
const deletePassword = (website) => {
let data = localStorage.getItem("passwords");
let arr = JSON.parse(data);
arrUpdated = arr.filter((e) => {
return e.website !== website;
});
localStorage.setItem("passwords", JSON.stringify(arrUpdated));
showPasswords();
};
const togglePasswordVisibility = (eyeBtn) => {
const passwordText = eyeBtn.nextElementSibling; // Get the password text element
if (passwordText.style.display === "none") {
passwordText.style.display = "inline-block"; // Show the password
eyeBtn.textContent = "👁️";
} else {
passwordText.style.display = "none"; // Hide the password
eyeBtn.textContent = "🙈";
}
};
const showPasswords = () => {
const table = document.querySelector("table");
const data = localStorage.getItem("passwords");
if (data == null) {
table.innerHTML = "There is no data to show!";
} else {
table.innerHTML = `
<tr>
<th>Website</th>
<th>Username</th>
<th>Password</th>
<th>Delete</th>
</tr>`;
const arr = JSON.parse(data);
let str = "";
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
str += `<tr>
<td>${element.website}</td>
<td>${element.username}</td>
<td><span id="eyeBtn" onclick="togglePasswordVisibility(this)">👁️</span>
<span class="password-text">${element.password}</span></td>
<td><button id="deleteBtn" onclick="deletePassword('${element.website}')"><i class="fa-solid fa-trash"></i></button></td>
</tr>
`;
}
table.innerHTML = table.innerHTML + str;
}
website.value = "";
username.value = "";
password.value = "";
};
showPasswords();
const submitBtn = document.querySelector("#btn");
submitBtn.addEventListener("click", (e) => {
e.preventDefault();
if (!website.value || !username.value || !password.value) {
alert("Please fill in all fields!");
return; // Stop further processing
}
const passwords = localStorage.getItem("passwords");
if (passwords == null) {
let json = [];
json.push({
website: website.value,
username: username.value,
password: password.value,
});
localStorage.setItem("passwords", JSON.stringify(json));
} else {
let json = JSON.parse(localStorage.getItem("passwords"));
json.push({
website: website.value,
username: username.value,
password: password.value,
});
localStorage.setItem("passwords", JSON.stringify(json));
}
savedTable.style.display = "block";
showPasswords();
createSuccessToast("Password Saved Successfully");
});