-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
executable file
·113 lines (89 loc) · 2.06 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'
const STATE = internalState()
/**
* Event listeners
*/
browser.windows.onFocusChanged.addListener((windowId) => {
if (windowId === browser.windows.WINDOW_ID_NONE) {
return
}
browser.tabs.query({
active: true,
windowId: windowId,
})
.then((tab) => {
STATE.setActiveTab(tab[0].id)
managePageAction()
})
})
browser.tabs.onActivated.addListener((active) => {
STATE.setActiveTab(active.tabId)
managePageAction()
})
browser.tabs.onUpdated.addListener((tabId, changed) => {
STATE.setActiveTab(tabId)
if (typeof changed.status !== 'undefined'
&& changed.status !== 'complete'
) {
return
}
managePageAction()
})
browser.commands.onCommand.addListener((name) => {
if (name !== 'reload-css'
|| ! STATE.isIntegrationEnabled('hasHotKey')
) {
return
}
main()
})
browser.pageAction.onClicked.addListener((tab) => {
main()
})
browser.contextMenus.onClicked.addListener((info, tab) => {
main()
})
browser.storage.onChanged.addListener((change, area) => {
Object.entries(change).map((integration) => {
STATE.setIntegrationState(integration[0], integration[1].newValue)
})
managePageAction()
manageContextMenu()
})
/**
* Main extension purpose
*/
function main() {
browser.tabs.executeScript(STATE.getActiveTab(), {
file: 'assets/css-reload.js',
}).catch(err => {
// mute permission errors
})
}
browser.storage.sync.get(STATE.getDefaultIntegrations())
.then((items) => {
STATE.setIntegrations(items)
managePageAction()
manageContextMenu()
})
function managePageAction() {
if (! STATE.getActiveTab()) {
return
}
if (! STATE.isIntegrationEnabled('hasPageAction')) {
browser.pageAction.hide(STATE.getActiveTab())
return
}
browser.pageAction.show(STATE.getActiveTab())
}
function manageContextMenu() {
if (! STATE.isIntegrationEnabled('hasContextMenu')) {
browser.contextMenus.removeAll()
return
}
browser.contextMenus.create({
id: 'reloadCss',
title: browser.i18n.getMessage('contextMenuItem'),
contexts: ['all']
})
}