X-Git-Url: https://git.r.bdr.sh/rbdr/junction/blobdiff_plain/ac259c8645783dcb92fc0eafff166769f5216aa9..344f7c1320d512a423c9f2133959417823f5cc83:/extension/junction.js?ds=sidebyside diff --git a/extension/junction.js b/extension/junction.js index 7de7c28..bb2d3e5 100644 --- a/extension/junction.js +++ b/extension/junction.js @@ -1,5 +1,8 @@ +const io = require('socket.io-client'); + const internals = { + promisesSupported: !!(window.browser), isInCallState: false, icons: { @@ -14,6 +17,10 @@ const internals = { } }, + socket: null, + + peers: 0, + onClick() { if (internals.isInCall()) { return internals.hangUp(); @@ -26,26 +33,51 @@ const internals = { internals.isInCallState = true; internals.setIcon('hangUp'); - const tabs = await browser.tabs.query({ - currentWindow: true, - active: true + const activeTabs = await internals.getActiveTabs(); + + const socketUrl = 'http://polypropylene.website:8000'; + const currentUrl = activeTabs[0].url; + + this.socket = io(socketUrl); + + this.socket.on('connect', function() { + console.log("Connected to signaling server"); }); - internals.createAudioElement(browser.runtime.getURL('sounds/tada.wav')); + 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) { + createAudioElement(source, type = 'audio/wav') { const audioElement = document.createElement('audio'); audioElement.src = source; audioElement.autoplay = 'autoplay'; + audioElement.type = type; document.querySelector('body').appendChild(audioElement); }, @@ -55,10 +87,37 @@ const internals = { setIcon(iconSet) { - browser.browserAction.setIcon({ + 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); + }); + }); + }, }; -browser.browserAction.onClicked.addListener(internals.onClick); +internals.getRoot().browserAction.onClicked.addListener(internals.onClick);