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