-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
114 lines (103 loc) · 3.07 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
114
/**
* The default minimum number of ms that any 'PTT active' window can last.
* @const {number}
*/
const MIN_PTT_LENGTH_DEFAULT = 800;
/**
* Runs the given callback with the tab ID for the broadcasting Discord tab, if
* one exists.
*
* @param {function(number)} cb - A callback that will be run on the
* broadcasting Discord tab ID.
*/
function withBroadcastingTab(cb) {
chrome.storage.local.get('broadcastingTab', function(result) {
if (result.broadcastingTab != null) {
cb(result.broadcastingTab);
}
});
}
/**
* Propogates the stored min PTT length, or the default (if no value has been stored).
*
* @param {function(number)} cb - A callback that will be run with the stored
* min PTT length as its argument.
* @return {boolean} true if cb will be called asynchronously.
*/
function sendMinPttLength(cb) {
chrome.storage.local.get('minPttLength', function(result) {
cb(result.minPttLength != null ? result.minPttLength : MIN_PTT_LENGTH_DEFAULT);
});
return true;
}
/**
* Stores the new min PTT length and updates all popups and Discord tabs about
* the change.
*
* @param {number} minPttLength - The new minimum PTT length.
*/
function onMinPttLengthChanged(minPttLength) {
chrome.storage.local.set({
minPttLength: minPttLength,
});
// Send message to all popups.
chrome.runtime.sendMessage({
id: 'min_ptt_length_changed',
value: minPttLength,
});
// Send message to Discord tab.
withBroadcastingTab(function(id) {
chrome.tabs.sendMessage(id, {
id: 'min_ptt_length_changed',
value: minPttLength,
});
});
}
/**
* Updates extension badge when a Discord tab starts / stops broadcasting.
*
* @param {number} id - The ID of the tab that has started or stopped broadcasting.
* @param {boolean} broadcasting - true if this is as notice that broadcasting
* has started.
*/
function onBroadcastingNotice(id, broadcasting) {
chrome.storage.local.get('broadcastingTab', function(result) {
if (broadcasting) {
chrome.browserAction.setBadgeText({
text: 'ON',
});
chrome.storage.local.set({
broadcastingTab: id,
});
} else if (result.broadcastingTab === id) {
chrome.browserAction.setBadgeText({
text: '',
});
chrome.storage.local.set({
broadcastingTab: null,
});
}
});
}
// Handle messages from Discord tabs and popups.
chrome.runtime.onMessage.addListener(function(msg, sender, cb) {
if (msg.id === 'discord_loaded' || msg.id === 'popup_loaded') {
return sendMinPttLength(cb);
} else if (msg.id === 'min_ptt_length_changed') {
onMinPttLengthChanged(msg.value);
return false;
} else if (msg.id === 'broadcasting' && sender != null &&
sender.tab != null && sender.tab.id != null) {
onBroadcastingNotice(sender.tab.id, msg.value);
return false;
}
return false;
});
// When extension shortcut is pressed, notify Discord tab.
chrome.commands.onCommand.addListener(function() {
withBroadcastingTab(function(id) {
chrome.tabs.sendMessage(id, {
id: 'ext_shortcut_pushed',
});
});
});