forked from mjmartis/discord_web_sys_ptt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
76 lines (67 loc) · 2.03 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
/**
* The default minimum number of ms that any 'PTT active' window can last.
* @const {number}
*/
const MIN_PTT_LENGTH_DEFAULT = 100;
/**
* 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) {
browser.storage.local.get('broadcastingTab', function(result) {
if (result.broadcastingTab != null) {
cb(result.broadcastingTab);
}
});
}
var port;
/**
* 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) {
browser.storage.local.get('broadcastingTab', function(result) {
if (broadcasting) {
browser.browserAction.setBadgeText({
text: 'ON',
});
browser.storage.local.set({
broadcastingTab: id,
});
port = browser.runtime.connectNative("discord_web_sys_ptt_native");
port.onMessage.addListener((response) => {
console.log(response);
withBroadcastingTab(function(id) {
browser.tabs.sendMessage(id, {
id: 'ext_shortcut_pushed',
});
});
});
} else if (result.broadcastingTab === id) {
browser.browserAction.setBadgeText({
text: '',
});
browser.storage.local.set({
broadcastingTab: null,
});
port.disconnect();
}
});
}
// Handle messages from Discord tabs and popups.
browser.runtime.onMessage.addListener(function(msg, sender, cb) {
if (msg.id === 'discord_loaded' || msg.id === 'popup_loaded') {
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;
});