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