]> git.r.bdr.sh - rbdr/junction/blame - extension/junction.js
New url
[rbdr/junction] / extension / junction.js
CommitLineData
344f7c13 1const io = require('socket.io-client');
2
1ed219c8
RBR
3const internals = {
4
a94a5407 5 promisesSupported: !!(window.browser),
1ed219c8
RBR
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
344f7c13 20 socket: null,
21
22 peers: 0,
23
1ed219c8
RBR
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');
a94a5407 36 const activeTabs = await internals.getActiveTabs();
1ed219c8 37
90b43ea8 38 const socketUrl = 'http://unlimited.pizza:8000/';
344f7c13 39 const currentUrl = activeTabs[0].url;
40
41 this.socket = io(socketUrl);
42
c3fafc92 43 var that = this;
44
344f7c13 45 this.socket.on('connect', function() {
46 console.log("Connected to signaling server");
c3fafc92 47
48 that.socket.emit('join', {
49 'url': currentUrl,
50 });
344f7c13 51 });
52
53 this.socket.on('disconnect', function() {
54 console.log("disconnected from signaling server");
55 });
56
c3fafc92 57 this.socket.on('addPeer', function(data) {
58 console.log(data);
59 that.peers++;
60 console.log(`There are now ${that.peers} participants`);
344f7c13 61 });
62
63 this.socket.on('removePeer', function() {
c3fafc92 64 that.peers--;
65 console.log(`There are now ${that.peers} participants`);
344f7c13 66 });
67
a94a5407
RBR
68 console.log(activeTabs[0].url); // placeholder while we connect backend.
69 internals.createAudioElement(internals.getRoot().runtime.getURL('sounds/tada.wav'));
1ed219c8
RBR
70 },
71
72 hangUp() {
344f7c13 73 this.socket.close();
74
75
1ed219c8
RBR
76
77 document.querySelectorAll('audio').forEach((audioElement) => audioElement.remove());
78 internals.setIcon('call');
79 internals.isInCallState = false;
80 },
81
a94a5407 82 createAudioElement(source, type = 'audio/wav') {
1ed219c8
RBR
83
84 const audioElement = document.createElement('audio');
85 audioElement.src = source;
86 audioElement.autoplay = 'autoplay';
a94a5407 87 audioElement.type = type;
1ed219c8
RBR
88 document.querySelector('body').appendChild(audioElement);
89 },
90
91 isInCall() {
92 return internals.isInCallState; // this should be replaced with actually checking the built stuff
93 },
94
95 setIcon(iconSet) {
96
a94a5407 97 internals.getRoot().browserAction.setIcon({
1ed219c8
RBR
98 path: internals.icons[iconSet]
99 });
a94a5407
RBR
100 },
101
102 getRoot() {
103
104 return window.browser || window.chrome;
105 },
106
107 // Chrome doesn't yet implement the promise based tabs.query :'(
108
109 getActiveTabs() {
110
111 const query = {
112 currentWindow: true,
113 active: true
114 };
115
116 if (internals.promisesSupported) {
117 return internals.getRoot().tabs.query(query);
118 }
119
120 return new Promise((resolve, reject) => {
121
122 internals.getRoot().tabs.query(query, (tabs) => {
123
124 return resolve(tabs);
125 });
126 });
127 },
1ed219c8
RBR
128};
129
a94a5407 130internals.getRoot().browserAction.onClicked.addListener(internals.onClick);