]> git.r.bdr.sh - rbdr/junction/blob - extension/junction.js
2dbff7895b0cf1d6722b63098f294cfe09901b83
[rbdr/junction] / extension / junction.js
1 const io = require('socket.io-client');
2
3 const internals = {
4
5 promisesSupported: !!(window.browser),
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
20 socket: null,
21
22 peers: 0,
23
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');
36 const activeTabs = await internals.getActiveTabs();
37
38 const socketUrl = 'http://unlimited.pizza: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
61 console.log(activeTabs[0].url); // placeholder while we connect backend.
62 internals.createAudioElement(internals.getRoot().runtime.getURL('sounds/tada.wav'));
63 },
64
65 hangUp() {
66 this.socket.close();
67
68
69
70 document.querySelectorAll('audio').forEach((audioElement) => audioElement.remove());
71 internals.setIcon('call');
72 internals.isInCallState = false;
73 },
74
75 createAudioElement(source, type = 'audio/wav') {
76
77 const audioElement = document.createElement('audio');
78 audioElement.src = source;
79 audioElement.autoplay = 'autoplay';
80 audioElement.type = type;
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
90 internals.getRoot().browserAction.setIcon({
91 path: internals.icons[iconSet]
92 });
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 },
121 };
122
123 internals.getRoot().browserAction.onClicked.addListener(internals.onClick);