]>
git.r.bdr.sh - rbdr/junction/blob - extension/content_script.js
1 import { io
} from "socket.io-client";
2 import { initializeTada
} from "./tada";
11 import { startMedia
, stopMedia
} from "./media";
14 kSocketUrl: "https://junction.tranquil.services",
15 kIceServers: [{ urls: "stun:stun.l.google.com:19302" }],
21 internals
[message
.action
](message
.data
);
25 // TODO: How do we want to handle errors?
26 console
.error(error
.stack
|| error
);
30 internals
.port
= chrome
.runtime
.connect({ name: "content" });
31 internals
.port
.onMessage
.addListener(internals
.onMessage
);
34 async
joinAudioCall({ currentUrl
, tada
}) {
36 const mediaStream
= await
startMedia();
37 const playTada
= initializeTada(tada
);
39 const socket
= (internals
.socket
= io(internals
.kSocketUrl
, {
40 transports: ["websocket"],
43 socket
.on("error", internals
.handleError
);
44 socket
.on("connect_error", internals
.handleError
);
46 socket
.on("connect", () => {
47 console
.debug("Connected to signaling server, group: ", currentUrl
);
54 socket
.on("disconnect", () => {
55 console
.debug("disconnected from signaling server");
58 socket
.on("addPeer", ({ peerId
, shouldCreateOffer
}) => {
63 onOffer: (data
) => socket
.emit("relayOffer", data
),
69 socket
.on("offerReceived", async ({ peerId
, offer
}) => {
70 socket
.emit("relayAnswer", await
answerPeerOffer({ peerId
, offer
}));
73 socket
.on("answerReceived", (data
) => processPeerAnswer(data
));
74 socket
.on("ICECandidateReceived", (data
) => addIceCandidate(data
));
75 socket
.on("removePeer", (data
) => removePeer(data
));
77 internals
.port
.postMessage({
80 internals
.port
.disconnect();
87 internals
.socket
.close();
88 internals
.port
.disconnect();
93 chrome
.runtime
.onConnect
.addListener(() => {
97 console
.debug("Content Script Loaded");
99 // Indicates to the background script that we executed correctly