]>
Commit | Line | Data |
---|---|---|
344f7c13 | 1 | const io = require('socket.io-client'); |
2 | ||
1ed219c8 RBR |
3 | const internals = { |
4 | ||
a94a5407 | 5 | promisesSupported: !!(window.browser), |
1ed219c8 RBR |
6 | isInCallState: false, |
7 | ||
8 | icons: { | |
9 | call: { | |
10 | 16: 'icons/action-16.png', | |
11 | 32: 'icons/action-32.png' | |
12 | }, | |
13 | ||
14 | hangUp: { | |
15 | 16: 'icons/hang_up-16.png', | |
16 | 32: 'icons/hang_up-32.png' | |
17 | } | |
18 | }, | |
19 | ||
344f7c13 | 20 | socket: null, |
21 | ||
22 | peers: 0, | |
23 | ||
1ed219c8 RBR |
24 | onClick() { |
25 | if (internals.isInCall()) { | |
26 | return internals.hangUp(); | |
27 | } | |
28 | ||
29 | return internals.joinAudioCall(); | |
30 | }, | |
31 | ||
32 | async joinAudioCall() { | |
33 | ||
34 | internals.isInCallState = true; | |
35 | internals.setIcon('hangUp'); | |
a94a5407 | 36 | const activeTabs = await internals.getActiveTabs(); |
1ed219c8 | 37 | |
344f7c13 | 38 | const socketUrl = 'http://polypropylene.website:8000'; |
39 | const currentUrl = activeTabs[0].url; | |
40 | ||
41 | this.socket = io(socketUrl); | |
42 | ||
43 | this.socket.on('connect', function() { | |
44 | console.log("Connected to signaling server"); | |
45 | }); | |
46 | ||
47 | this.socket.on('disconnect', function() { | |
48 | console.log("disconnected from signaling server"); | |
49 | }); | |
50 | ||
51 | this.socket.on('addPeer', function() { | |
52 | this.peers++; | |
53 | console.log(`There are now ${this.peers} participants`); | |
54 | }); | |
55 | ||
56 | this.socket.on('removePeer', function() { | |
57 | this.peers--; | |
58 | console.log(`There are now ${this.peers} participants`); | |
59 | }); | |
60 | ||
a94a5407 RBR |
61 | console.log(activeTabs[0].url); // placeholder while we connect backend. |
62 | internals.createAudioElement(internals.getRoot().runtime.getURL('sounds/tada.wav')); | |
1ed219c8 RBR |
63 | }, |
64 | ||
65 | hangUp() { | |
344f7c13 | 66 | this.socket.close(); |
67 | ||
68 | ||
1ed219c8 RBR |
69 | |
70 | document.querySelectorAll('audio').forEach((audioElement) => audioElement.remove()); | |
71 | internals.setIcon('call'); | |
72 | internals.isInCallState = false; | |
73 | }, | |
74 | ||
a94a5407 | 75 | createAudioElement(source, type = 'audio/wav') { |
1ed219c8 RBR |
76 | |
77 | const audioElement = document.createElement('audio'); | |
78 | audioElement.src = source; | |
79 | audioElement.autoplay = 'autoplay'; | |
a94a5407 | 80 | audioElement.type = type; |
1ed219c8 RBR |
81 | document.querySelector('body').appendChild(audioElement); |
82 | }, | |
83 | ||
84 | isInCall() { | |
85 | return internals.isInCallState; // this should be replaced with actually checking the built stuff | |
86 | }, | |
87 | ||
88 | setIcon(iconSet) { | |
89 | ||
a94a5407 | 90 | internals.getRoot().browserAction.setIcon({ |
1ed219c8 RBR |
91 | path: internals.icons[iconSet] |
92 | }); | |
a94a5407 RBR |
93 | }, |
94 | ||
95 | getRoot() { | |
96 | ||
97 | return window.browser || window.chrome; | |
98 | }, | |
99 | ||
100 | // Chrome doesn't yet implement the promise based tabs.query :'( | |
101 | ||
102 | getActiveTabs() { | |
103 | ||
104 | const query = { | |
105 | currentWindow: true, | |
106 | active: true | |
107 | }; | |
108 | ||
109 | if (internals.promisesSupported) { | |
110 | return internals.getRoot().tabs.query(query); | |
111 | } | |
112 | ||
113 | return new Promise((resolve, reject) => { | |
114 | ||
115 | internals.getRoot().tabs.query(query, (tabs) => { | |
116 | ||
117 | return resolve(tabs); | |
118 | }); | |
119 | }); | |
120 | }, | |
1ed219c8 RBR |
121 | }; |
122 | ||
a94a5407 | 123 | internals.getRoot().browserAction.onClicked.addListener(internals.onClick); |