]>
Commit | Line | Data |
---|---|---|
1ed219c8 RBR |
1 | const internals = { |
2 | ||
a94a5407 | 3 | promisesSupported: !!(window.browser), |
1ed219c8 RBR |
4 | isInCallState: false, |
5 | ||
6 | icons: { | |
7 | call: { | |
8 | 16: 'icons/action-16.png', | |
9 | 32: 'icons/action-32.png' | |
10 | }, | |
11 | ||
12 | hangUp: { | |
13 | 16: 'icons/hang_up-16.png', | |
14 | 32: 'icons/hang_up-32.png' | |
15 | } | |
16 | }, | |
17 | ||
18 | onClick() { | |
19 | if (internals.isInCall()) { | |
20 | return internals.hangUp(); | |
21 | } | |
22 | ||
23 | return internals.joinAudioCall(); | |
24 | }, | |
25 | ||
26 | async joinAudioCall() { | |
27 | ||
28 | internals.isInCallState = true; | |
29 | internals.setIcon('hangUp'); | |
a94a5407 RBR |
30 | console.log('setting tabs'); |
31 | const activeTabs = await internals.getActiveTabs(); | |
1ed219c8 | 32 | |
a94a5407 RBR |
33 | console.log(activeTabs[0].url); // placeholder while we connect backend. |
34 | internals.createAudioElement(internals.getRoot().runtime.getURL('sounds/tada.wav')); | |
1ed219c8 RBR |
35 | }, |
36 | ||
37 | hangUp() { | |
38 | ||
39 | document.querySelectorAll('audio').forEach((audioElement) => audioElement.remove()); | |
40 | internals.setIcon('call'); | |
41 | internals.isInCallState = false; | |
42 | }, | |
43 | ||
a94a5407 | 44 | createAudioElement(source, type = 'audio/wav') { |
1ed219c8 RBR |
45 | |
46 | const audioElement = document.createElement('audio'); | |
47 | audioElement.src = source; | |
48 | audioElement.autoplay = 'autoplay'; | |
a94a5407 | 49 | audioElement.type = type; |
1ed219c8 RBR |
50 | document.querySelector('body').appendChild(audioElement); |
51 | }, | |
52 | ||
53 | isInCall() { | |
54 | return internals.isInCallState; // this should be replaced with actually checking the built stuff | |
55 | }, | |
56 | ||
57 | setIcon(iconSet) { | |
58 | ||
a94a5407 | 59 | internals.getRoot().browserAction.setIcon({ |
1ed219c8 RBR |
60 | path: internals.icons[iconSet] |
61 | }); | |
a94a5407 RBR |
62 | }, |
63 | ||
64 | getRoot() { | |
65 | ||
66 | return window.browser || window.chrome; | |
67 | }, | |
68 | ||
69 | // Chrome doesn't yet implement the promise based tabs.query :'( | |
70 | ||
71 | getActiveTabs() { | |
72 | ||
73 | const query = { | |
74 | currentWindow: true, | |
75 | active: true | |
76 | }; | |
77 | ||
78 | if (internals.promisesSupported) { | |
79 | return internals.getRoot().tabs.query(query); | |
80 | } | |
81 | ||
82 | return new Promise((resolve, reject) => { | |
83 | ||
84 | internals.getRoot().tabs.query(query, (tabs) => { | |
85 | ||
86 | return resolve(tabs); | |
87 | }); | |
88 | }); | |
89 | }, | |
1ed219c8 RBR |
90 | }; |
91 | ||
a94a5407 | 92 | internals.getRoot().browserAction.onClicked.addListener(internals.onClick); |