]> git.r.bdr.sh - rbdr/junction/blame - extension/src/junction.js
Build system
[rbdr/junction] / extension / src / junction.js
CommitLineData
1ed219c8
RBR
1const 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 30 const activeTabs = await internals.getActiveTabs();
1ed219c8 31
a94a5407
RBR
32 console.log(activeTabs[0].url); // placeholder while we connect backend.
33 internals.createAudioElement(internals.getRoot().runtime.getURL('sounds/tada.wav'));
1ed219c8
RBR
34 },
35
36 hangUp() {
37
38 document.querySelectorAll('audio').forEach((audioElement) => audioElement.remove());
39 internals.setIcon('call');
40 internals.isInCallState = false;
41 },
42
a94a5407 43 createAudioElement(source, type = 'audio/wav') {
1ed219c8
RBR
44
45 const audioElement = document.createElement('audio');
46 audioElement.src = source;
47 audioElement.autoplay = 'autoplay';
a94a5407 48 audioElement.type = type;
1ed219c8
RBR
49 document.querySelector('body').appendChild(audioElement);
50 },
51
52 isInCall() {
53 return internals.isInCallState; // this should be replaced with actually checking the built stuff
54 },
55
56 setIcon(iconSet) {
57
a94a5407 58 internals.getRoot().browserAction.setIcon({
1ed219c8
RBR
59 path: internals.icons[iconSet]
60 });
a94a5407
RBR
61 },
62
63 getRoot() {
64
65 return window.browser || window.chrome;
66 },
67
68 // Chrome doesn't yet implement the promise based tabs.query :'(
69
70 getActiveTabs() {
71
72 const query = {
73 currentWindow: true,
74 active: true
75 };
76
77 if (internals.promisesSupported) {
78 return internals.getRoot().tabs.query(query);
79 }
80
81 return new Promise((resolve, reject) => {
82
83 internals.getRoot().tabs.query(query, (tabs) => {
84
85 return resolve(tabs);
86 });
87 });
88 },
1ed219c8
RBR
89};
90
a94a5407 91internals.getRoot().browserAction.onClicked.addListener(internals.onClick);