-
Notifications
You must be signed in to change notification settings - Fork 7
/
background.js
53 lines (47 loc) · 1.16 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
(function () {
let isEnabled = false;
/**
* Method to toggle the state of the extension.
*/
function toggleState() {
isEnabled = !isEnabled;
chrome.browserAction.setIcon({ path: isEnabled ? 'icon_enabled.png' : 'icon_disabled.png' });
console.log(`SilverDog is now globally ${isEnabled ? 'enabled' : 'disabled'}.`);
}
/**
* Method to notify the content script.
* @param {number} tabId The id of the the notification is intended to.
* @param {string} origin The origin of the notification.
*/
function notifyContent(tabId, origin) {
chrome
.tabs
.sendMessage(tabId, {
origin: origin,
state: isEnabled
});
}
chrome
.browserAction
.onClicked
.addListener(toggleState);
/**
* Notify the content sript when it requests thats.
*/
chrome
.runtime
.onMessage
.addListener(function (message, sender) {
notifyContent(sender.tab.id, message.origin);
});
/**
* Notify the content sript when a tab is updated.
*/
chrome
.tabs
.onUpdated
.addListener(function (tabId) {
notifyContent(tabId, 'backgroundScript');
});
toggleState();
})();