]>
Commit | Line | Data |
---|---|---|
1ed219c8 RBR |
1 | const internals = { |
2 | ||
a94a5407 | 3 | promisesSupported: !!(window.browser), |
4757c461 | 4 | port: null, |
1ed219c8 RBR |
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 | ||
4757c461 | 26 | onConnect(port) { |
1ed219c8 | 27 | |
4757c461 RBR |
28 | internals.port = port; |
29 | port.onDisconnect.addListener(internals.onDisconnect); | |
30 | port.onMessage.addListener(internals.onMessage); | |
31 | port.postMessage({ | |
32 | action: 'joinAudioCall', | |
33 | data: { | |
34 | tada: internals.getRoot().runtime.getURL('sounds/tada.wav') | |
35 | } | |
36 | }); | |
37 | internals.getRoot().browserAction.enable(); | |
1ed219c8 | 38 | internals.setIcon('hangUp'); |
1ed219c8 RBR |
39 | }, |
40 | ||
4757c461 | 41 | onMessage(message) { |
1ed219c8 | 42 | |
4757c461 RBR |
43 | if (message.action === 'error') { |
44 | internals.getRoot().browserAction.setBadgeText({ text: 'x' }, () => {}); | |
45 | } | |
46 | }, | |
47 | ||
48 | onDisconnect() { | |
49 | internals.getRoot().browserAction.setBadgeText({ text: '' }, () => {}); | |
1ed219c8 | 50 | internals.setIcon('call'); |
4757c461 RBR |
51 | internals.port = null; |
52 | internals.getRoot().browserAction.enable(); | |
1ed219c8 RBR |
53 | }, |
54 | ||
4757c461 | 55 | async joinAudioCall() { |
1ed219c8 | 56 | |
4757c461 RBR |
57 | internals.getRoot().browserAction.disable(); |
58 | const activeTabs = await internals.getActiveTabs(); | |
4757c461 | 59 | |
6393c915 RBR |
60 | const execution = await internals.getRoot().tabs.executeScript(activeTabs[0].id, { |
61 | file: '/content_script.js' | |
62 | }); | |
63 | ||
64 | if (!execution || !execution[0]) { | |
65 | internals.onDisconnect(); | |
66 | } | |
67 | } | |
68 | , | |
4757c461 RBR |
69 | |
70 | hangUp() { | |
71 | ||
72 | internals.getRoot().browserAction.disable(); | |
73 | internals.port.postMessage({ | |
74 | action: 'hangUp' | |
75 | }); | |
1ed219c8 RBR |
76 | }, |
77 | ||
78 | isInCall() { | |
4757c461 RBR |
79 | |
80 | return !!(internals.port); | |
1ed219c8 RBR |
81 | }, |
82 | ||
83 | setIcon(iconSet) { | |
84 | ||
a94a5407 | 85 | internals.getRoot().browserAction.setIcon({ |
1ed219c8 RBR |
86 | path: internals.icons[iconSet] |
87 | }); | |
a94a5407 RBR |
88 | }, |
89 | ||
90 | getRoot() { | |
91 | ||
92 | return window.browser || window.chrome; | |
93 | }, | |
94 | ||
95 | // Chrome doesn't yet implement the promise based tabs.query :'( | |
96 | ||
97 | getActiveTabs() { | |
98 | ||
99 | const query = { | |
100 | currentWindow: true, | |
101 | active: true | |
102 | }; | |
103 | ||
104 | if (internals.promisesSupported) { | |
105 | return internals.getRoot().tabs.query(query); | |
106 | } | |
107 | ||
108 | return new Promise((resolve, reject) => { | |
109 | ||
110 | internals.getRoot().tabs.query(query, (tabs) => { | |
111 | ||
112 | return resolve(tabs); | |
113 | }); | |
114 | }); | |
115 | }, | |
1ed219c8 RBR |
116 | }; |
117 | ||
a94a5407 | 118 | internals.getRoot().browserAction.onClicked.addListener(internals.onClick); |
4757c461 | 119 | internals.getRoot().runtime.onConnect.addListener(internals.onConnect); |