-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
140 lines (111 loc) · 4.43 KB
/
popup.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Open the database
var openDB = async () => {
return new Promise((resolve, reject) => {
const request = window.indexedDB.open("UserDataDB", 1);
request.onerror = (event) => {
reject(`Error opening database: ${event.target.errorCode}`);
};
request.onsuccess = (event) => {
const db = event.target.result;
resolve(db);
};
request.onupgradeneeded = (event) => {
const db = event.target.result;
const objectStore = db.createObjectStore("UserData", { keyPath: "id", autoIncrement: true });
objectStore.createIndex("username", "username", { unique: false });
objectStore.createIndex("age", "age", { unique: false });
};
});
};
var insertData = async () => {
const db = await openDB();
const transaction = db.transaction(["UserData"], "readwrite");
const objectStore = transaction.objectStore("UserData");
const username = document.getElementById("username").value;
const age = parseInt(document.getElementById("age").value);
const data = { username, age };
const request = objectStore.add(data);
request.onsuccess = () => {
console.log("Data inserted successfully!");
};
request.onerror = (event) => {
console.error(`Error inserting data: ${event.target.error}`);
};
};
var updateData = async () => {
const db = await openDB();
const transaction = db.transaction(["UserData"], "readwrite");
const objectStore = transaction.objectStore("UserData");
const username = document.getElementById("username").value;
const age = parseInt(document.getElementById("age").value);
const data = { username, age };
// Get the existing record by username
const getRequest = objectStore.index("username").get(username);
getRequest.onsuccess = (event) => {
const existingData = event.target.result;
if (existingData) {
// Update the existing record with the new data
existingData.age = data.age;
// Use put without specifying the key to update the record
const updateRequest = objectStore.put(existingData);
updateRequest.onsuccess = () => {
console.log("Data updated successfully!");
listData(); // Refresh the displayed list after update
};
updateRequest.onerror = (event) => {
console.error(`Error updating data: ${event.target.error}`);
};
} else {
console.error("Record not found for username:", username);
}
};
getRequest.onerror = (event) => {
console.error(`Error getting data for username ${username}: ${event.target.error}`);
};
};
var deleteData = async () => {
const db = await openDB();
const transaction = db.transaction(["UserData"], "readwrite");
const objectStore = transaction.objectStore("UserData");
const request = objectStore.clear();
request.onsuccess = () => {
console.log("All data deleted successfully!");
listData(); // Refresh the displayed list after deletion
};
request.onerror = (event) => {
console.error(`Error deleting all data: ${event.target.error}`);
};
};
// Function to list all data
var listData = async () => {
const db = await openDB();
const transaction = db.transaction(["UserData"], "readonly");
const objectStore = transaction.objectStore("UserData");
const request = objectStore.getAll();
request.onsuccess = (event) => {
const data = event.target.result;
displayDataAsList(data);
console.log("List of Data:", data);
// Display or process the data as needed
};
request.onerror = (event) => {
console.error(`Error listing data: ${event.target.error}`);
};
};
// Function to display data as an unordered list
var displayDataAsList = (data) => {
const resultList = document.getElementById("resultList");
resultList.innerHTML = ""; // Clear previous results
data.forEach((entry) => {
const listItem = document.createElement("li");
listItem.textContent = `Username: ${entry.username}, Age: ${entry.age}`;
resultList.appendChild(listItem);
});
};
document.addEventListener('DOMContentLoaded', function () {
// Button event listeners
document.getElementById("insertBtn").addEventListener("click", insertData);
document.getElementById("updateBtn").addEventListener("click", updateData);
document.getElementById("deleteBtn").addEventListener("click", deleteData);
document.getElementById("listBtn").addEventListener("click", listData);
});