-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptions.js
54 lines (44 loc) · 1.61 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
const addInactiveCourseButton = document.querySelector("#addInactiveCourse");
const inactiveCoursesList = document.querySelector("#inactiveCourses");
addInactiveCourseButton.addEventListener("click", addInactiveCourse);
async function loadInactiveCourses() {
return new Promise(resolve => {
chrome.storage.sync.get(["inactiveCourses"], (result) => resolve(result.inactiveCourses ? result.inactiveCourses : []));
})
}
async function saveInactiveCourses(inactiveCourses) {
return new Promise(resolve => {
chrome.storage.sync.set({ inactiveCourses }, () => resolve());
})
}
async function reloadInactiveCourses() {
const inactiveCourses = await loadInactiveCourses();
inactiveCoursesList.innerHTML = '';
for (const inactiveCourse of inactiveCourses) {
const li = document.createElement("li");
li.textContent = inactiveCourse + " ";
const remove = document.createElement('a');
remove.addEventListener('click', () => removeInactiveCourse(inactiveCourse));
remove.href = '#';
remove.textContent = "remove";
li.appendChild(remove);
inactiveCoursesList.appendChild(li);
}
}
async function addInactiveCourse() {
const courseName = prompt("Enter a course name");
if (courseName) {
const inactiveCourses = await loadInactiveCourses();
inactiveCourses.push(courseName);
await saveInactiveCourses(inactiveCourses);
await reloadInactiveCourses();
}
}
async function removeInactiveCourse(inactiveCourse) {
await saveInactiveCourses(
(await loadInactiveCourses())
.filter(c => c !== inactiveCourse)
);
await reloadInactiveCourses();
}
reloadInactiveCourses();