-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
executable file
·56 lines (49 loc) · 1.82 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
const renderItems = items => {
var q
items = items || []
document.querySelector("#hdr b").innerText = items.length
var lst = document.querySelector("#lst")
while (q = lst.lastChild) lst.removeChild(q)
if (items) for (let rs of items) lst.appendChild(renderItem(rs.href, rs.nm))
}
const renderItem = (href, text) => {
var ln = document.createElement("div")
ln.classList.add("restnm")
let callback = () => unblock(href)
ln.appendChild(makeButton(callback))
var tx = document.createElement("span")
tx.addEventListener("click", e => browser.tabs.create({active: false, url: href}))
tx.innerText = text
ln.appendChild(tx)
return ln
}
const toLastTab = cb => browser.tabs.query({active: true, lastFocusedWindow: true}).
then(tabs => {if (tabs && tabs.length == 1) cb(tabs[0])})
// outgoing messages
// to bg
const unblock = href => browser.runtime.sendMessage({event: MSG_UNBLOCK, href: href}).catch(e => {})
// to tab
const requestFiltered = () => toLastTab(tab =>
browser.tabs.sendMessage(tab.id, {event: MSG_REQUEST_FILT}).then
(f => renderItems(f), e => renderItems(null)))
// incoming messages
const handler = (msg, snd) => {
let resp = {}
switch (msg.event) {
// from tabs
case MSG_UPDATE:
toLastTab(tab => {if (tab.id == snd.tab.id) renderItems(msg.filtered)})
break
}
return Promise.resolve(resp)
}
// initialize
browser.runtime.onMessage.addListener(handler)
document.getElementById("refresh").addEventListener("click", e => requestFiltered())
document.getElementById("seeall").addEventListener("click", e =>
browser.tabs.create({active: true, url: "/main.html"}))
document.getElementById("amo").addEventListener("click", e =>
browser.tabs.create({active: true, url: AMO_URL}))
document.getElementById("homepage").addEventListener("click", e =>
browser.tabs.create({active: true, url: HOMEPAGE_URL}))
requestFiltered()