-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
71 lines (62 loc) · 1.67 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
'use strict';
const sendMessage = (action, tabId = null, responseFn = null) => {
if (!responseFn) {
responseFn = (response) => {
if(!response) return;
chrome.storage.sync.set({ globalState: response.state });
setIcon(response.state);
}
}
chrome.storage.sync.get({
global: false,
globalState: false,
disableList: []
}, (storage) => {
const message = {
action: action,
global: storage.global,
globalState: storage.globalState,
disableList: storage.disableList
}
if(tabId) {
chrome.tabs.sendMessage(tabId, message,responseFn);
} else {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
if(!tabs[0]?.id) {
return;
}
chrome.tabs.sendMessage(tabs[0].id, message, responseFn);
});
}
});
}
/**
* Handler for commands fired from keyboard shortcuts
*/
chrome.commands.onCommand.addListener(() => sendMessage('toggleLinks'));
/**
* Handler for extension button click
*/
chrome.browserAction.onClicked.addListener(() => sendMessage('toggleLinks'));
/**
* Fired when a tab is updated.
*/
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
sendMessage('refresh', tabId);
});
/**
* Fires when the active tab in a window changes
*/
chrome.tabs.onActivated.addListener(activeInfo => {
sendMessage('refresh', activeInfo.tabId);
});
/* UTILITY FUNCTIONS */
/**
* Sets the extension icon to either enabled or disabled
* @param {bool} status Boolean indicating whether the icon is enabled or not
*/
const setIcon = (status) => {
chrome.browserAction.setIcon({
path: `icons/icon48${status ? "" : 'enabled'}.png`
});
}