]>
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 'url': data
.currentUrl
,
49 internals
.socket
.on('disconnect', function() {
51 console
.log("disconnected from signaling server");
54 internals
.socket
.on('addPeer', function(data
) {
57 Peers
.add(data
.peer_id
, internals
.tada
);
58 const peerId
= data
.peer_id
;
60 const peerConn
= new RTCPeerConnection(
61 {"iceServers": internals
.kIceServers
},
62 {"optional": [{"DtlsSrtpKeyAgreement": true}]}
65 internals
.peers
[peerId
] = peerConn
;
67 peerConn
.onicecandidate
= (event
) => {
68 if (event
.candidate
) {
69 internals
.socket
.emit('relayICECandidate', {
72 'sdpMLineIndex': event
.candidate
.sdpMLineIndex
,
73 'candidate': event
.candidate
.candidate
79 peerConn
.onaddstream
= (stream
) => {
80 console
.log(`Received stream for peer ${peerId}`);
84 peerConn
.addStream(mediaStream
);
86 if (data
.should_create_offer
) {
87 console
.log("Creating RTC offer to ", peerId
);
88 peerConn
.createOffer((local_description
) => {
89 console
.log("Local offer description is: ", local_description
);
90 peerConn
.setLocalDescription(local_description
, () => {
91 internals
.socket
.emit('relaySessionDescription', {
93 'session_description': local_description
96 console
.log("Offer setLocalDescription succeeded");
97 }, () => { console
.log("Offer setLocalDescription failed!"); }
100 (error
) => { console
.log("Error sending offer: ", error
) }
103 console
.log(`There are now ${Peers.count()} participants`);
106 internals
.socket
.on('removePeer', function() {
108 Peers
.remove('id-'+(Peers
.count() - 1)); // This is only for testing, don't use count to remove ids.
109 console
.log(`There are now ${Peers.count()} participants`);
114 internals
.port
.postMessage({
117 internals
.port
.disconnect();
125 internals
.socket
.close();
126 internals
.port
.disconnect();
130 internals
.port
= chrome
.runtime
.connect({ name:"content" });
131 internals
.port
.onMessage
.addListener(internals
.onMessage
);
133 console
.log('Content Script Loaded');
135 // Indicates to the background script that we executed correctly