-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
105 lines (85 loc) · 2.62 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
(async function () {
const formEl = document.querySelector("form");
const tableEl = document.querySelector("table");
const tbodyEl = tableEl.querySelector("tbody");
const getWhitelist = async () => {
return new Promise((resolve, reject) => {
chrome.storage.sync.get({whitelists: JSON.stringify([])}, function (result) {
if (result === undefined) {
reject();
} else {
resolve(JSON.parse(result.whitelists))
}
});
});
};
const setWhitelist = async (whitelists) => {
chrome.storage.sync.set(
{ whitelists: JSON.stringify(whitelists) },
function (result) {
console.log("whitelists is set " + result);
}
);
};
const onDeleteRow = async (btn, index) => {
// Get all the current WL's.
const whitelists = await getWhitelist();
// remove specific whitelist from the list.
whitelists.splice(index, 1);
await setWhitelist(whitelists);
// re-create the table.
formTable(whitelists);
};
const formTable = async () => {
// Get all the current WL's.
const whitelists = await getWhitelist();
// remove table before we recreate it.
tbodyEl.innerHTML = '';
if (whitelists.length === 0) {
return;
}
whitelists.forEach((whitelist, index) => {
const tr = tbodyEl.insertRow();
// Add text to the table.
Object.keys(whitelist).forEach((key) => {
const td = tr.insertCell();
const value = document.createTextNode(whitelist[key]);
td.appendChild(value);
});
// Add delete button.
const td = tr.insertCell();
const btn = document.createElement("button");
btn.innerHTML = "Delete";
btn.className = "deleteBtn";
td.appendChild(btn);
// Add event listener to the button.
btn.addEventListener("click", function () {
onDeleteRow(this, index);
});
});
};
const onAddProject = async (e) => {
// Get all the current WL's.
const whitelists = await getWhitelist();
e.preventDefault();
const link = document.getElementById("link").value;
const date = document.getElementById("date").value;
const price = document.getElementById("price").value;
const wl = document.getElementById("wl").value;
const name = document.getElementById("name").value;
// Add the new wl to the list.
whitelists.push({
name,
wl,
price,
date,
link,
});
// update the whitelists.
await setWhitelist(whitelists);
// re-create the table.
formTable(whitelists);
};
formTable();
formEl.addEventListener("submit", onAddProject);
})();