]> git.r.bdr.sh - rbdr/junction/blob - extension/content_script.js
Build with Babel
[rbdr/junction] / extension / content_script.js
1 (() => {
2
3 const io = require('socket.io-client');
4
5 const internals = {
6
7 kSocketUrl: 'http://unlimited.pizza:8000/',
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
24 console.log('Connecting to server');
25 internals.socket.on('connect', function() {
26
27 console.log("Connected to signaling server");
28 internals.socket.emit('join', {
29 'url': currentUrl,
30 });
31 });
32
33 internals.socket.on('disconnect', function() {
34
35 console.log("disconnected from signaling server");
36 });
37
38 internals.socket.on('addPeer', function(data) {
39
40 console.log(data);
41 internals.peers++;
42 console.log(`There are now ${internals.peers} participants`);
43 });
44
45 internals.socket.on('removePeer', function() {
46
47 internals.peers--;
48 console.log(`There are now ${internals.peers} participants`);
49 });
50
51 console.log('Done!');
52 internals.createAudioElement(data.tada);
53 }
54 catch (err) {
55
56 internals.port.postMessage({
57 action: 'error'
58 });
59 internals.port.disconnect();
60 internals.createAudioElement(data.tada);
61 }
62 },
63
64 hangUp() {
65 document.querySelectorAll('.junction-call-audio').forEach((audioElement) => audioElement.remove());
66 internals.socket.close();
67 internals.port.disconnect();
68 },
69
70 createAudioElement(source, type = 'audio/wav') {
71
72 const audioElement = document.createElement('audio');
73 audioElement.setAttribute('class', 'junction-call-audio');
74 audioElement.src = source;
75 audioElement.autoplay = 'autoplay';
76 audioElement.type = type;
77 document.querySelector('body').appendChild(audioElement);
78 }
79 };
80
81 internals.port = chrome.runtime.connect({ name:"content" });
82 internals.port.onMessage.addListener(internals.onMessage);
83 })();
84
85 // Indicates to the background script that we executed correctly
86 true;