-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
73 lines (65 loc) · 1.71 KB
/
background.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
function getInternalIDFromURL(url) {
// Get only the main part of the URL, not the scheme nor fragment
const parsedURL = new URL(url);
return `${parsedURL.host}/${parsedURL.pathname}${parsedURL.search}`;
}
function getActiveTab() {
// Get the current active tab
return browser.tabs.query({ active: true, currentWindow: true })
.then(tabs => ({
internalId: getInternalIDFromURL(tabs[0].url),
data: tabs[0],
}))
}
function getTagForTab(id) {
return browser.storage.local.get(id)
.then(tag => {
const values = Object.values(tag);
return values.length === 1 ? values[0] : {};
})
}
function getTagForActiveTab() {
let activeTab;
return getActiveTab()
.then(tab => {
activeTab = tab;
return getTagForTab(tab.internalId)
})
.then(tag => ({ tag, tab: activeTab }));
}
function updatePopUp(color, text) {
return browser.runtime.sendMessage({
command: "updatePopUp",
color,
text,
});
}
function updateIcon(tabId, color = "default") {
browser.browserAction.setIcon({
tabId,
path: {
16: `icons/${color}_16.png`,
32: `icons/${color}_32.png`,
48: `icons/${color}_48.png`,
96: `icons/${color}_96.png`,
},
})
}
function update() {
getTagForActiveTab()
.then(({ tag, tab }) => {
return Promise.all([
updatePopUp(tag.color, tag.text),
updateIcon(tab.id, tag.color),
])
})
}
browser.tabs.onActivated.addListener(update);
browser.tabs.onUpdated.addListener(update);
browser.runtime.onMessage.addListener(message => {
if (message.command === "update") {
update();
} else if (message.command === "getActiveTabInternalId") {
return getActiveTab().then(tab => tab.internalId);
}
})