]> git.r.bdr.sh - rbdr/junction/blob - extension/junction.js
e78a02bc0755301ca59def111a7a84050b265467
[rbdr/junction] / extension / junction.js
1 const internals = {
2
3 promisesSupported: !!(window.browser),
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');
30 const activeTabs = await internals.getActiveTabs();
31
32 console.log(activeTabs[0].url); // placeholder while we connect backend.
33 internals.createAudioElement(internals.getRoot().runtime.getURL('sounds/tada.wav'));
34 },
35
36 hangUp() {
37
38 document.querySelectorAll('audio').forEach((audioElement) => audioElement.remove());
39 internals.setIcon('call');
40 internals.isInCallState = false;
41 },
42
43 createAudioElement(source, type = 'audio/wav') {
44
45 const audioElement = document.createElement('audio');
46 audioElement.src = source;
47 audioElement.autoplay = 'autoplay';
48 audioElement.type = type;
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
58 internals.getRoot().browserAction.setIcon({
59 path: internals.icons[iconSet]
60 });
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 },
89 };
90
91 internals.getRoot().browserAction.onClicked.addListener(internals.onClick);