-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
70 lines (63 loc) · 2.13 KB
/
content.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
let adsCount = 0
// 页面加载和
function updateContentStyle () {
Promise.all([getSelectors(), getWhiteList()]).then(function ([selectorList, whiteList]) {
if (whiteList.includes(location.host)) { // 跳过白名单网站
const styleElem = document.querySelector('#Noad-style')
if (styleElem) styleElem.parentNode.removeChild(styleElem)
sendAdsCount(0)
return
}
// 插入style
const styleElem = document.querySelector('#Noad-style') || (function () {
const style = document.createElement('style')
style.id = 'Noad-style'
document.head.appendChild(style)
return style
}())
styleElem.innerHTML = selectorList.join(',') + '{ display:none !important }'
// 计算屏蔽广告的个数
adsCount = selectorList.reduce(function (count, selector) {
return count + document.querySelectorAll(selector).length
}, 0)
sendAdsCount(adsCount)
})
}
updateContentStyle()
chrome.runtime.onMessage.addListener(function ({ action, data }, sender, sendResponse) {
if (action === 'updateContentStyle') updateContentStyle()
else if (action === 'getData') {
getWhiteList().then(function (whiteList) {
const isEffective = !whiteList.includes(location.host)
sendResponse({ adsCount, isEffective })
})
return true
} else if (action === 'toggleEffective') {
getWhiteList().then(function (whiteList) {
const host = location.host
const includes = whiteList.includes(host)
let list = []
if (!data.isEffective && !includes) list = whiteList.concat(host)
else if (data.isEffective && includes) list = whiteList.filter(function (existedHost) {
return existedHost !== host
})
setWhiteList(list)
})
}
})
function sendAdsCount (adsCount) {
chrome.runtime.sendMessage({ adsCount })
}
function getWhiteList () {
return new Promise(function (resolve) {
storage.get('whiteList', function ({ whiteList }) {
if (!whiteList) {
resolve([])
setWhiteList([])
} else resolve(whiteList)
})
})
}
function setWhiteList (whiteList) {
storage.set({ whiteList }, updateContentStyle)
}