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