]> git.r.bdr.sh - rbdr/junction/blame - extension/content_script.js
Update dependencies
[rbdr/junction] / extension / content_script.js
CommitLineData
80172072
RBR
1import { io } from 'socket.io-client';
2import Peers from './peers';
3import Media from './media';
d9e5fa1a 4
80172072 5const internals = {
4757c461 6
80172072 7 kSocketUrl: 'ws://localhost:8000',
d4fb48eb 8
80172072
RBR
9 port: null,
10 socket: null,
11 peers: 0,
d4fb48eb 12
80172072
RBR
13 onMessage(message) {
14 internals[message.action](message.data);
15 },
d4fb48eb 16
80172072 17 async joinAudioCall(data) {
4757c461 18
80172072 19 internals.tada = data.tada; // Keeping for fun
4757c461 20
80172072
RBR
21 try {
22 await Media.start();
4757c461 23
80172072
RBR
24 internals.socket = io(internals.kSocketUrl, {
25 transports: ['websocket']
26 });
d9e5fa1a 27
80172072 28 internals.socket.on('error', function(error) {
d4fb48eb 29
80172072
RBR
30 console.error('GENERAL ERROR', error);
31 });
02071d8e 32
80172072 33 internals.socket.on('connect_error', function(error) {
02071d8e 34
80172072
RBR
35 console.error('CONNNECT ERROR', error);
36 });
02071d8e 37
80172072 38 internals.socket.on('connect', function() {
02071d8e 39
80172072
RBR
40 console.log("Connected to signaling server, group: ", data.currentUrl);
41 internals.socket.emit('join', {
42 'url': data.currentUrl,
02071d8e 43 });
80172072 44 });
d4fb48eb 45
80172072 46 internals.socket.on('disconnect', function() {
d4fb48eb 47
80172072
RBR
48 console.log("disconnected from signaling server");
49 });
d4fb48eb 50
80172072 51 internals.socket.on('addPeer', function(data) {
d4fb48eb 52
80172072
RBR
53 console.log(data);
54 Peers.add('id-'+Peers.count(), internals.tada);
55 console.log(`There are now ${Peers.count()} participants`);
56 });
d4fb48eb 57
80172072 58 internals.socket.on('removePeer', function() {
d4fb48eb 59
80172072
RBR
60 Peers.remove('id-'+(Peers.count() - 1)); // This is only for testing, don't use count to remove ids.
61 console.log(`There are now ${Peers.count()} participants`);
62 });
63 }
64 catch (err) {
d4fb48eb 65
80172072
RBR
66 internals.port.postMessage({
67 action: 'error'
68 });
69 internals.port.disconnect();
70 }
71 },
4757c461 72
80172072 73 hangUp() {
4757c461 74
80172072
RBR
75 Peers.reset();
76 Media.stop();
77 internals.socket.close();
78 internals.port.disconnect();
79 }
80};
d9e5fa1a 81
80172072
RBR
82internals.port = chrome.runtime.connect({ name:"content" });
83internals.port.onMessage.addListener(internals.onMessage);
4757c461 84
80172072 85console.log('Content Script Loaded');
4757c461
RBR
86
87// Indicates to the background script that we executed correctly
88true;