-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (88 loc) · 3.02 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
92
93
94
95
96
function maskPassword(pass){
let str = ""
for (let index = 0; index < pass.length; index++) {
str += "*"
}
return str
}
function copyText(txt) {
navigator.clipboard.writeText(txt).then(
() => {
/* clipboard successfully set */
document.getElementById("alert").style.display = "inline"
setTimeout(() => {
document.getElementById("alert").style.display = "none"
}, 2000);
},
() => {
/* clipboard write failed */
alert("Clipboard copying failed")
},
);
}
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))
alert(`Successfully deleted ${website}'s password`)
showPasswords()
}
// Logic to fill the table
const showPasswords = () => {
let tb = document.querySelector("table")
let data = localStorage.getItem("passwords")
if (data == null || JSON.parse(data).length == 0) {
tb.innerHTML = "No Data To Show"
}
else {
tb.innerHTML = `<tr>
<th>Website</th>
<th>Username</th>
<th>Password</th>
<th>Delete</th>
</tr> `
let arr = JSON.parse(data);
let str = ""
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
str += `<tr>
<td>${element.website} <img onclick="copyText('${element.website}')" src="./copy.svg" alt="Copy Button" width="10" width="10" height="10">
</td>
<td>${element.username} <img onclick="copyText('${element.username}')" src="./copy.svg" alt="Copy Button" width="10" width="10" height="10">
</td>
<td>${maskPassword(element.password)} <img onclick="copyText('${element.password}')" src="./copy.svg" alt="Copy Button" width="10" width="10" height="10">
</td>
<td><button class="btnsm" onclick="deletePassword('${element.website}')">Delete</button></td>
</tr>`
}
tb.innerHTML = tb.innerHTML + str
}
website.value = ""
username.value = ""
password.value = ""
}
console.log("Working");
showPasswords()
document.querySelector(".btn").addEventListener("click", (e) => {
e.preventDefault()
console.log("Clicked....")
console.log(username.value, password.value)
let passwords = localStorage.getItem("passwords")
console.log(passwords)
if (passwords == null) {
let json = []
json.push({website: website.value, username: username.value, password: password.value })
alert("Password Saved");
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 })
alert("Password Saved");
localStorage.setItem("passwords", JSON.stringify(json))
}
showPasswords()
})