-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.js
164 lines (153 loc) · 5.84 KB
/
options.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(() => {
// const manifest = chrome.runtime.getManifest();
// document.getElementById("version").textContent = 'v' + manifest.version;
const totalSpan = document.getElementById("total-words");
loadData();
// Add a click event on buttons to open a specific modal
const modal = document.getElementById("import-modal");
const importBtn = document.getElementById("import-btn");
const fileInput = document.getElementById("id-file");
const searchInput = document.getElementById("search-input");
document.getElementById("open-modal").addEventListener("click", () => {
modal.show();
});
document.getElementById("cancel-btn").addEventListener("click", () => {
importBtn.disabled = true;
fileInput.value = null;
modal.close();
});
fileInput.addEventListener("change", () => {
importBtn.disabled = false;
});
async function loadData() {
const params = new URL(window.location.href).searchParams;
const page = parseInt(params.get('page')??1);
const search = params.get('search');
const limit = 100;
let res = await chrome.storage.local.get(["words"])
res = res.words ? res.words : []
// search
if(search) {
res = res.filter(i => i[0].includes(search))
searchInput.value = search;
}
totalSpan.textContent = res.length;
const pagination = document.getElementById("pagination");
pagination.content.querySelector(".current-page").textContent = page;
if(page > 1) {
pagination.content.querySelector(".previous-page").href = `?search=${search}&page=${page-1}`;
} else {
pagination.content.querySelector(".previous-page").remove();
}
if(res.length > page * limit) {
pagination.content.querySelector(".next-page").href = `?search=${search}&page=${page+1}`;
} else {
pagination.content.querySelector(".next-page").remove();
}
document.querySelector("table").closest("main").appendChild(pagination.content);
const tb = document.querySelector("table tbody");
res.slice((page-1)*limit, page*limit).map(i => {
const t = document.getElementById("row");
const td = t.content.querySelectorAll("td");
td[0].textContent = i[0];
td[1].textContent = i[1];
const clone = document.importNode(t.content, true);
clone.querySelector("md-text-button").addEventListener("click", async(e) => {
e.target.closest("tr").remove();
let words = await chrome.storage.local.get(['words']);
words = words.words ? words.words : [];
chrome.storage.local.set({"words": words.filter(j => j[0] !== i[0])});
});
tb.appendChild(clone);
});
}
const addModal = document.getElementById("add-modal");
const keyInput = document.querySelector("[name=key]");
const valueInput = document.querySelector("[name=value]");
const addBtn = document.getElementById("add-btn");
document.getElementById("open-add-modal").addEventListener("click", () => {
addModal.show();
});
document.getElementById("cancel-add-btn").addEventListener("click", () => {
keyInput.value = '';
valueInput.value = '';
addModal.close();
});
keyInput.addEventListener("input", (e) => {
if(!valueInput.value || !keyInput.value) {
addBtn.disabled = true;
} else {
addBtn.disabled = false;
};
});
valueInput.addEventListener("input", (e) => {
if(!valueInput.value || !keyInput.value) {
addBtn.disabled = true;
} else {
addBtn.disabled = false;
};
});
addBtn.addEventListener("click", async() => {
const key = keyInput.value;
const value = valueInput.value;
if(!key || !value) {
alert('Error');
return;
}
let words = await chrome.storage.local.get(["words"])
words = words.words ? words.words : [];
const res = await chrome.storage.local.set({"words": [[key,value], ...words ]});
// const tb = document.querySelector("table tbody");
// const t = document.getElementById("row");
// const td = t.content.querySelectorAll("td");
// td[0].textContent = key.value;
// td[1].textContent = value.value;
// const clone = document.importNode(t.content, true);
// clone.querySelector("md-text-button").addEventListener("click", async(e) => {
// e.target.closest("tr").remove();
// let words = await chrome.storage.local.get(['words']);
// words = words.words ? words.words : [];
// chrome.storage.local.set({"words": words.filter(j => j[0] !== key.value)});
// });
// //tb.insertBefore(clone, document.getElementById("add-form").nextSibling);
// tb.appendChild(clone);
keyInput.value = '';
valueInput.value = '';
addModal.close();
window.location.reload();
});
importBtn.addEventListener("click", (e) => {
//e.target.classList.add("is-loading");
e.preventDefault();
const files = document.querySelector("[name=source]").files;
for (const file of files) {
const reader = new FileReader();
reader.onload = async(e) => {
const data = JSON.parse(e.target.result);
await chrome.storage.local.set({"words":data})
window.location.reload();
}
reader.readAsText(file);
}
});
document.getElementById("clear-btn").addEventListener("click", async(e) => {
if(window.confirm('确定清空词库吗?')) {
e.target.classList.add("is-loading");
e.preventDefault();
await chrome.storage.local.set({"words":[]})
window.location.reload();
}
});
document.getElementById("export-btn").addEventListener("click", async(e) => {
const res = await chrome.storage.local.get(['words']);
const resStr = JSON.stringify(res.words);
const a = document.createElement("a");
a.href = `data:text/json;charset=utf-8,${resStr}`; //
a.download = 'my_words.json'
a.click();
});
document.getElementById("search-btn").addEventListener("click", async(e) => {
const value = searchInput.value;
window.location.href = `?search=${value}`;
});
})();