const io = require('socket.io-client'); const internals = { promisesSupported: !!(window.browser), isInCallState: false, icons: { call: { 16: 'icons/action-16.png', 32: 'icons/action-32.png' }, hangUp: { 16: 'icons/hang_up-16.png', 32: 'icons/hang_up-32.png' } }, socket: null, peers: 0, onClick() { if (internals.isInCall()) { return internals.hangUp(); } return internals.joinAudioCall(); }, async joinAudioCall() { internals.isInCallState = true; internals.setIcon('hangUp'); const activeTabs = await internals.getActiveTabs(); const socketUrl = 'http://unlimited.pizza:8000/'; const currentUrl = activeTabs[0].url; this.socket = io(socketUrl); this.socket.on('connect', function() { console.log("Connected to signaling server"); }); this.socket.on('disconnect', function() { console.log("disconnected from signaling server"); }); this.socket.on('addPeer', function() { this.peers++; console.log(`There are now ${this.peers} participants`); }); this.socket.on('removePeer', function() { this.peers--; console.log(`There are now ${this.peers} participants`); }); console.log(activeTabs[0].url); // placeholder while we connect backend. internals.createAudioElement(internals.getRoot().runtime.getURL('sounds/tada.wav')); }, hangUp() { this.socket.close(); document.querySelectorAll('audio').forEach((audioElement) => audioElement.remove()); internals.setIcon('call'); internals.isInCallState = false; }, createAudioElement(source, type = 'audio/wav') { const audioElement = document.createElement('audio'); audioElement.src = source; audioElement.autoplay = 'autoplay'; audioElement.type = type; document.querySelector('body').appendChild(audioElement); }, isInCall() { return internals.isInCallState; // this should be replaced with actually checking the built stuff }, setIcon(iconSet) { internals.getRoot().browserAction.setIcon({ path: internals.icons[iconSet] }); }, getRoot() { return window.browser || window.chrome; }, // Chrome doesn't yet implement the promise based tabs.query :'( getActiveTabs() { const query = { currentWindow: true, active: true }; if (internals.promisesSupported) { return internals.getRoot().tabs.query(query); } return new Promise((resolve, reject) => { internals.getRoot().tabs.query(query, (tabs) => { return resolve(tabs); }); }); }, }; internals.getRoot().browserAction.onClicked.addListener(internals.onClick);