-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
59 lines (52 loc) · 1.88 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
let currentItems = await chrome.storage.local.get("currentItems").then(e => e?.currentItems) || ["issues", "pulls"]
let actions = await fetch("./actions.json").then(e => e.json())
actions.forEach(action => {
document.getElementById("items").insertAdjacentHTML("beforeend",`
<input id="${action.id}" type="checkbox"/>
<label for="${action.id}">${action.name}</label><br>
`)
document.getElementById(action.id).checked = currentItems.includes(action.id)
document.getElementById(action.id).addEventListener("change", e => {
e.target.checked ? addItem(e.target.id) : removeItem(e.target.id)
})
})
writeTable()
function addItem(id){
currentItems.push(id)
writeTable()
}
function removeItem(id){
currentItems = currentItems.filter(item => item !== id)
writeTable()
}
function upItem(index){
if(currentItems[index - 1]){
let move = currentItems.splice(index, 1)[0]
currentItems.splice(index - 1, 0, move)
writeTable()
}
}
function downItem(index){
if(currentItems[index + 1]){
let move = currentItems.splice(index, 1)[0]
currentItems.splice(index + 1, 0, move)
writeTable()
}
}
function writeTable(){
let target = document.getElementById("currentItems")
target.innerHTML = ""
chrome.storage.local.set({"currentItems": currentItems})
currentItems.forEach((item, index) => {
let itemDict = actions.find(e => item == e.id)
target.insertAdjacentHTML("beforeend",`
<tr>
<th>${itemDict.name}</th>
<th><a id="up${item}" href="#!">↑</a></th>
<th><a id="down${item}" href="#!">↓</a></th>
</tr>
`)
document.getElementById(`up${item}`).addEventListener("click", () => upItem(index))
document.getElementById(`down${item}`).addEventListener("click", () => downItem(index))
})
}