-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
59 lines (53 loc) · 1.64 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
const renderBlacklist = () => {
const container = document.getElementById('blacklist')
container.innerHTML = ''
chrome.storage.sync.get(['blackList'], (result) => {
const blackList = result.blackList || []
blackList.forEach((domain) => {
const item = document.createElement('div')
item.className = 'blacklist-item'
item.innerHTML = `
<span>${domain}</span>
<button data-domain="${domain}">删除</button>
`
container.appendChild(item)
})
})
}
const addDomain = (domain) => {
chrome.storage.sync.get(['blackList'], (result) => {
const blackList = result.blackList || []
if (!blackList.includes(domain)) {
blackList.push(domain)
chrome.storage.sync.set({ blackList }, () => {
renderBlacklist()
})
}
})
}
const removeDomain = (domain) => {
chrome.storage.sync.get(['blackList'], (result) => {
const blackList = result.blackList || []
const newList = blackList.filter(d => d !== domain)
chrome.storage.sync.set({ blackList: newList }, () => {
renderBlacklist()
})
})
}
document.addEventListener('DOMContentLoaded', () => {
renderBlacklist()
document.getElementById('addDomain').addEventListener('click', () => {
const input = document.getElementById('newDomain')
const domain = input.value.trim()
if (domain) {
addDomain(domain)
input.value = ''
}
})
document.getElementById('blacklist').addEventListener('click', (e) => {
if (e.target.tagName === 'BUTTON') {
const domain = e.target.dataset.domain
removeDomain(domain)
}
})
})