]>
git.r.bdr.sh - rbdr/junction/blob - extension/content_script.js
1 import { io
} from 'socket.io-client';
2 import Peers
from './peers';
3 import Media
from './media';
7 kSocketUrl: 'https://junction.tranquil.services',
9 {url:"stun:stun.l.google.com:19302"}
17 internals
[message
.action
](message
.data
);
20 async
joinAudioCall(data
) {
22 internals
.tada
= data
.tada
; // Keeping for fun
25 const mediaStream
= await Media
.start();
27 internals
.socket
= io(internals
.kSocketUrl
, {
28 transports: ['websocket']
31 internals
.socket
.on('error', function(error
) {
33 console
.error('GENERAL ERROR', error
);
36 internals
.socket
.on('connect_error', function(error
) {
38 console
.error('CONNNECT ERROR', error
);
41 internals
.socket
.on('connect', function() {
43 console
.log('Connected to signaling server, group: ', data
.currentUrl
);
44 internals
.socket
.emit('join', {
45 room: data
.currentUrl
,
49 internals
.socket
.on('disconnect', function() {
51 console
.log("disconnected from signaling server");
54 internals
.socket
.on('addPeer', function(data
) {
56 Peers
.add(data
.peerId
, internals
.tada
);
57 const peerId
= data
.peerId
;
59 const peerConnection
= new RTCPeerConnection(
60 { iceServers: internals
.kIceServers
},
61 { optional: [{ DtlsSrtpKeyAgreement: true }] }
64 internals
.peers
[peerId
] = peerConnection
;
65 mediaStream
.getTracks().forEach((track
) => {
66 peerConnection
.addTrack(track
, localStream
);
69 peerConnection
.onicecandidate
= (event
) => {
70 if (event
.candidate
) {
71 internals
.socket
.emit('relayICECandidate', {
73 candidate: event
.candidate
78 const remoteStream
= new MediaStream();
79 peerConnection
.ontrack
= (event
) => {
80 remoteStream
.addTrack(event
.track
);
81 const remoteAudioElement
= new Audio();
82 remoteAudioElement
.srcObject
= remoteStream
;
83 remoteAudioElement
.play();
86 peerConnection
.onnegotiationneeded
= async () => {
87 console
.log("Creating RTC offer to ", peerId
);
88 const offer
= await peerConnection
.createOffer();
89 await peerConnection
.setLocalDescription(offer
);
91 // Emit the offer to the peer
92 socket
.emit('relayOffer', { offer
, peerId
});
95 console
.log(`There are now ${Peers.count()} participants`);
98 socket
.on('offerReceived', async (data
) => {
100 const peerConnection
= internals
.peers
[data
.peerId
];
102 const offer
= new RTCSessionDescription(data
.offer
);
103 await peerConnection
.setRemoteDescription(offer
);
105 const answer
= await peerConnection
.createAnswer();
106 await peerConnection
.setLocalDescription(answer
);
108 // Send the answer to the peer
109 socket
.emit('relayAnswer', { answer
, peerId: data
.peerId
});
112 socket
.on('answerReceived', async (data
) => {
114 const peerConnection
= internals
.peers
[data
.peerId
];
115 const answer
= new RTCSessionDescription(data
.answer
);
116 await peerConnection
.setRemoteDescription(answer
);
119 socket
.on('ICECandidateReceived', async (data
) => {
121 const peerConnection
= internals
.peers
[data
.peerId
];
122 const candidate
= new RTCIceCandidate(data
.candidate
);
123 await peerConnection
.addIceCandidate(candidate
);
127 internals
.socket
.on('removePeer', function() {
129 delete internals
.peers
[data
.peerId
];
130 Peers
.remove('id-'+(Peers
.count() - 1)); // This is only for testing, don't use count to remove ids.
131 console
.log(`There are now ${Peers.count()} participants`);
136 internals
.port
.postMessage({
139 internals
.port
.disconnect();
147 internals
.socket
.close();
148 internals
.port
.disconnect();
152 internals
.port
= chrome
.runtime
.connect({ name:"content" });
153 internals
.port
.onMessage
.addListener(internals
.onMessage
);
155 console
.log('Content Script Loaded');
157 // Indicates to the background script that we executed correctly