-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
116 lines (107 loc) · 2.97 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// console.log("hiii");
let btn=document.querySelector(".btn");
console.log(btn);
function encryptPwd(txt)
{
let str="";
for(let i=0;i<txt.length;i++)
{
str+="*";
}
// alert(str);
return str;
}
function textCopy(text)
{
// alert("insd")
navigator.clipboard.writeText(text)
.then(()=>{
alert("copied");
document.getElementById("cpy_icon").style.display="inline";
setTimeout(()=>{
document.getElementById("cpy_icon").style.display="none";
},2000);
},
()=>{
alert("not copied")
}
)
}
const deletePassword=(website)=>{
//one website has one password
const data=JSON.parse(localStorage.getItem("passwords"));
let newarr=data.filter((ele)=>{
if(ele.website!=website)
{
return ele;
}
})
localStorage.setItem("passwords",JSON.stringify(newarr));
alert("password is deleted");
showTable();
}
const showTable=()=>{
let table=document.querySelector("table");
const data=localStorage.getItem("passwords");
console.log(data);
if(data==null || data.length==0)
{
// alert("ff")
table.innerHTML="<h4> NO password found</h4>";
}
else{
let dataarray=JSON.parse(data);
table.innerHTML=` <tr>
<th>Website</th>
<th>Username</th>
<th>Password</th>
<th>Delete</th>
</tr> `;
let str="";
dataarray.forEach((ele)=>{
str+=`<tr>
<td>${ele.website}<i onclick="textCopy('${ele.website}')" class="fa-solid fa-copy"></i></td>
<td>${ele.username}<i onclick="textCopy('${ele.username}')" class="fa-solid fa-copy"></i></td>
<td>${encryptPwd(ele.password)}<i onclick="textCopy('${ele.password}') "class="fa-solid fa-copy"></i></td>
<td><button class="delbtn" onclick="deletePassword('${ele.website}')">delete</button></td>
</tr>`
})
table.innerHTML+=str;
}
}
showTable();
btn.onclick=(e)=>{
e.preventDefault();
if(website.value==""||username.value==""|| password.value=="")
{
alert("input filed is empty ");
}
// alert("hello");
//id.value is used to get value of input field
else{
let obj={
website:website.value,
username:username.value,
password:password.value
};
console.log(obj);
let data=localStorage.getItem("passwords");
if(data==null)
{
let jsondata=[];
jsondata.push(obj);
alert("password is saved");
localStorage.setItem("passwords",JSON.stringify(jsondata));
}
else{
let jsondata=JSON.parse(localStorage.getItem("passwords"));
console.log(jsondata)
jsondata.push(obj);
alert("password is saved");
localStorage.setItem("passwords",JSON.stringify(jsondata));
}
document.querySelector("form").reset();
// window.location.reload();
showTable();
}
}