-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
executable file
·97 lines (88 loc) · 2.42 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
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
(() => {
"use strict"
if (window.kebabRemoved) return
window.kebabRemoved = true
let filt = []
// class names
const RESN_CN = "restaurantName"
const REMD_CN = "removedKebab"
const RSLS_CN = "ys-reslist"
const ITEM_CN = "ys-item"
const HEAD_CN = "head"
const hasch = (rt, cn) => {
var b = rt.getElementsByClassName(cn)
if (b && b.length > 0) return b[0]
return null
}
const haslink = (conf, href) => {
let l = conf.rmd.filter(x => x.href === href)
if (l && l.length > 0) return l
return null
}
const applyFilter = (conf, nd = document.body) => {
let filtered = []
let its = nd.getElementsByClassName(ITEM_CN)
if (!its || its.length == 0) return filtered
for (let it of its) {
let hd = hasch(it, HEAD_CN)
if (hd) {
let ln = hasch(hd, RESN_CN)
if (ln) {
let link = ln.href
let nm = ln.textContent.trim()
if (!hasch(hd, "rmkebab-btn")) {
let callback = () => block(link, nm)
hd.prepend(makeButton(callback))
}
let entry = haslink(conf, ln.href)
if (entry) {
it.classList.add(REMD_CN)
if (filtered.filter(x => x.href == link) == 0)
filtered.push({href: link, nm: nm})
} else while (it.classList.contains(REMD_CN))
it.classList.remove(REMD_CN)
}
}
}
return filtered
}
const updateConfig = c => {
if (c) {
filt = applyFilter(c)
sendCurrent(filt)
} else requestConfig()
}
// outgoing messages
// to bg
const requestConfig = () => browser.runtime.sendMessage({event: MSG_REQUEST_CONF}).
then(c => updateConfig(c), e => updateConfig(null))
// to bg
const block = (href, name) => browser.runtime.sendMessage(
{event: MSG_BLOCK, href: href, nm: name}).catch(e => {})
// to bg / popup
const sendCurrent = filtered => browser.runtime.sendMessage({
event: MSG_UPDATE, filtered: filtered}).catch(e => {})
// incoming messages
const handler = (msg, snd, sendResponse) => {
let resp = {}
switch (msg.event) {
// from bg
case MSG_CONFIG:
updateConfig(msg.config)
break
// from popup
case MSG_REQUEST_FILT:
resp = filt
break
}
return Promise.resolve(resp)
}
// initialize
browser.runtime.onMessage.addListener(handler)
var reslists = document.getElementsByClassName(RSLS_CN)
if (reslists) {
let mo = new MutationObserver(ms => requestConfig().then(() => sendCurrent(filt)))
for (let reslist of reslists) mo.observe(reslist, {subtree: true, childList: true})
}
requestConfig()
})()