]> git.r.bdr.sh - rbdr/junction/blob - extension/content_script.js
Fix rooms and force ws protocol
[rbdr/junction] / extension / content_script.js
1 (() => {
2
3 const io = require('socket.io-client');
4
5 const internals = {
6
7 kSocketUrl: 'https://junction.unlimited.pizza/',
8
9 port: null,
10 socket: null,
11 peers: 0,
12
13 onMessage(message) {
14 internals[message.action](message.data);
15 },
16
17 async joinAudioCall(data) {
18
19 try {
20 const mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true });
21
22 internals.socket = io(internals.kSocketUrl, {
23 transports: ['websocket']
24 });
25
26 internals.socket.on('error', function(error) {
27
28 console.error('GENERAL ERROR', error);
29 });
30
31 internals.socket.on('connect_error', function(error) {
32
33 console.error('CONNNECT ERROR', error);
34 });
35
36 internals.socket.on('connect', function() {
37
38 console.log("Connected to signaling server, group: ", data.currentUrl);
39 internals.socket.emit('join', {
40 'url': data.currentUrl,
41 });
42 });
43
44 internals.socket.on('disconnect', function() {
45
46 console.log("disconnected from signaling server");
47 });
48
49 internals.socket.on('addPeer', function(data) {
50
51 console.log(data);
52 internals.peers++;
53 console.log(`There are now ${internals.peers} participants`);
54 });
55
56 internals.socket.on('removePeer', function() {
57
58 internals.peers--;
59 console.log(`There are now ${internals.peers} participants`);
60 });
61
62 internals.createAudioElement(data.tada);
63 }
64 catch (err) {
65
66 internals.port.postMessage({
67 action: 'error'
68 });
69 internals.port.disconnect();
70 internals.createAudioElement(data.tada);
71 }
72 },
73
74 hangUp() {
75 document.querySelectorAll('.junction-call-audio').forEach((audioElement) => audioElement.remove());
76 internals.socket.close();
77 internals.port.disconnect();
78 },
79
80 createAudioElement(source, type = 'audio/wav') {
81
82 const audioElement = document.createElement('audio');
83 audioElement.setAttribute('class', 'junction-call-audio');
84 audioElement.src = source;
85 audioElement.autoplay = 'autoplay';
86 audioElement.type = type;
87 document.querySelector('body').appendChild(audioElement);
88 }
89 };
90
91 internals.port = chrome.runtime.connect({ name:"content" });
92 internals.port.onMessage.addListener(internals.onMessage);
93 })();
94
95 // Indicates to the background script that we executed correctly
96 true;